使用curl方法从RSS提要获取图像路径

时间:2014-12-25 07:25:40

标签: php curl rss

Hello Sir我正在尝试使用curl方法从RSS Feed中获取图像路径...我尝试了这段代码,但它不起作用...请帮助!!!!

<?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>";
}

?> 

2 个答案:

答案 0 :(得分:0)

http://feeds.bbci.co.uk/news/uk/rss.xml 的源代码中,我没有在频道节点中看到缩略图子节目。

如果您想获得频道图片,请尝试

$rss->channel->image->url

如果您是rss的初学者并且想要解析更多站点,请尝试使用SimplePie库。非常容易使用并实现更多原子/ rss协议。

答案 1 :(得分: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;
} ?>