使用php的带有图像的rss提要
我正在尝试为下面的rss提要拉一个rss提要 使用此方法,我可以毫无问题地解决此问题:使用php的带有图像的rss提要,php,rss,Php,Rss,我正在尝试为下面的rss提要拉一个rss提要 使用此方法,我可以毫无问题地解决此问题: <? $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,
<?
$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;
}
?>
$title,
“url”=>$url,
'description'=>$description,
'timestamp'=>$timestamp
);
}
if(sizeof($item_数组)>0){
//首先创建一个div元素作为整个容器
//这使得CSS样式更容易。
$html='';
//将频道标题标记为超链接。
$html.='';
//现在遍历数据数组,为
//每一条新闻。
$count=0;
回声“;
foreach($item\u数组作为$item){
$html.='';
回声';
//将输出限制为五条新闻。
如果(++$count==1){
打破
}
}
$html.='';
echo$html;
}
函数make_safe($string){
$string=preg#u replace('##s',''$string);
$string=带标签($string);
//不幸的是,下一行需要PHP5。
//$string=htmlentities($string,entnoquotes,'UTF-8',false);
//相反,在PHP4中使用此替换集。
$string=str_replace(“”,,$string);
$string=str_replace(“(”,“(;”,$string);
$string=str_replace(')'、');'、$string);
返回$string;
}
?>
然而,我试图让这些图像也能通过rss提要上的附件标签来实现
在我使用的那一刻:
<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" />
我不知道您的XML类,但您需要知道,在使用element_set之后,您是否可以访问元素属性。或者如果有其他方法访问属性
<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" />
一旦知道URL,您就可以从该URL获取图像并在自己的服务器上创建副本。但是,这两种选择都会导致不同的问题:
// $attribute is the url-attribute of the enclosure-tag
<img src="'.$attribute.'">
您可以打印\r$项目以查看它有什么吗?这与我使用的编码不兼容为什么我可以问@meohmy?在foreach中,只需编写print_r($item),就可以得到$item元素的结构。谢谢,我不完全确定,因为它不是我的代码。我如何调用数组$属性=>value_(附件['url'])。。。。类似的东西?谷歌搜索了你正在使用的代码,在中找到了方法属性。请参阅上面的编辑。谢谢你的帮助,但它并没有成功,我们将在谷歌上看一看!好的,成功了!!!!!更改为:$attribute_array=element_attributes('enclosure',$xml);foreach($news_items as$item){$title=value_in('title',$item);$url=value_in('link',$item);$description=value_in('description',$item);$timestamp=strottime(value_in('pubDate',$item));$encurl=$attribute_数组['url'];$item数组[]=array('title'=>$title,'url'=>$url,'description'=>$description,'timestamp'=>$timestamp,'enclosure'=>$encurl,);}元素属性('enclosure',$xml)是做什么的?我在代码中看不到该函数。我尝试了你的代码,但我得到了一些结果web服务器错误日志中的“调用未定义的函数元素_attributes()”错误。
<img src="'.$urlToImageOnYourServer.'">
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,
);
}