hubgit/web-resource

A PHP interface for fetching HTTP resources from the web

v0.4 2017-01-17 15:27 UTC

This package is not auto-updated.

Last update: 2025-04-26 23:20:53 UTC


README

composer require hubgit/web-resource

Usage

See examples.

Fetch a single resource

use WebResource\Resource;

$resource = new Resource('https://api.spotify.com/v1/artists/0gusqTJKxtU1UTmNRMHZcv');

$artist = $resource->get(); // `$artist` is an array

Fetch a paginated collection

use WebResource\Collection;

$collection = new Collection('https://api.github.com/users/hubgit/repos');

foreach ($collection as $repo) {
  // do something with the `$repo` array
}

Fetch a paginated collection with custom processing and pagination

use WebResource\Collection;

class SpotifyCollection extends Collection {
  // find the array of items in the JSON response
  protected function items($data, $response) {
    return $data['items'];
  }
  
  // find the URL of the next page in the JSON response
  protected function next($data, $response) {
    return isset($data['next']) ? $data['next'] : null;
  }
}

// first parameter is a URL, second parameter is an array of query parameters
$collection = new SpotifyCollection('https://api.spotify.com/v1/artists/0gusqTJKxtU1UTmNRMHZcv/albums', [
  'album_type' => 'single',
]);

foreach ($collection as $album) {
  // do something with the `$album` array
}