2017-05-10 2 views
0

Ich habe den Code unten, um iTunes Charts direkt aus dem RSS zu holen und anzuzeigen. Ich möchte jedoch nur die Informationen auf dem rss-Eintrag für eine bestimmte ID anzeigen. Irgendeine Idee, wie das gemacht werden kann?So zeigen Sie einzelne ID von iTunes RSS

<script> 
jQuery(function($) { 
    $.ajax({ 
     type: "GET", 
     url: "https://itunes.apple.com/us/rss/topsongs/limit=200/xml", 
     dataType: "xml", 
     success: function(xml) { 
      var rank = 1; 
      $(".loading").html("").hide(); 
      $(xml).find('entry').each(function() { 
       var img = $(this).find("image[height=170]").first().text(); 
       var title = $(this).find('name').first().text().substring(0,35); 
       var artist = $(this).find('artist').first().text().substring(0,30); 
       var link = $(this).find('id').first().text(); 
       var m4a = $(this).find('link[title=Preview]').first().attr('href'); 
       var id = $(this).find('id').first().attr('im:id'); 
       var settings = {}; 
       var html = '<li class="' + id + '"><div class="artwork-wrapper"><audio class="player' + id + '" src="' + m4a + '"></audio><img src="' + img + '" class="artwork"></div><a href="' + link + '" target="_blank"><p class="title">' + rank +'. ' + title + '</p><p class="artist">' + artist + '</p></a></li>'; 
       $("ul.chartList").append($(html)); 
       $(".player"+id+"").player(settings); 
       rank++; 
      }); 
     } 
    }); 
}); 
</script> 

<ul class="chartList cf"> 
    <div class="loading"></div> 
</ul> 

Antwort

0

Es scheint, kann man einfach die ID in der Abfrage senden, so dass die URL so sein würde:

https://itunes.apple.com/us/rss/topsongs/limit=1&id=1227088906/xml 

Statt:

https://itunes.apple.com/us/rss/topsongs/limit=200/xml 

Wenn ich Sie wäre, ich würde vielleicht darüber nachdenken, eine Seite anzurufen, die Sie unter Kontrolle haben und senden Sie JSON mit cURL, so dass Sie ein Array zur Verarbeitung zurück erhalten können. Dann müssen Sie Ihre jQuery nicht die ganze Arbeit bei der Verarbeitung der Rückgabedaten machen lassen, denn zugegeben, was die iTunes-API ein wenig durcheinander bringt!

test.php:

# Curl send the request to iTunes 
function getAPI($url) 
    { 
     $con = curl_init(); 

     curl_setopt($con,CURLOPT_URL,$url); 
     curl_setopt($con,CURLOPT_RETURNTRANSFER,1); 
     $resp = curl_exec($con); 
     curl_close($con); 

     return json_decode($resp,true); 
    } 

if(!empty($_POST['action']) && $_POST['action'] == 'get_itunes_id') { 
    $url = 'https://itunes.apple.com/us/rss/topsongs/'.$_POST['url']; 
    # Fetch array 
    $array = getAPI($url); 
    # Process the array using backend and then send back what you need in array/json form 
    die(json_encode($array['feed']['entry'])); 
} 

index.php:

<script> 
$.ajax({ 
    url: '/test.php', 
    type: 'post', 
    data: { 
     "action":"get_itunes_id", 
     // You need to come up with your mechanism to send or not send the id 
     "url":"limit=1&id=1227088906/json" 
    }, 
    success: function(response) { 
     // Parse here and extract what you need from the array/object 
     var getJson = JSON.parse(response); 
     // Do code 
    } 
}); 
</script>