2014-12-25 101 views
-2

您好先生我想從RSS Feed使用捲曲方法獲取圖像路徑...我試過這個代碼,但它不工作...請幫助!!!!使用捲曲方法從RSS提要獲取圖像路徑

<?php 
$feed = "http://feeds.bbci.co.uk/news/uk/rss.xml"; 

// Use cURL to fetch text 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $feed); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_USERAGENT, $useragent); 
$rss = curl_exec($ch); 
curl_close($ch); 

// Manipulate string into object 
$rss = simplexml_load_string($rss); 

$siteTitle = $rss->channel->title; 
echo "<h1>".$siteTitle."</h1>"; 
echo "<hr />"; 

$cnt = count($rss->channel->item); 

for($i=0; $i<10; $i++) 
{ 
    $images = $rss->channel->thumbnail->xpath('url'); 
    echo "Image Path : ".$image; 
    echo "</br>"; 
} 

?> 

回答

0

http://feeds.bbci.co.uk/news/uk/rss.xml源代碼我犯規看到縮略圖兒童信道節點

如果你想獲得通道圖像嘗試

$rss->channel->image->url 

如果你是RSS初學者,想分析更多的網站嘗試使用了SimplePie庫。非常容易使用並實現更多的atom/rss的限制。

+0

我累了,但又不工作 – user3193751

+0

我寫的只是返回SimpleXMLElement。你需要像(字符串)$ rss-> channel-> image-> url一樣使用它。但是在你的代碼中有更多的錯誤。 $ images/$ image typo,undefined $ useragent variable ... –

0
<?php 
      $feed = "http://feeds.bbci.co.uk/news/uk/rss.xml"; 
      // Use cURL to fetch text 
      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_URL, $feed); 
      curl_setopt($ch, CURLOPT_HEADER, 0); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      $rss = curl_exec($ch); 
      curl_close($ch); 
      // Manipulate string into object 
      $rss = simplexml_load_string($rss); 
      $siteTitle = $rss->channel->title; 
      $cnt = count($rss->channel->item); 
      for($i=0; $i<14; $i++){ 
       $url = $rss->channel->item[$i]->link; 
       $title = $rss->channel->item[$i]->title; 
    $image = $rss->channel->item[$i]->children('media', true)->thumbnail->attributes()->url; 

    echo $image; 
    echo "<br>"; 
    echo $title; 
} ?>