I’m using the following code to parse the latest tweets from a twitter account:
$host = "http://search.twitter.com";
$filename = "/search.json";
$opts = array('http' => array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n"
));
$context = stream_context_create($opts);
$search = "mashable";
$search = str_replace(" ", "%20", $search);
$count = "10";
$a = "$host$filename?q=$search&rpp=$count&include_entities=true";
echo "$a\n";
$json = file_get_contents($a, false, $context);
$obj = json_decode($json, true);
$id = $obj['results'][0]['id'];
$tweet = $obj['results'][0]['text'];
$user = $obj['results'][0]['from_user'];
$to_user = $obj['results'][0]['to_user'];
$media_url = $obj['results'][0]['media_url'];
#echo $json;
echo "<br /><br />";
echo "searching for $search\n tweet count: $count\n";
echo "<br /><br /><b>tweets</b><br />";
echo "tweet_id: $id <br />";
echo "user: $user <br />";
echo "Tweet: $tweet<br />";
echo "to_user: $to_user <br />";
echo "media: $media_url";
echo ""
?>
I would like to extract the following values:
– username (sender)
– tweet (text)
– to user (if is reply)
– media attachments (pictures)
The code is working but for some reason I only receive the latest tweet instead of the number ($count) value. I also can’t receive the media_url value of a tweet. My question is: how?
I found the solution to receive the media_url parameter of each tweet: