jwilsson / spotify-web-api-php

A PHP wrapper for Spotify's Web API.
MIT License
862 stars 156 forks source link

Malformed JSON error when using play #224

Closed ericlove02 closed 3 years ago

ericlove02 commented 3 years ago

I am getting this error when I try and use the Play endpoint: Fatal error: Uncaught SpotifyWebAPI\SpotifyWebAPIException: Malformed json in D:\wamp64\www\vendor\jwilsson\spotify-web-api-php\src\Request.php on line 57

My code: `<?php require '../vendor/autoload.php';

$api = new SpotifyWebAPI\SpotifyWebAPI(); session_start();

$api->setAccessToken($_SESSION['token']); $api->play($_SESSION['playback_device']);`

I am using the same code (with the endpoint change of course) for pause, next, and previous and am having no problems. Any ideas?

jwilsson commented 3 years ago

Hey! Spotify no longer seems to like an empty $options being sent. Passing null seems to be a workaround until I can get a fix out. For example:

$api->play($_SESSION['playback_device'], null);

Hope that solves it for you!

ericlove02 commented 3 years ago

Thanks for the idea. I tried that and now I am getting a new error: Fatal error: Uncaught SpotifyWebAPI\SpotifyWebAPIException: Server error. in D:\wamp64\www\vendor\jwilsson\spotify-web-api-php\src\Request.php on line 57

ericlove02 commented 3 years ago

It works correctly when I pass a uri, for example: $api->play($_SESSION['playback_device'], ['uris'=>['spotify:track:5QO79kh1waicV47BqGRL3g']]); but it does not like: $api->play($_SESSION['playback_device'], null);

jwilsson commented 3 years ago

Hmm, I can't think of anything more right now (unless you're fine with always passing a URI). I'll try and get a fix out ASAP.

ericlove02 commented 3 years ago

Yeah, I couldn't get it working without passing a uri, so for my application I ended up doing this:

$trackUri = "spotify:track:" . $_GET['id']; $position = $_GET['pos']; $api->play($_SESSION['playback_device'], ['uris'=>[$trackUri], 'position_ms'=>$position]);

jwilsson commented 3 years ago

I just published 4.2.2 which should solve the issue.

Let me know if it doesn't!