Fetching posts from Twitter with PHP

Fetching posts from Twitter with PHP


Today I had the task to fetch the latest posts from Twitter. It turns out this is not as easy as it sounds, but I found a solution that works pretty well.

First of all you need to sign up to twitter and create your twitter app at http://dev.twitter.com

Next you have to copy your consumer key/secret and access token/secret from apps OAuth tool tab.

Now its time to download the two files you find on the following link:
https://github.com/abraham/twitteroauth/tree/master/twitteroauth

Finally, create a php file and put this code in it that I blatantly copied just as 99% of the rest of this post.

 <?php 
session_start();
require_once("twitteroauth/twitteroauth/twitteroauth.php"); //Path to twitteroauth library you downloaded in step 3
$twitteruser = "twitterusername"; //user name you want to reference
$notweets = 30; //how many tweets you want to retrieve
$consumerkey = "12345"; //Noted keys from step 2
$consumersecret = "123456789"; //Noted keys from step 2
$accesstoken = "123456789"; //Noted keys from step 2
$accesstokensecret = "12345"; //Noted keys from step 2
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);
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
echo json_encode($tweets);
echo $tweets; //testing remove for production
?>

Modify as needed and have fun :-)

Source: http://stackoverflow.com/questions/17049821/setting-up-twitter-api-getting-the-last-few-tweets

download file now

Unknown

About Unknown

Author Description here.. Nulla sagittis convallis. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.

Subscribe to this Blog via Email :