I have a php function that fetches and returns tweets data from twitter as simplexml object.I could get its contents by using php syntax. Here is php function
<?php
function searchResults($q) {
$host = "http://search.twitter.com/search.atom?q=" . urlencode( $q ) . "&rpp=100";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//Raw xml
$result = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($result);
return $xml;
}
?>
If I call it like
$xml = searchResults('xyz');
I could fetch its contents like
echo $xml->content.''.$xml->author->name;
Now I need to return it from php function in JSON format. Like
return json_encode($xml);
in spite of
return $xml;
So how do now I get same ‘content’ and ‘author->name’ etc contents from it in JSON format when I decode json.
It depends. If you’re accessing it back from PHP, there are two ways:
or
If you are accessing it using JavaScript, it should be: