Home >Backend Development >PHP Tutorial >How to Retrieve a User's Twitter Timeline Using PHP and the v1.1 API?
Simplest PHP Example for Retrieving User Timeline with Twitter API Version 1.1
Introduction:
The Twitter API 1.0 has been retired, rendering the provided script ineffective. This article presents an updated solution and a PHP class to retrieve user timelines with minimal code.
Prerequisites:
Solution:
Below is the simplified PHP code integrated with OAuth and the Twitter v1.1 API:
require_once('TwitterAPIExchange.php'); // Set access tokens $settings = array( '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' ); // Set request URL $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?count=10'; $requestMethod = 'GET'; // Instantiate Twitter API Exchange $twitter = new TwitterAPIExchange($settings); // Perform the request $output = $twitter->buildOauth($url, $requestMethod)->performRequest(); // Decode and print tweets $tweets = json_decode($output, true); foreach ($tweets as $tweet) { print_r($tweet); }
Implementation:
The above is the detailed content of How to Retrieve a User's Twitter Timeline Using PHP and the v1.1 API?. For more information, please follow other related articles on the PHP Chinese website!