Though Twitter has lunched API Ver1.1, most
of the application developed in older version API will not work, You
need to migrate your application to Ver1.1 and which requires
Authentication on all endpoints and support JSON only. Lets discuss
how to display User feeds from Twitter in you website.
The first thing you need to create an application in twitter and get
the following
- oauth access token
- oauth access token secret
- consumer key
- consumer secret
This you can create in
dev.twitter.com, by using your twitter user
name and password.
Then you need TwitterAPIExchange.php class file which you can
download
from here.
Now copy paste the following code to get user Twitter Feeds.
<?php
include("TwitterAPIExchange.php");
$settings = array(
'oauth_access_token' => "Enter Your Access Token",
'oauth_access_token_secret' => "Enter Your Token Secret",
'consumer_key' => "Enter Your Consumer Key",
'consumer_secret' => "Enter Your Consumer Secret"
);
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=a2zwebhelp';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
/* Here you will get all info from user timeline */
var_dump(json_decode($response));
?>
Copy/ paste the following code to display user feeds
<?php
$valid_data = json_decode($response); //JSON data to PHP.
print "<ul>";
foreach ($valid_data as $key=>$value) {
print "<li>";
print $value->text;
print "</li>";
}
print "</ul>";
?>
Click here to generate App ID/API Key for Twitter.
Download Code Files
Some other options from Twitter You may need:
Search global tweets for a hashtag
<?php
include("TwitterAPIExchange.php");
$settings = array(
'oauth_access_token' => "Enter Your Access Token",
'oauth_access_token_secret' => "Enter Your Token Secret",
'consumer_key' => "Enter Your Consumer Key",
'consumer_secret' => "Enter Your Consumer Secret"
);
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q=#a2zwebhelp';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
var_dump(json_decode($response));
?>
Searching using a geocode
<?php
include("TwitterAPIExchange.php");
$settings = array(
'oauth_access_token' => "Enter Your Access Token",
'oauth_access_token_secret' => "Enter Your Token Secret",
'consumer_key' => "Enter Your Consumer Key",
'consumer_secret' => "Enter Your Consumer Secret"
);
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfield = '?q=test&geocode= 20.2386889,85.7820416,10mi&count=100";';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
var_dump(json_decode($response));
?>