Javascript 如何从iTunes RSS显示单个Id
我有下面的代码,可以直接从RSS获取iTunes图表并显示出来。但是,我只想在rss条目上显示一个特定id的信息。你知道怎么做吗Javascript 如何从iTunes RSS显示单个Id,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有下面的代码,可以直接从RSS获取iTunes图表并显示出来。但是,我只想在rss条目上显示一个特定id的信息。你知道怎么做吗 <script> jQuery(function($) { $.ajax({ type: "GET", url: "https://itunes.apple.com/us/rss/topsongs/limit=200/xml", dataType: "xml", success: fu
<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>
jQuery(函数($){
$.ajax({
键入:“获取”,
url:“https://itunes.apple.com/us/rss/topsongs/limit=200/xml",
数据类型:“xml”,
成功:函数(xml){
var秩=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 html='”;
$($图表列表”).append($(html));
$(“.player”+id+”).player(设置);
排名++;
});
}
});
});
看起来您可以在查询中发送id,因此url如下所示:
https://itunes.apple.com/us/rss/topsongs/limit=1&id=1227088906/xml
而不是:
https://itunes.apple.com/us/rss/topsongs/limit=200/xml
如果我是你,我可能会考虑调用你控制的页面,并使用cURL
发送JSON
,这样你就可以返回一个数组进行处理。然后,您就不必让jQuery完成处理返回数据的所有工作,因为无可否认,iTunesAPI返回的数据有点乱
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']));
}
<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>
index.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']));
}
<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>
$.ajax({
url:“/test.php”,
键入:“post”,
数据:{
“操作”:“获取itunes\u id”,
//您需要想出发送或不发送id的机制
“url”:“limit=1&id=1227088906/json”
},
成功:功能(响应){
//在这里解析并从数组/对象中提取所需内容
var getJson=JSON.parse(响应);
//执行代码
}
});