下面的代码使用 PHP CURL 从 RSS 提要中提取数据,但是我似乎无法弄清楚如何从描述变量中获取图像 URL。我只需要第一张图片。
Fatal error: Call to undefined method SimpleXMLElement::description() in /home/feedolu/public_html/index.php on line 26
Function feedMe($feed) {
// 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<$cnt; $i++)
{
$url = $rss->channel->item[$i]->link;
$title = $rss->channel->item[$i]->title;
$desc = $rss->channel->item[$i]->description;
$image = $rss->channel->item[$i]->description('img', 0);
echo '<h3><a href="'.$url.'">'.$title.'</a></h3>'.$desc.'';
echo $image;
}
}
feedMe("localhost/feed/");