I am running a php website and would like to pull in the most recent tweet from my companies twitter feed. Below is the code that I have, however it is currently not working. What have I done wrong? Do I need to change any setting in Twitter to get this method to work?
$responseJson = file_get_contents('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=take_me_fishing&include_rts=1&count=1');
if ($responseJson)
{
$response = json_decode($responseJson);
$status = $response->text;
}
The value of $response
is an array of objects. Since you only retrieved the last one, you'll want to access the first item within this array. To access the "text" property of the first element, change your code as follows:
if ($responseJson) {
$response = json_decode($responseJson);
$status = $response[0]->text;
}
print $status; // The tweet.
Note that when you print_r($response)
you'll see the following structure:
Array
(
[0] => stdClass Object
(
[created_at] => Fri Jun 22 15:00:34 +0000 2012
[id] => 216183907643699200
[id_str] => 216183907643699200
[text] => Help us determine the Top 8 State Parks for boating and fishing:
http://t.co/SQEzWpru #takemefishing
... rest of the tweet object ...