【问题标题】:rss feed with images using php使用 php 的带有图像的 RSS 提要
【发布时间】:2011-12-29 10:11:54
【问题描述】:

我正在尝试为以下 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('<', '&lt;', $string);
    $string = str_replace('>', '&gt;', $string);
    $string = str_replace('(', '&#40;', $string);
    $string = str_replace(')', '&#41;', $string);
    return $string;
}


?>

但是,我试图让图片也可以通过 rss 提要的附件标签中的图片。

在我使用的那一刻:

<img src="'.$item['enclosure'].'">

这行不通。

任何想法都将不胜感激!

谢谢

【问题讨论】:

  • 你能打印_r $item 看看它有什么吗?
  • 这不适用于我正在使用的编码
  • 如果我可以问@meohmy 为什么?在 foreach 中只需编写 print_r($item) 即可获得 $item 元素的结构。

标签: php rss


【解决方案1】:

在我看来,外壳是一个只包含属性的开闭标签。

<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" />

这意味着,您不能像使用guidtitle 那样仅访问其值,而是必须访问属性

目前您甚至没有设置稍后尝试访问的索引:

$item_array[] = array(
    'title' => $title,
    'url' => $url,
    'description' => $description,
    'timestamp' => $timestamp
    // Here enclosure is missing
);

我不知道你的 XML 类,但你需要知道,在使用 element_set 之后,你是否可以访问 元素属性,不知何故。或者如果有其他方法可以访问属性。

一旦您知道 URL,您就可以从该 URL 获取图像并在您自己的服务器上创建一个副本。然而,这两种选择都会导致不同的问题:

  1. 如果您在服务器上创建自己的副本,您可能会侵犯版权
  2. 如果您对 URL 的深层链接违反了 HTML 开发中的常识,因为对图像的深层链接被视为邪恶(可能在您的网站上显示图像也违反了版权,我不知道那里的国际法)

取决于你会走哪条路,你要么打电话给

// $attribute is the url-attribute of the enclosure-tag
<img src="'.$attribute.'">

或者把图片复制到自己的服务器上然后调用

<img src="'.$urlToImageOnYourServer.'">

如果您正在使用 functions from bobulous.org.uk 并且它已经包含 part 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,
    );
}

【讨论】:

  • 谢谢你,不完全确定,因为它不是我的编码。我将如何调用数组? $attribute => value_in(enclosure['url']).... 类似的东西?
  • 用谷歌搜索你正在使用的代码并找到方法 attributes_in。请参阅上面的编辑。
  • 谢谢你,但它没有成功,将在谷歌上看看!
  • 好的,成功了!!!!!!改为:$attribute_array = element_attributes('enclosure', $xml); foreach($news_items as $item) { $title = value_in('title', $item); $url = value_in('链接', $item); $description = value_in('description', $item); $timestamp = strtotime(value_in('pubDate', $item)); $encurl = $attribute_array['url']; $item_array[] = array( 'title' => $title, 'url' => $url, 'description' => $description, 'timestamp' => $timestamp, 'enclosure' => $encurl, ); }
  • element_attributes('enclosure',$xml) 是做什么的?我无法在您的代码中看到该功能。我尝试了您的代码,但在 Web 服务器错误日志中出现“调用未定义函数 element_attributes()”错误。
猜你喜欢
  • 2020-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-03
  • 2019-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多