[GH-ISSUE #214] getMyRecentTracks timestamp issue #149

Closed
opened 2026-02-27 19:26:19 +03:00 by kerem · 2 comments
Owner

Originally created by @ghost on GitHub (Jan 2, 2021).
Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/214

Hi,

I'm using the getMyRecentTracks Method to return recent tracks.

It works fine for retrieving my recent tracks but I'm experiencing issues with the pagination part of the method.

I've read your docs for how to use it and have also read the relevant Spotify docs.

I want to add next / previous links to the page displaying the recent tracks, and for that I need to retrieve the 'after' and 'before' values from the 'cursors' array returned by the API.

I am making sure to set the timestamp parameter (x) as an integer by passing it through a string to number function.

This is my code:

$x = $_GET['x']; // x = 1609412257987
var_dump($x); // string(13) "1609586161186"
$x = (int)$x);
var_dump($x); //  int(1609586161186) 

// @@@@@@@@@@@@@@@ CALL THE API @@@@@@@@@@@@@@@

$options = ['return_assoc' => true];
$api = new SpotifyWebAPI\SpotifyWebAPI($options);	
$api->setAccessToken($accessToken);

// @@@@@@@@@@@@@@@ getMyRecentTracks @@@@@@@@@@@@@@@

try {
    // $recent_options = ['limit' => $limit,'before' => '$x'];
    $recent_options = ['limit' => $limit,'before' => '1609412257987'];
    $my_recent = $api->getMyRecentTracks($recent_options);
    var_dump($my_recent);
} catch (SpotifyWebAPI\SpotifyWebAPIException $e) {
    header('Location:index.php');
    exit;
}

When I hard-code the before option value as 1609412257987 then the method returns an array without an issue.

If I change this:

// $recent_options = ['limit' => $limit,'before' => '$x'];
$recent_options = ['limit' => $limit,'before' => '1609412257987'];

To this:

$recent_options = ['limit' => $limit,'before' => '$x'];
// $recent_options = ['limit' => $limit,'before' => '1609412257987'];

Then the method does not return an array, even though I have confirmed earlier on via var_dump($x); that `$x' is an integer.

I also made sure to try with out turning the $x string to an integer, but get the same result when it is defined as a string.

I'm not sure what might be going on - just wondered if this is an issue with the actual Spotify API's method rather than the getMyRecentTracks method in this respository?

I could ask on the Spotify Community, but I don't know how to use their API without using the techniques used in this respository so am not sure how to ask there.

Thanks

Jim

Originally created by @ghost on GitHub (Jan 2, 2021). Original GitHub issue: https://github.com/jwilsson/spotify-web-api-php/issues/214 Hi, I'm using the `getMyRecentTracks` Method to return recent tracks. It works fine for retrieving my recent tracks but I'm experiencing issues with the pagination part of the method. I've read [your docs](https://github.com/jwilsson/spotify-web-api-php/blob/9063d2f18934319b71393fb1780de32d45897df9/docs/method-reference/SpotifyWebAPI.md#getmyrecenttracks) for how to use it and have also read the [relevant Spotify docs](https://developer.spotify.com/documentation/web-api/reference/player/get-recently-played/). I want to add next / previous links to the page displaying the recent tracks, and for that I need to retrieve the 'after' and 'before' values from the 'cursors' array returned by the API. I am making sure to set the timestamp parameter (`x`) as an integer by passing it through a string to number function. This is my code: ``` $x = $_GET['x']; // x = 1609412257987 var_dump($x); // string(13) "1609586161186" $x = (int)$x); var_dump($x); // int(1609586161186) // @@@@@@@@@@@@@@@ CALL THE API @@@@@@@@@@@@@@@ $options = ['return_assoc' => true]; $api = new SpotifyWebAPI\SpotifyWebAPI($options); $api->setAccessToken($accessToken); // @@@@@@@@@@@@@@@ getMyRecentTracks @@@@@@@@@@@@@@@ try { // $recent_options = ['limit' => $limit,'before' => '$x']; $recent_options = ['limit' => $limit,'before' => '1609412257987']; $my_recent = $api->getMyRecentTracks($recent_options); var_dump($my_recent); } catch (SpotifyWebAPI\SpotifyWebAPIException $e) { header('Location:index.php'); exit; } ``` When I hard-code the `before` option value as `1609412257987` then the method returns an array without an issue. If I change this: // $recent_options = ['limit' => $limit,'before' => '$x']; $recent_options = ['limit' => $limit,'before' => '1609412257987']; To this: $recent_options = ['limit' => $limit,'before' => '$x']; // $recent_options = ['limit' => $limit,'before' => '1609412257987']; Then the method does not return an array, even though I have confirmed earlier on via `var_dump($x);` that `$x' is an integer. I also made sure to try with out turning the `$x` string to an integer, but get the same result when it is defined as a string. I'm not sure what might be going on - just wondered if this is an issue with the actual Spotify API's method rather than the `getMyRecentTracks` method in this respository? I could ask on the Spotify Community, but I don't know how to use their API without using the techniques used in this respository so am not sure how to ask there. Thanks Jim
kerem 2026-02-27 19:26:19 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@jwilsson commented on GitHub (Jan 2, 2021):

Hi Jim!
Right now, when using '$x' the literal string "$x" will be sent instead of the value of $x. If you want the value to be used double quotes can be used but in this case I'd just write 'before' => $x and it should work.

Cheers!

<!-- gh-comment-id:753483190 --> @jwilsson commented on GitHub (Jan 2, 2021): Hi Jim! Right now, when using `'$x'` the literal string "$x" will be sent instead of the value of `$x`. If you want the value to be used double quotes can be used but in this case I'd just write `'before' => $x` and it should work. Cheers!
Author
Owner

@ghost commented on GitHub (Jan 2, 2021):

Hi Jonathan!

As Homer Simpson would say, Doh!!!

I feel a bit silly for not spotting that, I wasted about an hour on that :-)

Thanks again for your help and for this excellent resource.

And, Happy New Year to you.

Jim

<!-- gh-comment-id:753485640 --> @ghost commented on GitHub (Jan 2, 2021): Hi Jonathan! As Homer Simpson would say, Doh!!! I feel a bit silly for not spotting that, I wasted about an hour on that :-) Thanks again for your help and for this excellent resource. And, Happy New Year to you. Jim
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotify-web-api-php#149
No description provided.