jwilsson / spotify-web-api-php

A PHP wrapper for Spotify's Web API.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can i just get album cover

salimcansatici opened this issue · comments

Hi,

I did everything but how can I get only the album cover?

Print_r output;

stdClass Object ( [timestamp] => 1611682228121 [context] => [progress_ms] => 74362 [item] => stdClass Object ( [album] => stdClass Object ( [album_type] => single [artists] => Array ( [0] => stdClass Object ( [external_urls] => stdClass Object ( [spotify] => https://open.spotify.com/artist/7Gx2wDVHyW5HJh7dc2t5h7 ) [href] => https://api.spotify.com/v1/artists/7Gx2wDVHyW5HJh7dc2t5h7 [id] => 7Gx2wDVHyW5HJh7dc2t5h7 [name] => Lil Zey [type] => artist [uri] => spotify:artist:7Gx2wDVHyW5HJh7dc2t5h7 ) ) [available_markets] => Array ( [0] => AD [1] => AE [2] => AL [3] => AR [4] => AT [5] => AU [6] => BA [7] => BE [8] => BG [9] => BH [10] => BO [11] => BR [12] => BY [13] => CA [14] => CH [15] => CL [16] => CO [17] => CR [18] => CY [19] => CZ [20] => DE [21] => DK [22] => DO [23] => DZ [24] => EC [25] => EE [26] => EG [27] => ES [28] => FI [29] => FR [30] => GB [31] => GR [32] => GT [33] => HK [34] => HN [35] => HR [36] => HU [37] => ID [38] => IE [39] => IL [40] => IN [41] => IS [42] => IT [43] => JO [44] => JP [45] => KW [46] => KZ [47] => LB [48] => LI [49] => LT [50] => LU [51] => LV [52] => MA [53] => MC [54] => MD [55] => ME [56] => MK [57] => MT [58] => MX [59] => MY [60] => NI [61] => NL [62] => NO [63] => NZ [64] => OM [65] => PA [66] => PE [67] => PH [68] => PL [69] => PS [70] => PT [71] => PY [72] => QA [73] => RO [74] => RS [75] => RU [76] => SA [77] => SE [78] => SG [79] => SI [80] => SK [81] => SV [82] => TH [83] => TN [84] => TR [85] => TW [86] => UA [87] => US [88] => UY [89] => VN [90] => XK [91] => ZA ) [external_urls] => stdClass Object ( [spotify] => https://open.spotify.com/album/28mncPY48RKO2fxCjvbr7X ) [href] => https://api.spotify.com/v1/albums/28mncPY48RKO2fxCjvbr7X [id] => 28mncPY48RKO2fxCjvbr7X [images] => Array ( [0] => stdClass Object ( [height] => 640 [url] => https://i.scdn.co/image/ab67616d0000b27338bcf717008d05db618b15ca [width] => 640 ) [1] => stdClass Object ( [height] => 300 [url] => https://i.scdn.co/image/ab67616d00001e0238bcf717008d05db618b15ca [width] => 300 ) [2] => stdClass Object ( [height] => 64 [url] => https://i.scdn.co/image/ab67616d0000485138bcf717008d05db618b15ca [width] => 64 ) ) [name] => Zor [release_date] => 2020-10-02 [release_date_precision] => day [total_tracks] => 2 [type] => album [uri] => spotify:album:28mncPY48RKO2fxCjvbr7X ) [artists] => Array ( [0] => stdClass Object ( [external_urls] => stdClass Object ( [spotify] => https://open.spotify.com/artist/7Gx2wDVHyW5HJh7dc2t5h7 ) [href] => https://api.spotify.com/v1/artists/7Gx2wDVHyW5HJh7dc2t5h7 [id] => 7Gx2wDVHyW5HJh7dc2t5h7 [name] => Lil Zey [type] => artist [uri] => spotify:artist:7Gx2wDVHyW5HJh7dc2t5h7 ) ) [available_markets] => Array ( [0] => AD [1] => AE [2] => AL [3] => AR [4] => AT [5] => AU [6] => BA [7] => BE [8] => BG [9] => BH [10] => BO [11] => BR [12] => BY [13] => CA [14] => CH [15] => CL [16] => CO [17] => CR [18] => CY [19] => CZ [20] => DE [21] => DK [22] => DO [23] => DZ [24] => EC [25] => EE [26] => EG [27] => ES [28] => FI [29] => FR [30] => GB [31] => GR [32] => GT [33] => HK [34] => HN [35] => HR [36] => HU [37] => ID [38] => IE [39] => IL [40] => IN [41] => IS [42] => IT [43] => JO [44] => JP [45] => KW [46] => KZ [47] => LB [48] => LI [49] => LT [50] => LU [51] => LV [52] => MA [53] => MC [54] => MD [55] => ME [56] => MK [57] => MT [58] => MX [59] => MY [60] => NI [61] => NL [62] => NO [63] => NZ [64] => OM [65] => PA [66] => PE [67] => PH [68] => PL [69] => PS [70] => PT [71] => PY [72] => QA [73] => RO [74] => RS [75] => RU [76] => SA [77] => SE [78] => SG [79] => SI [80] => SK [81] => SV [82] => TH [83] => TN [84] => TR [85] => TW [86] => UA [87] => US [88] => UY [89] => VN [90] => XK [91] => ZA ) [disc_number] => 1 [duration_ms] => 180392 [explicit] => [external_ids] => stdClass Object ( [isrc] => TRUM72000090 ) [external_urls] => stdClass Object ( [spotify] => https://open.spotify.com/track/07BSbfzLQ4t09hwD8PO7iT ) [href] => https://api.spotify.com/v1/tracks/07BSbfzLQ4t09hwD8PO7iT [id] => 07BSbfzLQ4t09hwD8PO7iT [is_local] => [name] => Zor [popularity] => 64 [preview_url] => https://p.scdn.co/mp3-preview/1b6d2cc14380bf17dccc84f38b4c87ed066d3275?cid=[Are you right? i hidden] [track_number] => 1 [type] => track [uri] => spotify:track:07BSbfzLQ4t09hwD8PO7iT ) [currently_playing_type] => track [actions] => stdClass Object ( [disallows] => stdClass Object ( [resuming] => 1 ) ) [is_playing] => 1

thanks,

Hi!
I'm not quite sure what you mean?

There is an array of image objects returned, which you can access for example:

$image = $my_data->item->images[0]->url;

I don't know of a way to only get the image, just an album object with the images array and fetch the image from there.

Hi,

Notice: Undefined variable: my_data in C:\xampp\htdocs\index.php on line 89
Notice: Trying to get property 'item' of non-object in C:\xampp\htdocs\index.php on line 89
Notice: Trying to get property 'images' of non-object in C:\xampp\htdocs\index.php on line 89
Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\index.php on line 89
Notice: Trying to get property 'url' of non-object in C:\xampp\htdocs\index.php on line 89

Sorry, it didn't work. I know that I need to get value in the Array. But "print_r ($ api-> getMyCurrentTrack ());" I don't know how to get images from the output that comes in the code. Can you help me?

Thanks,

Hey!
All right, so you assign the return value of $api->getMyCurrentTrack() to the $my_data variable and then you can access the image URL like this:

$my_data = $api->getMyCurrentTrack();

$image = $my_data->item->images[0]->url;

echo $image;

Merhaba,

When I try this code, I still get an error. The std object data comes but I cannot image value select it from it.

Notice: Undefined property: stdClass::$images in C:\xampp\htdocs\index.php on line 90
Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\index.php on line 90
Notice: Trying to get property 'url' of non-object in C:\xampp\htdocs\index.php on line 90

There is a lot of them data in $mydata, so I can get all the information. There is no problem, but I cannot get the images part. The arrays in my question are coming.

Thank you,
PHP Version 7.4.14
Composer v2.---

Hmm, how does the complete code you're using look? You can wrap it in ``` to make it easier to read. Also, remember to remove your client ID/secret before posting.

Below you can see the php code.

Thanks;

require 'vendor/autoload.php';
ob_start();

$session = new SpotifyWebAPI\Session(
    'Hidden',
    'Hidden',
    'http://localhost:80'
);
$api = new SpotifyWebAPI\SpotifyWebAPI(['auto_refresh' => true, 'auto_retry' => true]);
if (isset($_GET['code'])) {
    $session->requestAccessToken($_GET['code']);
    $api->setAccessToken($session->getAccessToken());
    // $refresh_token = $session->getRefreshToken();
    //print_r($api->me());
} else {
    $options = [
        'scope' => [
            'user-read-currently-playing',
            'user-read-playback-state'
        ],
    ];

    header('Location: ' . $session->getAuthorizeUrl($options));
    die();
}

$my_data = $api->getMyCurrentTrack();
$image = $my_data->item->images[0]->url;
echo $image;

Sorry, my bad.

In my example code I forgot the album part. Replace the $image = ... line with this and it should work:

$image = $my_data->item->album->images[0]->url;