rss feed with images using php

拥有回忆 提交于 2019-12-02 16:34:51

问题


I am trying to pull an rss feed through for the following rss feed http://menmedia.co.uk/manchestereveningnews/news/rss.xml

I can pull this through no problem using this method:

<?
$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;
}


?>

However I am trying to get the images to also pull through which are in the enclosure tag on the rss feed.

At the minute i am using :

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

This isnt working.

Any ideas would be really appreciated!

thanks


回答1:


As far as I can see it, the enclosure is an open-closed tag only consisting of attributes.

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

This means, that you cannot just access its values like you do with guid or title, but you have to access the attributes.

Currently you do not even set the index you are trying to access later:

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

I do not know your XML class, but you need to find out, if you can access element attributes after using element_set, somehow. Or if there is another method to access the attributes.

As soon as you know the URL, you can grasp the image from that URL and create a copy on your own server. Both options however cause different problems:

  1. If you create an own copy on your server you might violate against copyright
  2. If you deeplink to the URL you violate against common sense in HTML development, because deeplinking to images is seen evil (possibly displaying an image on your site also goes against copyright, I do not know international law there)

Dependant on which way you’ll go, you will either just call

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

or copy the image to your own server and then call

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

If you are using the functions from bobulous.org.uk and it includes part 3 already, you could edit your foreach-loop like this to get the enclosure 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,
    );
}


来源:https://stackoverflow.com/questions/8666440/rss-feed-with-images-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!