Using the Pandora API with PHP

Posted in PHP -

Pandora is a music streaming service which can be accessed via a web browser, smartphone/tablet app or desktop app. There is a closed API which can be used to extract some information from Pandora (and edit radio stations etc). This post looks at using a PHP API by Alex Dallaway to access radio stations and bookmarks from Pandora.

tl;dr

Download the code here and try it out. Note that I test this sort of stuff using the PHP CLI and not in a web browser, so all the line breaks are \n and there's no HTML formatting.

Disclaimer

As far as I am aware, the Pandora API is closed and not documented officially. It is therefore subject to change at any time and the code on this page may not work in the future. The PHP API can be downloaded from GitHub, and there's some 3rd party documentation of the API here.

Motivation for doing this

I wanted to be able to export my list of thumbed up tracks from Pandora with PHP. Unfortunately there doesn't appear to be a (known) API call for doing this so my efforts didn't achieve much. However, I thought I should post the code examples here for other people who would like to have a go as well.

There are a variety of in browser methods to extract the list of likes, including browser plugins which will turn Pandora likes into Spotify playlists. I haven't tried it myself yet so don't know how well it works.

Example - logging in

Set the correct path to the Pandora.php API library in the require_once() statement and set up $username and $password variables with your Pandora username and password.

I set up the handleError() function to provide a simple way to report errors after each API call, but you could just as easily subclass the Pandora class and handle it another way, or take another approach.

require_once('path/to/Pandora.php');

use php_pandora_api\Pandora;

$p = new Pandora();

// login to Pandora
if (!$p->login($username, $password)) {
    handleError($p);
}

function handleError(Pandora $p)
{
    echo "Error message: $p->last_error\n";
    echo "Last request: ";
    print_r(json_decode($p->last_request_data));
    echo "Last response: ";
    print_r(json_decode($p->last_response_data));
    exit;
}

Example - get the station list

Call the 'user.getStationList' method to get the list of stations. The easiest way to get a complete list of the data returned from each call is to use print_r() and then structure your output based on what's returned. The example below echoes out the station token and station name; the token is used in other API calls.

// get the station list and show the token and station name
if (!$response = $p->makeRequest('user.getStationList')) {
    handleError($p);
}
// print_r($response); // use print_r() to get the full dataset returned to see what's available
echo "Station list:\n";
foreach($response['stations'] as $station) {
    echo "  $station[stationToken] - $station[stationName]\n";
} 

Using print_r, a single station's output looks something like this (I've replaced some of the values that I don't want to share with ...):

[1] => Array
    (
        [isQuickMix] => 
        [stationId] => ...
        [stationDetailUrl] => ...
        [genre] => Array
            (
                [0] => Jazz
                [1] => Holiday
            )

        [isShared] => 
        [dateCreated] => Array
            (
                [date] => 19
                [day] => 4
                [hours] => 11
                [minutes] => 44
                [month] => 11
                [nanos] => 51000000
                [seconds] => 27
                [time] => 1387482267051
                [timezoneOffset] => 480
                [year] => 113
            )

        [stationToken] => ...
        [stationName] => ...
        [stationSharingUrl] => ...
        [allowRename] => 1
        [allowAddMusic] => 
        [allowDelete] => 1
    )

Example - get user's bookmarks

Although there doesn't appear to be a method to get the user's likes / thumb ups, you can get a list of the bookmarked songs and artists using the 'user.getBookmarks' call:

// get the user's bookmarks
if(!$response = $p->makeRequest('user.getBookmarks')) {
    handleError($p);
}
// print_r($response); // use print_r() to get the full dataset returned to see what's available
echo "Bookmarked songs:\n";
foreach($response['songs'] as $song) {
    echo "  $song[artistName] - $song[albumName] - $song[songName]\n";
}
echo "Bookmarked artists:\n";
foreach($response['artists'] as $artist) {
    echo "  $artist[artistName]\n";
}

print_r() output for a single song:

[26] => Array
    (
        [sampleGain] => -5.77
        [musicToken] => S1773395
        [bookmarkToken] => ...
        [sampleUrl] => ...
        [albumName] => Divergent Spectrum
        [songName] => Lights (Bassnectar Remix)
        [artUrl] => ...
        [dateCreated] => Array
            (
                [date] => 12
                [day] => 6
                [hours] => 20
                [minutes] => 10
                [month] => 0
                [nanos] => 241000000
                [seconds] => 52
                [time] => 1358050252241
                [timezoneOffset] => 480
                [year] => 113
            )

        [artistName] => Ellie Goulding
    )

and for a single artist:

[5] => Array
    (
        [artistName] => Calvin Harris
        [dateCreated] => Array
            (
                [date] => 12
                [day] => 6
                [hours] => 18
                [minutes] => 0
                [month] => 0
                [nanos] => 970000000
                [seconds] => 24
                [time] => 1358042424970
                [timezoneOffset] => 480
                [year] => 113
            )

        [bookmarkToken] => ...
        [artUrl] => ...
        [musicToken] => R247557
    )

Download the example code

As noted at the top of the post, you can download the code here and try it out. Note that I test this sort of stuff using the PHP CLI and not in a web browser, so all the line breaks are \n and there's no HTML formatting.

The example code also includes the 'station.getPlaylist' which gets a few tracks which the app would then play from the radio station. Check out the disclaimer section at the top of this post which includes a link to some documentation of the API for other available methods.




Comments