How To Get Twitter Trends API OAuth with PHP Script

You currently have Essential access which includes access to Twitter API v2 endpoints only. If you need access to this endpoint, you’ll need to apply for Elevated access via the Developer Portal.

https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api#item0

{
  "errors": [
    {
      "message": "You currently have Essential access which includes access to Twitter API v2 endpoints only. If you need access to this endpoint, you’ll need to apply for Elevated access via the Developer Portal. You can learn more here: https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api#v2-access-leve",
      "code": 453
    }
  ]
}


Here’s a sample PHP script that uses the Twitter API OAuth authentication process to get the current trending topics:

<?php

// Replace with your own values
$consumer_key = 'xxxxxx';
$consumer_secret = 'xxxxxx';

// Set up the URL for the OAuth request
$url = "https://api.twitter.com/oauth2/token";

// Set up the headers for the OAuth request
$headers = array(
    "Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
    "Authorization: Basic " . base64_encode($consumer_key . ":" . $consumer_secret)
);

// Set up the data for the OAuth request
$data = "grant_type=client_credentials";

// Initialize a cURL session
$curl = curl_init();

// Set the cURL options
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

// Send the OAuth request and get the response
$response = curl_exec($curl);

// Close the cURL session
curl_close($curl);

// Decode the JSON response
$json = json_decode($response);

// Get the access token from the response
$access_token = $json->access_token;

// Set up the URL for the Trends API request
$url = "https://api.twitter.com/1.1/trends/place.json?id=1";

// Set up the headers for the Trends API request
$headers = array(
    "Authorization: Bearer " . $access_token
);

// Initialize a new cURL session
$curl = curl_init();

// Set the cURL options
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

// Send the Trends API request and get the response
$response = curl_exec($curl);
var_dump($response);
// Close the cURL session
curl_close($curl);

// Decode the JSON response
$json = json_decode($response);

// Print out the top 10 trending topics in the United States
for ($i = 0; $i < 10; $i++) {
    echo ($i+1) . ". " . $json[0]->trends[$i]->name . "\n";
}

?>

Make sure to replace YOUR_CONSUMER_KEY and YOUR_CONSUMER_SECRET with your own Twitter API credentials.

This script first makes an OAuth 2.0 request to obtain an access token, then uses that access token to make a request to the Trends API and print out the top 10 trending topics in the United States. You can modify the id parameter in the Trends API URL to get trending topics for a different location.

[wpedon id=2706]

Exit mobile version