Solved

Grabbing PHP variable

Posted on 2016-09-08
4
66 Views
Last Modified: 2016-09-10
Having trouble grabbing the URL field from this string in php:

object(SimpleXMLElement)#11 (1) { ["@attributes"]=> array(2) { ["url"]=> string(46) "http://pbs.twimg.com/media/Cr2xto1UkAA56xV.jpg" ["type"]=> string(10) "image/jpeg" } }

Open in new window


Tried

$e->@attributes->url
$e['@attributes']['url']

Neither are working for me
0
Comment
Question by:N R
  • 2
  • 2
4 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41790317
Please use var_export() to print the object and post the output in the code snippet, thanks.

Also, please see this variant on your other question:
https://iconoun.com/demo/temp_nr2.php
<?php // demo/temp_nr2.php
/**
 * https://www.experts-exchange.com/questions/28968624/Grabbing-PHP-variable.html
 *
 * https://www.experts-exchange.com/questions/28968617/RSS-Feed-Enclosure-URL.html
 */
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
echo '<pre>';


// TEST DATA FROM THE POST AT E-E
$xml = <<<EOX
<rss version="2.0">
<channel>
<title>
<![CDATA[ Queryfeed | #golf,from:nrileykc ]]>
</title>
<ttl>10</ttl>
<description>
Queryfeed searches in Twitter, Google+ and gives you a nice RSS. Enjoy!
</description>
<generator>queryfeed.net</generator>
<webmaster>admin@queryfeed.net</webmaster>
<item>
<link>
http://twitter.com/twitter/status/773970114764869632
</link>
<guid>
http://twitter.com/twitter/status/773970114764869632
</guid>
<title>
<![CDATA[ Nathan Riley @nrileykc ]]>
</title>
<author>nrileykc</author>
<enclosure url="http://pbs.twimg.com/media/Cr2xto1UkAA56xV.jpg" type="image/jpeg"/>
<category>golf</category>
<pubDate>Thu, 08 Sep 2016 19:43:49 +0000</pubDate>
<description>
<![CDATA[
<a href="https://twitter.com/hashtag/golf">#golf</a> test https://t.co/7SBUt9cd0A
]]>
</description>
</item>
<item>
<link>
http://twitter.com/twitter/status/773954372262699008
</link>
<guid>
http://twitter.com/twitter/status/773954372262699008
</guid>
<title>
<![CDATA[ Nathan Riley @nrileykc ]]>
</title>
<author>nrileykc</author>
<category>golf</category>
<pubDate>Thu, 08 Sep 2016 18:41:16 +0000</pubDate>
<description>
<![CDATA[
test <a href="https://twitter.com/hashtag/golf">#golf</a>
]]>
</description>
</item>
</channel>
</rss>
EOX;


// MAKE AN OBJECT AND FIND THE URL IN THE ENCLOSURE TAG
$obj = SimpleXML_Load_String($xml, 'SimpleXMLElement', LIBXML_NOCDATA);

// object(SimpleXMLElement)#11 (1) { ["@attributes"]=> array(2) { ["url"]=> string(46) "http://pbs.twimg.com/media/Cr2xto1UkAA56xV.jpg" ["type"]=> string(10) "image/jpeg" } }
$enc = $obj->channel->item[0]->enclosure;

$url = (string)$enc->attributes()->url;
var_dump($url);

Open in new window

0
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 41790886
This will work:

$e->attributes()->url;

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41790898
See line 75 in the prior code snippet.  It might  be necessary to cast the variable as a string, depending on what you want to do with the URL.
0
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 41791205
@N R:
I encourage you to read more about using SimpleXMLElement class:
http://php.net/manual/en/class.simplexmlelement.php
1

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question