Solved

Grabbing PHP variable

Posted on 2016-09-08
4
39 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 108

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 108

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses four methods for overlaying images in a container on a web page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now