好的,我让它工作了。:) 为每个标题显示一个图像。我的代码:
首先,我正在使用 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"))
?>