PHP code example of cjavdev / spotted

1. Go to this page and download the library: Download cjavdev/spotted library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cjavdev / spotted example snippets




use Spotted\Client;

$client = new Client(
  accessToken: getenv('SPOTIFY_ACCESS_TOKEN') ?: 'My Access Token'
);

$album = $client->albums->retrieve('4aawyAB9vmqN3uQ7FjRGTy');

var_dump($album->id);



use Spotted\Client;

$client = new Client(
  accessToken: getenv('SPOTIFY_ACCESS_TOKEN') ?: 'My Access Token'
);

$page = $client->shows->listEpisodes('showid', limit: 10, offset: 20);

var_dump($page);

// fetch items from the current page
foreach ($page->getItems() as $item) {
  var_dump($item->id);
}
// make additional network requests to fetch items from all pages, including and after the current page
foreach ($page->pagingEachItem() as $item) {
  var_dump($item->id);
}



use Spotted\Core\Exceptions\APIConnectionException;
use Spotted\Core\Exceptions\RateLimitException;
use Spotted\Core\Exceptions\APIStatusException;

try {
  $album = $client->albums->retrieve('4aawyAB9vmqN3uQ7FjRGTy');
} catch (APIConnectionException $e) {
  echo "The server could not be reached", PHP_EOL;
  var_dump($e->getPrevious());
} catch (RateLimitException $e) {
  echo "A 429 status code was received; we should back off a bit.", PHP_EOL;
} catch (APIStatusException $e) {
  echo "Another non-200-range status code was received", PHP_EOL;
  echo $e->getMessage();
}



use Spotted\Client;

// Configure the default for all requests:
$client = new Client(requestOptions: ['maxRetries' => 0]);

// Or, configure per-request:
$result = $client->albums->retrieve(
  '4aawyAB9vmqN3uQ7FjRGTy', requestOptions: ['maxRetries' => 5]
);



$album = $client->albums->retrieve(
  '4aawyAB9vmqN3uQ7FjRGTy',
  requestOptions: [
    'extraQueryParams' => ['my_query_parameter' => 'value'],
    'extraBodyParams' => ['my_body_parameter' => 'value'],
    'extraHeaders' => ['my-header' => 'value'],
  ],
);



$response = $client->request(
  method: "post",
  path: '/undocumented/endpoint',
  query: ['dog' => 'woof'],
  headers: ['useful-header' => 'interesting-value'],
  body: ['hello' => 'world']
);