rss feed with images using php

大憨熊 提交于 2019-12-02 11:52:52

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