2

Question: How to Parse <media:content URL="IMG" /> from XML?

OK. This is like asking why 1+1 = 2. And 2+2=Not Available.

Orginal Link: How to Parse XML With SimpleXML and PHP // By: John Morris. https://www.youtube.com/watch?v=_1F1Iq1IIS8

Using his method I can easily reach items on RSS FEED New York Times With Following Code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>How to Parse XML with SimpleXML and PHP</title>
</head>
<body>
<?php
$url = 'http://rss.nytimes.com/services/xml/rss/nyt/Sports.xml';
$xml = simplexml_load_file($url) or die("Can't connect to URL");

?><pre><?php //print_r($xml); ?></pre><?php

foreach ($xml->channel->item as $item) {
    printf('<li><a href="%s">%s</a></li>', $item->link, $item->title);
}
?>  
</body>
</html>

GIVES:
Sparky Lyle in Monument Park? Fans Say Yes, but He Disagrees
The Thickly Accented American Behind the N.B.A. in France
On Pro Basketball: ‘That Got Ugly in a Hurry’: More Playoff Pain Delivered by the Spurs
...

BUT

TO reach media:content you cannot use simplexml_load_file as it doesn't grab any media.content tags.

So... Yes.. I searched around on the Webb. I found this example on StackOverflow:
get media:description and media:content url from xml

But using the Code:

<?php
function feeds() 
{
    $url = "http://rss.nytimes.com/services/xml/rss/nyt/Sports.xml"; // xmld.xml contains above data
    $feeds = file_get_contents($url);
    $rss = simplexml_load_string($feeds);
    foreach($rss->channel->item as $entry) {
         if($entry->children('media', true)->content->attributes()) {
                $md = $entry->children('media', true)->content->attributes();
                print_r("$md->url");
            }
    }
}
?>

Gave me no errors. But also a blank page.

And it seems most people (googling) has little to no idea how to really use media:content . So I have to turn for Stackoverflow and hope someone can provide an answer. Im even willing to not use SimpleXML.

What I want.. is .. to grab media:content url IMAGES and use them on a external site.

Also.. if possible.
I would like to put the XML parsed items into a SQL database.

1 Answer 1

1

I came up with this:

<?php
$url = "http://rss.nytimes.com/services/xml/rss/nyt/Sports.xml"; // xmld.xml contains above data
$feeds = file_get_contents($url);
$rss = simplexml_load_string($feeds);

$items = [];

foreach($rss->channel->item as $entry) {
    $image = '';
    $image = 'N/A';
    $description = 'N/A';
    foreach ($entry->children('media', true) as $k => $v) {
        $attributes = $v->attributes();

        if ($k == 'content') {
            if (property_exists($attributes, 'url')) {
                $image = $attributes->url;
            }
        }
        if ($k == 'description') {
            $description = $v;
        }
    }

    $items[] = [
        'link' => $entry->link,
        'title' => $entry->title,
        'image' => $image,
        'description' => $description,
    ];
}

print_r($items);
?>

Giving:

Array
(
    [0] => Array
        (
            [link] => SimpleXMLElement Object
                (
                    [0] => https://www.nytimes.com/2017/04/17/sports/basketball/a-court-used-for-playing-hoops-since-1893-where-paris.html?partner=rss&emc=rss
                )

            [title] => SimpleXMLElement Object
                (
                    [0] => A Court Used for Playing Hoops Since 1893. Where? Paris.
                )

            [image] => SimpleXMLElement Object
                (
                    [0] => https://static01.nyt.com/images/2017/04/05/sports/basketball/05oldcourt10/05oldcourt10-moth-v13.jpg
                )

            [description] => SimpleXMLElement Object
                (
                    [0] => The Y.M.C.A. in Paris says its basketball court, with its herringbone pattern and loose slats, is the oldest one in the world. It has been continuously functional since the building opened in 1893.
                )

        )
.....

And you can iterate over

foreach ($items as $item) {
    printf('<img src="%s">', $item['image']);
    printf('<a href="%s">%s</a>', $item['url'], $item['title']);
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks sorry I got upset. I really looked all over the web for a solution. Yours works beautifully. Just 'changed' printf('<a href="%s">%s</a>', $item['url'], $item['title']); to ` `printf('<a href="%s">%s</a>', $item['link'], $item['title']);``
how could I reach ... <media:description> Sparky Lyle, who won two World Series championships with the Yankees, signing autographs at the Somerset Patriots’ Fan Fest. as well ? rss.nytimes.com/services/xml/rss/nyt/Sports.xml
IM here now @alistaircol : stackoverflow.com/questions/43451098/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.