PHP Twitter API Can't grab georss:point and store as variable?
I am new to API's like today new. I have learned how to simply call the twitter api xml data and pull certain things off from a post. My problem though is I can't seem to figure out how to pull off the Geo or Coordinates in the Twitter API and display it or store it as a variable.
<html>
<?php
$xmldata = 'http://twitter.com/statuses/user_timeline/allencoded.xml';
$open = fopen($xmldata, 'r');
$content = stream_get_contents($open);
fclose($open);
$xml = new SimpleXMLElement($content);
?>
<?php
$geo = $xml->status[0]->coordinates->georss:point; //<---PROBLEM POINT!!!!
?>
<body>
<table>
<tr>
<td><img src="<? echo $xml->status[0]->user->profile_image_url; ?>" /></td>
<td>
<? echo $xml->status[0]->text; ?> at <? echo $xml->status[0]->created_at; ?> by <? echo $xml->status[0]->user->name; ?>
</td>
<td>
GEO IS AT:<? echo $geo; ?>
</td>
</tr>
</table>
</body>
</html>
When I run code I get: Parse error: syntax error, unexpected ':' in C:\inetpub\vhosts\allencoded.com\httpdocs\twitter.php on line 11
When I remove the开发者_开发知识库 code for line 11 everything works though.
When dealing with namespaced elements (like namespace_name:element_name
), you need to tell SimpleXML to load the elements of that namespace (default is to load elements without a namespace). For that, you use SimpleXMLElement::children()
(docs).
$xml = simplexml_load_file('http://twitter.com/statuses/user_timeline/allencoded.xml');
$geo = (string) $xml->status->geo->children('georss', true)->point;
echo $geo;
The first chunk of the key line there $xml->status->geo
gets the first geo
element of the first status element. Then we ask for the children that belong to the georss
namespace (so, any georss:…
elements), followed by specifying that we want the (first, only) point
.
I don't think PHP parsed the XML verbatim. While georss:point
is a valid node in the XML tree, that syntax means something else to PHP. Try doing a print_r()
on the $xml->status[0]->coordinates
value and see what it says the properties are.
精彩评论