Home >Backend Development >PHP Tutorial >How to Retrieve Twitter User Timeline Data Using PHP and Twitter API v1.1?
Simplest PHP Example for Retrieving user_timeline with Twitter API Version 1.1
Due to the Twitter API 1.0 retirement on June 11th, 2013, the provided script in the question no longer works. However, transitioning to Twitter API version 1.1 requires an updated approach.
Steps to Retrieve user_timeline with Twitter API Version 1.1:
1. Create a Developer Account and Application:
2. Adjust Application Settings:
3. Utilize the TwitterAPIExchange PHP Class:
Download and include the TwitterAPIExchange class from GitHub.
4. Configure the API Call:
require_once('TwitterAPIExchange.php'); $settings = [ 'oauth_access_token' => 'YOUR_OAUTH_ACCESS_TOKEN', 'oauth_access_token_secret' => 'YOUR_OAUTH_ACCESS_TOKEN_SECRET', 'consumer_key' => 'YOUR_CONSUMER_KEY', 'consumer_secret' => 'YOUR_CONSUMER_SECRET', ]; $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json'; $requestMethod = 'GET'; $getfield = '?screen_name=YOUR_SCREEN_NAME&count=10';
5. Perform the API Request:
$twitter = new TwitterAPIExchange($settings); $result = $twitter->setGetfield($getfield) ->buildOauth($url, $requestMethod) ->performRequest(); if ($result) { $tweets = json_decode($result, true); foreach ($tweets as $tweet) { print_r($tweet); } }
This code example will retrieve and display your most recent 10 user statuses from Twitter using the Twitter API version 1.1.
The above is the detailed content of How to Retrieve Twitter User Timeline Data Using PHP and Twitter API v1.1?. For more information, please follow other related articles on the PHP Chinese website!