rss使用php提供图像

时间:2011-12-29 10:11:55

标签: php rss

我正在尝试为以下RSS Feed提取rss Feed 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 Feed中的enclosure标签中的图像。

我正在使用的那一刻:

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

这不起作用。

任何想法都会非常感激!

感谢

1 个答案:

答案 0 :(得分: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. 如果您在HTML开发中深入链接违反常识的网址,因为图片深层链接被视为邪恶(可能在您的网站上显示图片也违反版权,我不知道国际法)
  3. 根据您的去向,您只需致电

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

    或将图像复制到您自己的服务器,然后调用

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

    如果您使用的是functions from bobulous.org.uk且已包含part 3,则可以像这样编辑foreach-loop以获取附件网址:

    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,
        );
    }