好的,我得到它的工作。 :)每個標題都顯示一個圖像。我的代碼:
首先,我下載使用curl RSS提要:
<?php
if (function_exists("curl_init")){
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL, 'http://menmedia.co.uk/manchestereveningnews/news/rss.xml');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
//curl_setopt($ch,CURLOPT_HEADER,0);
$data=curl_exec($ch);
curl_close($ch);
$doc=new SimpleXmlElement($data,LIBXML_NOCDATA);
然後我解析RSS提要。方法1和方法2都產生相同的輸出。
function parseRSS($xml){
$encl_array=array();
$x=0;
$i=0;
$encls=$xml->xPath('/rss/channel/item/enclosure');
foreach($encls as $encl) {
$encl_array[$x]=$encl->attributes()->url;
$x+=1;
}
//method 1
/*$cnt=count($xml->channel->item);
for($i=0;$i<$cnt;$i++){
$title=$xml->channel->item[$i]->title;
print_r('<img src='.$encl_array[$i].'/>');
echo $title.'<br>';
}*/
//method 2 - using xPath
$items=$xml->xPath('/rss/channel/item');
foreach ($items as $item){
echo('<img src='.$encl_array[$i].'/>');
echo($item->title.'<br>');
$i+=1;
}
}//end function parseRSS
if (isset($doc->channel)) parseRSS($doc);
}//end if (function_exists("curl_init"))
?>
提供XML的片段或指向我們的RSS Feed,這樣我們就可以回答這個問題。 – 2012-07-15 05:02:29