user1048676
user1048676

Reputation: 10066

Search iTunes Music Library for a search term via API

All, I'm trying to allow a user to search for a song. Say the user is searching for Jack Johnson and I want to display all of the songs on iTunes by Jack Johnson. I know I need to construct a URL like the following:

http://itunes.apple.com/search?term=jack+johnson&limit=25&media=music&entity=musicArtist,musicTrack,album,mix,song

From a programming persepective, how would I send this to Apple? If I copy that URL into my browser I get some results. Based on those results, how could I parse those results so that I can display them back to the user? Would I want to use JSON, cURL, a combination of the two? What would be the most efficient way to do this?

Any help here would be greatly appreciated!

Upvotes: 1

Views: 6925

Answers (3)

Zul
Zul

Reputation: 3608

If I understand correctly what you want, maybe something like this:

<?php
if(isset($_POST['term']))
{
    $term = urlencode($_POST['term']); // user input 'term' in a form
    $json =  file_get_contents('http://itunes.apple.com/search?term='.$term.'&limit=25&media=music&entity=musicArtist,musicTrack,album,mix,song');    
    $array = json_decode($json, true);

    foreach($array['results'] as $value)
    {
        echo '<p>';
        echo $value['artistName'].'<br />';
        echo $value['artistLinkUrl'].'<br />';
        echo $value['primaryGenreName'];
        echo '</p>';
    }
}
?> 

<form method="post">
    <input type="text" name="term" /><input type="submit" value="Go" />
</form>

For more $array value, try print_r($array);

Upvotes: 4

NuclearDog
NuclearDog

Reputation: 91

The request must be performed over HTTP. JSON is only a format for the data it returns, not a method of retrieving it.

There are many ways you could fetch that data.

The simplest option is simply to use file_get_contents if your server has fopen wrappers enabled (it likely does).

$data = file_get_contents('http://itunes.apple.com/search?term=jack+johnson&limit=25&media=music&entity=musicArtist,musicTrack,album,mix,song');

cURL is also an option using something like:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://itunes.apple.com/search?term=jack+johnson&limit=25&media=music&entity=musicArtist,musicTrack,album,mix,song");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);

Once you've retrieved the contents of that page, unserializing it is easy as long as you have the JSON extension installed. If you're using PHP 5.2.0+, then it's part of the core. Otherwise you need to install the PECL module. See the PHP manual page on the extension for more information.

$response = json_decode($data);
var_dump($response);

The unserialized data is an object. You could use it like so:

foreach ($response->results as $result)
{
    echo "Artist: ".$result->artistName."<br />".PHP_EOL;
}

Upvotes: 1

mowwwalker
mowwwalker

Reputation: 17334

You would use file_get_contents and parse the result with json_decode.

$js= json_decode(file_get_contents('http://itunes.apple.com/search?term=jack+johnson&limit=25&media=music&entity=musicArtist,musicTrack,album,mix,song'));

$js would now be an array with all the information apple returned.

Upvotes: 1

Related Questions