1

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="" />

1
  • Try $myimg = (string) $PRODUCT->link->attributes()->href; Commented Jul 7, 2014 at 1:10

3 Answers 3

2

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]
   );
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes but how can echo the results as single? For example how can i echo only each image url?
@IreneT.: I have made the choice to put all in a result array to be more clean and because its more handy to reuse the results later, example: echo $results[2]['img']; displays the image url from the third entry. But if you don't need a result array at all and you only want to display the image url, replace $results[].....); with echo (string)$entry->xpath('e:link[@type="image/jpeg"]/@href')[0][0];
1

If that's the exact XML, actually there is no need for a foreach. Try this:

$xml = simplexml_load_file($url);

$my_title = (string) $xml->title;
$myimg = (string) $xml->link->attributes()['href']; // 5.4 or above
echo $myimg; // http://example.com/photos/f_0px_30px/image687.jpg

Comments

0

Try:

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.