Show Twitter feed in Webpage
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>"; ?>