rss feed with images using php

后端 未结 1 1784
猫巷女王i
猫巷女王i 2021-01-26 15:44

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

相关标签:
1条回答
  • 2021-01-26 16:25

    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,
        );
    }
    
    0 讨论(0)
提交回复
热议问题