简体   繁体   中英

How to get image from rss feed in php

My Rss

That has link to my episode image: <itunes:image href="https://d3t3ozftmdmh3i.cloudfront.net/production/podcast_uploaded_nologo/19641917/19641917-1637668693797-44b27f4c00a16.jpg"/>

<?php
 $url = "https://anchor.fm/s/75abc654/podcast/rss";

        $invalidurl = false;
        if(@simplexml_load_file($url)){
            $feeds = simplexml_load_file($url);
        }else{
            $invalidurl = true;
            echo "<h2>Invalid RSS feed URL.</h2>";
        }
        $i=0;
        if(!empty($feeds)){

            $site = $feeds->channel->title;
            $sitelink = $feeds->channel->link;

            foreach ($feeds->channel->item as $item) {

                $title = $item->title;
                $link = $item->link;
                $description = $item->description;
                $mp3 = $item->enclosure['url'];
               // $season= $item->itunes:season;
                $image = $item->itunes->image->href;
                $postDate = $item->pubDate;
                $pubDate = date('D, d M Y',strtotime($postDate));


                if($i>=5) break;
        ?>

In this code $image = $item->image->href; doesn't get the image url.. I don't know how to get the image url, please help me to get the image URL. .. Thanks in advance

You are dealing with a namespaced node within the XML / RSS document so you should use the namespace to help identify the nodes of interest. I have never used simpleXML so can only offer an alternative solution that uses conventional DOMDocument and DOMXPath

As I say I have never used SimpleXML but I notice that there is XPath support for it so you could adapt the code below

The trick here is to register the appropriate namespace so that the XPath expression can use it.

$url='https://anchor.fm/s/75abc654/podcast/rss';
$dom=new DOMDocument;
$dom->load( $url );

$xp=new DOMXPath( $dom );
$xp->registerNamespace('itunes','http://www.itunes.com/dtds/podcast-1.0.dtd');


$img=$xp->query('//channel/itunes:image');
if( $img )echo $img->item(0)->getAttribute('href');

The above will simply output

https://d3t3ozftmdmh3i.cloudfront.net/production/podcast_uploaded_nologo/19641917/19641917-1637668693797-44b27f4c00a16.jpg

To expand upon the above slightly and register other namespaces that are used in the RSS feed and then query the DOM for namespaced elements perhaps the following will help clarify how you can work with namespaces. The $tags array is simply a random selection of elements from the source file that will be used to construct the XPath expression - some of which use the dc or atom namespace and some have none.

$url='https://anchor.fm/s/75abc654/podcast/rss';

$dom=new DOMDocument;
$dom->load( $url );

$xp=new DOMXPath( $dom );
$xp->registerNamespace('dc','http://purl.org/dc/elements/1.1/');
$xp->registerNamespace('itunes','http://www.itunes.com/dtds/podcast-1.0.dtd');
$xp->registerNamespace('atom','http://www.w3.org/2005/Atom');




$tags=array('title','description','author','link','atom:link','/item/title','/item/dc:creator','/item/pubDate','/item/itunes:summary');
foreach( $tags as $tag ){
    $expr=sprintf('//%s',$tag );
    $col=$xp->query( $expr );
    if( $col && $col->length > 0 ){
        foreach( $col as $node )printf('%s -> %s<br />',$tag,trim($node->nodeValue));
    }
}

This will print:

title -> Espada Podcast
title -> Espada Podcast
title -> Discussion about Doremon | ft:Ryuk,Ryomen
title -> Espada's Trailer
description -> Our Podcast Trailer
description ->
We discussed about doremon .. Support us on Instagram


description ->
Adula oonum illa keela potrunga


author -> Espada Podcast
link -> http://espadapodcsat.ml
link -> http://espadapodcsat.ml
link -> https://anchor.fm/espada-podcast8/episodes/Discussion-about-Doremon--ftRyuk-Ryomen-e1at2fu
link -> https://anchor.fm/espada-podcast8/episodes/Espadas-Trailer-e1asvh8
atom:link ->
atom:link ->
/item/title -> Discussion about Doremon | ft:Ryuk,Ryomen
/item/title -> Espada's Trailer
/item/dc:creator -> Espada Podcast
/item/dc:creator -> Espada Podcast
/item/pubDate -> Sat, 27 Nov 2021 13:29:47 GMT
/item/pubDate -> Sat, 27 Nov 2021 10:56:21 GMT
/item/itunes:summary ->
We discussed about doremon .. Support us on Instagram


/item/itunes:summary ->
Adula oonum illa keela potrunga

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM