0

I'm creating a music based application, and i'm trying to access the iTunes api to return a link to purchase an album, that is generated dynamically based on the album clicked on (which works fine, and generates a link to the correct JSON data). An Example link

What i can't get my head around is the accessing of the 'collectionViewUrl' key in the JSON, which i simply want to set as the href of a link that appears after the album art is selected, which will be scored in; $topalbums[$i]['iTunes'] = ....

I've tried using json_decode to access the data but whenever I try to access the key I need i get no return. Am I missing something simple with this?

==================================

(My current code for this section)

$iTunes_link_prefix = "https://itunes.apple.com/search?country=GB&media=music&entity=album&limit=1&artist=";

    for ($i = 0; $i < 6; $i++) {
        $topalbum[$i]['title'] = $xml['top_albums']->topalbums->album[$i]->name;
        $topalbum[$i]['image'] = $xml['top_albums']->topalbums->album[$i]->image[3];

        $json_link = $iTunes_link_prefix . $artist . "&term=" . urlencode($topalbum[$i]['title']);

        $topalbum[$i]['itunes'] = (Contents of the collectionViewUrl Key)

    }
4

3 回答 3

0

For this, remember that the json that you get has to be transformed "before" you can actually operate with it. As i see in the example that you show you have to do somehting like get the json content (with curl or don't know how you do), as you get the content you do:

$itunesDisc = json_decode($jsonFromItunes);

Then, you have the contents of the json. If all the json's that iTunes returns are in the form of response:[...] you can do this for simplicity

$itunesDisc = $itunesDisc["response"];

Then you can walk through the array. Maybe this page can help you in show you the content of a json response http://jsonviewer.stack.hu/

于 2013-04-18T17:35:18.680 回答
0

A simple solution, just change your foreach code to this:

for ($i = 0; $i < 6; $i++) {
    $topalbum[$i]['title'] = $xml['top_albums']->topalbums->album[$i]->name;
    $topalbum[$i]['image'] = $xml['top_albums']->topalbums->album[$i]->image[3];

    $json_link = "{$iTunes_link_prefix}{$artist}&term=" . urlencode($topalbum[$i]['title']);

    // Fetch the JSON data from iTunes - you can add some more verifications here
    // to make sure your app runs smooth if the request fails for some reason
    $itunes_json       = file_get_contents($json_link);
    $itunes_album_data = json_decode($itunes_json, true);

    // Access the URL from the 1st result if any
    if (!empty($itunes_album_data['results'][0]['collectionViewUrl'])) {
        $topalbum[$i]['itunes'] = $itunes_album_data['results'][0]['collectionViewUrl'];
    }
}
于 2013-04-18T17:37:27.427 回答
0

I found that the error was actually in the generation of the link to the JSON after I tried entering the static link in place of the dynamically generated one. The $artist variable needed to be run through urlencode(). The following is the final code for the section, thanks for assistance!

for ($i = 0; $i < 6; $i++) {
        $topalbum[$i]['title'] = $xml['top_albums']->topalbums->album[$i]->name;
        $topalbum[$i]['image'] = $xml['top_albums']->topalbums->album[$i]->image[3];

        $json_link = $iTunes_link_prefix . urlencode($artist) . "&term=" . urlencode($topalbum[$i]['title']);
        $itunes_json       = file_get_contents($json_link);
        $itunes_album_data = json_decode($itunes_json);

        if (!empty($itunes_album_data->results[0]->collectionViewUrl)) {
            $topalbum[$i]['itunes'] = $itunes_album_data->results[0]->collectionViewUrl;
        }
    }
于 2013-04-18T18:24:26.817 回答