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

How to Retrieve a User's Twitter Timeline Using PHP and API v1.1?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-29 01:33:09195browse

How to Retrieve a User's Twitter Timeline Using PHP and API v1.1?

Simplest PHP Example for Retrieving User_Timeline with Twitter API Version 1.1

Due to the retirement of Twitter API 1.0 on June 11th, 2013, the original script mentioned is no longer functional. This guide provides an updated approach to retrieving recent user statuses with minimal coding effort.

Prerequisites:

  1. Create a Developer Account: Register for a Twitter developer account and create an application to obtain API tokens.
  2. Adjust Access Level: Change the application's access level to Read & Write.

PHP Code

To simplify the task, a PHP class is provided:

require_once('TwitterAPIExchange.php');

// Replace these values with your application's tokens
$settings = [
    'oauth_access_token' => 'YOUR_ACCESS_TOKEN',
    'oauth_access_token_secret' => 'YOUR_ACCESS_TOKEN_SECRET',
    'consumer_key' => 'YOUR_CONSUMER_KEY',
    'consumer_secret' => 'YOUR_CONSUMER_SECRET'
];

// Define the API endpoint and request method
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$requestMethod = 'GET';

// Create the TwitterAPIExchange instance
$twitter = new TwitterAPIExchange($settings);

// Build the request and perform it
echo $twitter->buildOauth($url, $requestMethod)->performRequest();

Usage:

  1. Replace the placeholder token values with your actual tokens obtained from the Twitter developer platform.
  2. Run the PHP script, and the output will display the user_timeline (recent statuses) for the authenticated user.

The above is the detailed content of How to Retrieve a User's Twitter Timeline Using PHP and 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