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

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

DDD
Release: 2024-12-20 04:58:17
Original
851 people have browsed it

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

Simplest PHP Example for Retrieving user_timeline with Twitter API Version 1.1

Understanding the API Changes

Twitter API 1.0 has been retired, and its replacement, API 1.1, requires different authentication mechanisms.

Twilio Credentials

To obtain the necessary credentials for Twitter API 1.1, follow these steps:

  • Create a developer account: Register at dev.twitter.com.
  • Create an application: Set up an application that will have its own unique keys.
  • Generate access tokens: Create access tokens for the application, consisting of the consumer key, consumer secret, access token, and access token secret.
  • Set read/write access: Enable read and write access for your application in the settings.

Simplified PHP Code

The following PHP class simplifies the process of accessing the Twitter API:

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';
$getfield = '?screen_name=username&count=10';
$requestMethod = 'GET';

$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
    ->buildOauth($url, $requestMethod)
    ->performRequest();

$tweets = json_decode($response);
foreach ($tweets as $tweet) {
    print_r($tweet);
}
Copy after login

The above is the detailed content of How to Retrieve Twitter User Timeline Data Using PHP and API v1.1?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template