[英]rss feed with images using php
我正在尝试通过以下RSS提要的RSS提要http://menmedia.co.uk/manchestereveningnews/news/rss.xml
我可以使用此方法毫无问题地解决这个问题:
<?
$xml = file_get_contents('http://menmedia.co.uk/manchestereveningnews/news/rss.xml');
// Use cURL to get the RSS feed into a PHP string variable.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
'http://menmedia.co.uk/manchestereveningnews/news/rss.xml');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
curl_close($ch);
// Include the handy XML data extraction functions.
include 'xml_regex.php';
// An RSS 2.0 feed must have a channel title, and it will
// come before the news items. So it's safe to grab the
// first title element and assume that it's the channel
// title.
$channel_title = value_in('title', $xml);
// An RSS 2.0 feed must also have a link element that
// points to the site that the feed came from.
$channel_link = value_in('link', $xml);
// Create an array of item elements from the XML feed.
$news_items = element_set('item', $xml);
foreach($news_items as $item) {
$title = value_in('title', $item);
$url = value_in('link', $item);
$description = value_in('description', $item);
$timestamp = strtotime(value_in('pubDate', $item));
$item_array[] = array(
'title' => $title,
'url' => $url,
'description' => $description,
'timestamp' => $timestamp
);
}
if (sizeof($item_array) > 0) {
// First create a div element as a container for the whole
// thing. This makes CSS styling easier.
$html = '';
// Markup the title of the channel as a hyperlink.
$html .= '';
// Now iterate through the data array, building HTML for
// each news item.
$count = 0;
echo "";
foreach ($item_array as $item) {
$html .= '<a href="'.make_safe($item['url']).'" target="_blank">
<img src="'.$item['enclosure'].'">
'.substr("".$item['title']."", 0, 80).'
</div></a>';
echo '';
// Limit the output to five news items.
if (++$count == 1) {
break;
}
}
$html .= '';
echo $html;
}
function make_safe($string) {
$string = preg_replace('#<!\[CDATA\[.*?\]\]>#s', '', $string);
$string = strip_tags($string);
// The next line requires PHP 5, unfortunately.
//$string = htmlentities($string, ENT_NOQUOTES, 'UTF-8', false);
// Instead, use this set of replacements in PHP 4.
$string = str_replace('<', '<', $string);
$string = str_replace('>', '>', $string);
$string = str_replace('(', '(', $string);
$string = str_replace(')', ')', $string);
return $string;
}
?>
但是,我试图使图像也可以通过rss feed上的附件标签中的图像。
在我正在使用的分钟:
<img src="'.$item['enclosure'].'">
这是行不通的。
任何想法将不胜感激!
谢谢
据我所见,附件是一个仅包含属性的开闭标签。
<enclosure length="1280" url="http://m.gmgrd.co.uk/res/108.$plit/C_71_article_1469226_short_teaser_group_short_teaser_image.jpg" type="image/jpeg" />
这意味着,您不能像使用guid
或title
那样仅访问其值,而是必须访问attribute 。
当前,您甚至都没有设置稍后要访问的索引:
$item_array[] = array(
'title' => $title,
'url' => $url,
'description' => $description,
'timestamp' => $timestamp
// Here enclosure is missing
);
我不知道您的XML类,但是您需要找出,是否可以在使用element_set之后访问元素属性 。 或者是否存在另一种访问属性的方法。
一旦知道了URL,就可以从该URL抓取图像,并在自己的服务器上创建一个副本。 但是,这两种选择都会导致不同的问题:
根据您走的路,您要么只是打电话给
// $attribute is the url-attribute of the enclosure-tag
<img src="'.$attribute.'">
或将图像复制到您自己的服务器,然后调用
<img src="'.$urlToImageOnYourServer.'">
如果您使用的是bobulous.org.uk中的函数,并且已经包含第3部分 ,则可以像这样编辑foreach循环以获取附件URL:
foreach($news_items as $item) {
$title = value_in('title', $item);
$url = value_in('link', $item);
$description = value_in('description', $item);
$timestamp = strtotime(value_in('pubDate', $item));
$imageAttrs = attributes_in('enclosure', $item));
$imageUrl = $imageAttrs['url'];
$item_array[] = array(
'title' => $title,
'url' => $url,
'description' => $description,
'timestamp' => $timestamp,
'enclosure' => $imageUrl,
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.