I am using jwilsson's spotify-web-api-php to return data from the Spotify API using PHP.
Also I'm using digitalnature's php-ref to return info about variables.
This is my code, apologies for the fact it's probably full of errors, misconceptions, rubbish etc.
// https://stackoverflow.com/questions/4345554/convert-a-php-object-to-an-associative-array
function objectToArray($r)
{
if (is_object($r)) {
if (method_exists($r, 'toArray')) {
return $r->toArray(); // returns result directly
} else {
$r = get_object_vars($r);
}
}
if (is_array($r)) {
$r = array_map(__FUNCTION__, $r); // recursive function call
}
return $r;
}
$session = new SpotifyWebAPI\Session(
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
);
$session->requestCredentialsToken();
$accessToken = $session->getAccessToken();
$api = new SpotifyWebAPI\SpotifyWebAPI();
$api->setAccessToken($accessToken);
$search = $api->search('fugazi','artist');
// convert object to array
$fff = objectToArray($search);
// php-ref to view info about the $fff array
r($fff);
// try to access the total value
$v1 = $fff[0]['artists']['total'];
This is what the php-ref looks like for the $fff
variable:
I tried to access the total
attribute via:
$v1 = $fff[0]['artists']['total'];
But receive these errors:
PHP Notice: Undefined offset: 0 in C:\websites\spotify\search.php on line 49
PHP Notice: Trying to access array offset on value of type null in C:\websites\spotify\search.php on line 49
PHP Notice: Trying to access array offset on value of type null in C:\websites\spotify\search.php on line 49
Sorry for asking a silly question and for not understanding what I'm doing, but, what am I doing wrong?