Search code examples
phpxmlwordpresssimplexmlfeed

Wordpress Feeds RSS XML - PHP media


I have this code but I don't know how I get the media

Please someone could help me?

I need show the image of post on my website

Below my PHP and XML for Wordpress Feeds

PHP

$html = "";
$url = "https://intercambioemgalway.com.br/feed/";
$xml = simplexml_load_file( $url, 'SimpleXMLElement', LIBXML_NOCDATA );

for ($i = 0; $i < 5; $i++){
    $img = $xml->channel->item->media;
    $html .= "$img";
}
echo $html;

XML

<item>
<title>Qual escola vou estudar?</title>
<link>https://intercambioemgalway.com.br/2017/09/11/qual-escola-estudar/</link>
<comments>https://intercambioemgalway.com.br/2017/09/11/qual-escola-estudar/#respond</comments>
<pubDate>Mon, 11 Sep 2017 17:36:59 +0000</pubDate>
<dc:creator><![CDATA[admin]]></dc:creator>
<category><![CDATA[Meu intercâmbio]]></category>

<guid isPermaLink="false">https://intercambioemgalway.com.br/?p=179</guid>
<description><![CDATA[<p>Hoje vou contar um pouquinho da escola que vou estudar em Galway, a Atlantic Language.</p>]]></description>
<wfw:commentRss>https://intercambioemgalway.com.br/2017/09/11/qual-escola-estudar/feed/</wfw:commentRss>
<slash:comments>0</slash:comments>
<media:content url="https://intercambioemgalway.com.br/wp-content/uploads/2017/09/Untitled-1.jpg" type="image/jpeg" medium="image" width="900" height="600">
    <media:title type="plain">
        <![CDATA[atlantic-language-galway]]>
    </media:title>
    <media:thumbnail url="https://intercambioemgalway.com.br/wp-content/uploads/2017/09/Untitled-1-150x150.jpg" width="150" height="150" />
    <media:description type="plain">
        <![CDATA[]]>
    </media:description>
    <media:copyright>
        admin
    </media:copyright>
</media:content>


Solution

  • Each $item is of type SimpleXMLElement.

    Now you can loop the children using the namespace http://search.yahoo.com/mrss/ and then you can access the attributes.

    For example:

    foreach($xml->channel->item as $item) {
        foreach($item->children("http://search.yahoo.com/mrss/") as $media) {
            $img = (string)$media->attributes()->url;
        }
    }