-1

why is this wrong?

[enclosure] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [url] => http://www.thestar.com.my/~/media/Images/TSOL/Photos-Gallery/features/2014/07/02/dominiclau020714.ashx?crop=1&w=460&h=345&
                    [length] => 
                    [type] => image/jpeg
                )

        )

I want to get the url to get the image file

I wrote print_r($eachItem->enclosure['@attributes']->url) it doesn't work. Why?

user1978142
  • 7,946
  • 3
  • 17
  • 20
user3522729
  • 55
  • 1
  • 4
  • 1
    What if you tried `$eachItem->enclosure->{'@attributes'}['url']` or `$eachItem->enclosure->['@attributes']['url']` as `@attributes` is an `Array` not an object? – Darren Jul 02 '14 at 05:17
  • 1
    the proper way of getting the attributes is by using the `->attributes()` method – user1978142 Jul 02 '14 at 05:41

2 Answers2

1

That is not the correct way of getting the attribute value. Use ->attributes() method:

echo (string) $eachItem->enclosure->attributes()['url'];
// as of PHP 5.4 (dereferencing)

Or

// PHP 5.3 below
$eachItem_attribute = $eachItem->enclosure->attributes();
echo (string) $eachItem_attribute['url'];
user1978142
  • 7,946
  • 3
  • 17
  • 20
  • +1 This is what you should be doing OP, I overlooked the SimpleXML. – Darren Jul 02 '14 at 05:54
  • it became this SimpleXMLElement Object ( [0] => http://www.thestar.com.my/~/media/Images/TSOL/Photos-Gallery/features/2014/07/02/JamyYangMingjie020714.ashx?crop=1&w=460&h=345& ) SimpleXMLElement Object ( [0] => http://www.thestar.com.my/~/media/Images/TSOL/Photos-Gallery/features/2014/07/02/dominiclau020714.ashx?crop=1&w=460&h=345& ) – user3522729 Jul 02 '14 at 06:05
  • @user3522729 add a typecast string `(string)`, check the revision – user1978142 Jul 02 '14 at 06:06
  • Or, use the array dereference on `->enclosure` itself. – Ja͢ck Jul 02 '14 at 06:36
  • @Jack yeah probably use that one, the OP didn't post the whole structure so that it'l be easier to analyze. you'll see the OP posted a bunch (spawned) new questions regarding this issue. with using other accounts. – user1978142 Jul 02 '14 at 06:40
0

Right & Quick Format

$eachItem->enclosure->attributes()->{'url'};
Bora
  • 10,529
  • 5
  • 43
  • 73