Search code examples
phpxmlsimplexml

Parsing xml with simplexml_load


I am trying to parse an xml but I get a problem while I am trying to fetch image url.

My xml is:

<entry>
<title>The Title</title>
<id>http://example.com/post/367327.html</id>
<summary>Some extra text</summary>
<link rel="enclosure" href="http://example.com/photos/f_0px_30px/image687.jpg" type="image/jpeg"  length="" /> 
</entry>

So far I am using the code below to fetch the other data:

$url = "http://msdssite.com/feeds/xml/myxml.xml";
$xml = simplexml_load_file($url);

foreach($xml->entry as $PRODUCT)
{
$my_title = trim($PRODUCT->title);
$url = trim($PRODUCT->id);
$myimg = $PRODUCT->link;
}

How can I parse the href from this: <link rel="enclosure" href="http://example.com/photos/f_0px_30px/image687.jpg" type="image/jpeg" length="" />


Solution

  • Since it seems that your entries can contain several link tags, you need to check that the type attribute has the value image/jpeg to be sure to obtain a link to an image:

    ini_set("display_errors", "On");
    
    $feedURL = 'http://OLDpost.gr/feeds/xml/category-takhs-xatzhs.xml';
    
    $feed = simplexml_load_file($feedURL);
    
    $results = array();
    
    foreach($feed->entry as $entry) {
        $result = array('title' => (string)$entry->title,
                        'url'   => (string)$entry->id);
    
        $links = $entry->link;
        foreach ($links as $link) {
            $linkAttr = $link->attributes();
            if (isset($linkAttr['type']) && $linkAttr['type']=='image/jpeg') {
                $result['img'] = (string)$linkAttr['href'];
                break;
            }
        }
        $results[] = $result;
    }
    
    print_r($results);
    

    Note that using simplexml like that (the foreach loop to find the good link tag) isn't very handy. It's better to use an XPath query:

    foreach($feed->entry as $entry) {
       $entry->registerXPathNamespace('e', 'http://www.w3.org/2005/Atom');
       $results[] = array(
         'title' => (string)$entry->title,
         'url'   => (string)$entry->id,
         'img'   => (string)$entry->xpath('e:link[@type="image/jpeg"]/@href')[0][0]
       );
    }