RSS饲料用PHP
我想拉一个RSS以下RSS订阅 http://menmedia.co.uk/manchestereveningnews/news/rss.xmlRSS饲料用PHP
穿心,我可以用这个方法拉这个通过没有问题图片:
<?
$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" />
这意味着,你不能访问它的值像你或title
做,但你必须访问属性。
目前你甚至不设置你想稍后访问索引:
$item_array[] = array(
'title' => $title,
'url' => $url,
'description' => $description,
'timestamp' => $timestamp
// Here enclosure is missing
);
我不知道你的XML类,但你需要了解,如果你能访问元素属性后不知何故,使用element_set。或者如果有另一种方法来访问属性。
只要知道该URL,就可以掌握该URL的图像并在自己的服务器上创建副本。然而,这两个选项将导致不同的问题:
- 如果您在服务器上创建一个自己的副本,如果你深层连结到你违反了针对HTML开发常识的网址,您可能会违反防止对版权
- ,因为deeplinking到的图像是见过的邪恶(可能显示在您的网站也违背著作权的图像,我不知道有国际法)
依赖于你会往哪个方向走,你要么就叫
// $attribute is the url-attribute of the enclosure-tag
<img src="'.$attribute.'">
或图像复制到自己的服务器,然后调用
<img src="'.$urlToImageOnYourServer.'">
如果您使用的是functions from bobulous.org.uk,它包括part 3已经,您可以编辑您的foreach循环这样让机箱网址:
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,
);
}
感谢这一点,不完全确定,因为它不是我的编码。我将如何在数组中调用? $ attribute => value_in(enclosure ['url'])....类似的东西? – meohmy 2011-12-29 12:41:11
使用Google代码搜索您正在使用的代码,并找到方法attributes_in。见上面的编辑。 – Aufziehvogel 2011-12-29 12:53:16
感谢这一点,但它不通过,将有一点点在谷歌! – meohmy 2011-12-29 13:07:18
你可以print_r $ item看看它有什么? – 2011-12-29 10:19:30
这不会与我使用的编码 – meohmy 2011-12-29 10:40:37
为什么如果我可以问@meohmy?在foreach内部,只需编写print_r($ item),就可以得到$ item元素的结构。 – 2011-12-29 11:11:31