Wednesday, September 4, 2013

Read Tweets with PHP using Twitter API

0

Twitter returns information in several formats (such as json, XML), Twitter information which is return in json format we will work today to read the latest status(tweets) update of user.


The url which help us to read the latest tweets(status) is something like in this format:

https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=User_name&count=number_of_tweets

Below are full working code which you can use and read the status(tweets) of specific user.

<?php
require_once('twitteroauth/twitteroauth.php'); //Path to twitteroauth library
$twitteruser = "screen name";
$nooftweets= 5;
$consumerkey = "consumer key";
$consumersecret = "consumer secret";
$accesstoken = "access token";
$accesstokensecret = "access token secret";

function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
  $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
  return $connection;
}
  
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
 
$data = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$nooftweets);
echo '<pre>';
print_r($data);

?>

How to read:


for($i=0; $i<=$nooftweets;$i++){
echo '
';
echo $data[$i]->text;
echo "
";
}

View Demo Download

No comments :

Post a Comment