Solved

Grabbing PHP variable

Posted on 2016-09-08
4
60 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 109

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 109

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php mysql check email already in database. (second check) 7 64
Cpanel file manager 8 39
Is there a simpler dropbox system? 10 34
parse url to form? 7 25
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

789 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