Home >Backend Development >PHP Tutorial >How to Retrieve Twitter User Timeline Data Using PHP and Twitter API v1.1?

How to Retrieve Twitter User Timeline Data Using PHP and Twitter API v1.1?

DDD
DDDOriginal
2024-12-28 01:57:12727browse

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:

  • Register as a developer on Twitter.
  • Create an application and obtain the necessary API keys, including consumer key, consumer secret, access token, and access token secret.

2. Adjust Application Settings:

  • Set the application's access level to Read & Write for desired functionality.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn