1

I hope you are well. I am brand new to coding and have spent a lot of time trying to find a solution to my question, without much luck.

I was trying to utilize Spotify's API with PHP but ran into thus far unsurmountable hurdles. Please see steps taken below:

1.) I downloaded the Spotify PHP Wrapper library here https://github.com/jwilsson/spotify-web-api-php, and saved it in the same directory as my app. I then created a composer.json file as below :

{
    "require": {
        "jwilsson/spotify-web-api-php": "^1.0.0"
    }
}

2.) After creating and saving the composer.json file, I ran "composer install" in terminal in the same directory as where the composer.json file was stored, which led to the creation of a composer.lock file and a vendor folder.

3.) I then moved the composer.lock, composer.json and vendor folders to my online hosting service (ecowebhosting) via firebug, so that I can run and test the code. I also created a .php file that I moved to the server as well.

In my .php page, i declared the required files as such (below) and ran a sample query form the Spotify API PHP Wrapper documentation on github, to confirm whether or not I successfully loaded the library (the autoload.php folder was automatically created in the "Vendor" folder, after I performed step 2 above):

require 'vendor/autoload.php';
$tracks = $api->getAlbumTracks('1oR3KrPIp4CbagPa3PhtPp');
print_r ($tracks);

When I run the code above, I get the error below:

Notice: Undefined variable: api in /home/sites/amanka.com/public_html/content/SmallApps/ListeningPod/home.php on line 8 Fatal error: Call to a member function getAlbumTracks() on a non-object in /home/sites/amanka.com/public_html/content/SmallApps/ListeningPod/home.php on line 8

This error causes me to think that I have not installed the Library correctly. Please any ideas where I might be going wrong?

Thanks a lot

TryHardy91
  • 13
  • 2
  • You probably have installed the library but seems like you didn't create the api object. There is an example here : https://github.com/jwilsson/spotify-web-api-php/blob/master/docs/examples/getting-objects.md you missed the "$api = new SpotifyWebAPI\SpotifyWebAPI();" – engvrdr Nov 01 '16 at 00:35
  • Thanks a lot for the feedback! That makes sense. When I make the change, I am met with a different error, which seems strange as it is supposedly coming from a file in the library (not my code). I am searching around to see if anyone has stumbled on a similar error. **Parse error: syntax error, unexpected '[' in /home/sites/amanka.com/public_html/content/SmallApps/ListeningPod/vendor/jwilsson/spotify-web-api-php/src/SpotifyWebAPI.php on line 7** – TryHardy91 Nov 01 '16 at 00:47
  • what is the php version in the server you are trying this? php short array syntax is introduced in php5.4 and library requires 5.5 or 7.0, so my assumption would be you are installing the library locally which you have a newer version than the server. – engvrdr Nov 01 '16 at 00:52
  • The server was initially running 5.2. I updated to 5.5 and it works great now. Can't say thank you enough. I truly appreciate your time. – TryHardy91 Nov 01 '16 at 01:05

0 Answers0