我有这个PHP:有没有写这个PHP更有效的方式/ jQuery的
<?php
$data = file_get_contents('http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/rss.xml');
$xml = simplexml_load_string($data);
$data1 = file_get_contents('http://www.skysports.com/rss/0,20514,11661,00.xml');
$xml1 = simplexml_load_string($data1);
$master[0] = $xml;
$master[1] = $xml1;
echo json_encode($master);
?>
这jQuery的:
var myRSS =[];
function rssReader() {
console.log('ran');
$.getJSON('bbc.php', function(data){
console.log(data);
$.each(data[0].channel.item, function(index, item){
// check if item title is stored in the array
if (jQuery.inArray(item.title, myRSS) != -1) {
//do nothing
} else {
// save item title in the array
myRSS.push(item.title);
// publish item
$('.container').prepend("<a target='_BLANK' href='" + item.link + "' class='title' data-date='" + item.pubDate + "'>" + item.title + "</a>");
$("title").text('EMG: ' + item.title);
$('#loader').remove();
}
});
$.each(data[1].channel.item, function(index, item){
// check if item title is stored in the array
if (jQuery.inArray(item.title, myRSS) != -1) {
//do nothing
} else {
// save item title in the array
myRSS.push(item.title);
// publish item
$('.container').prepend("<a target='_BLANK' href='" + item.link + "' class='title' data-date='" + item.pubDate + "'>" + item.title + "</a>");
$("title").text('EMG: ' + item.title);
}
});
});
}
rssReader();
setInterval(rssReader, 10000);
似乎有很多重复的代码,因此ISN非常干燥的编程。返回的JSON实际上具有与BBC和Sky Sports相同的结构,因此必须有更高效的写作方式。
感谢
当你做'file_get_contents('http:// some_url');',如果远程服务器需要时间来响应,它会冻结你的洞页。我认为为这项任务做一批是个好主意。 –
这是一个AJAX网络应用程序,所以如果PHP页面挂起,用户不会注意到。 – benhowdle89