Twitter is one of the most powerful social network platform to promote your application. With Twitter API, you can make users follow, tweet, DM and much more directly from your website without opening twitter.
In this article, we will learn about how to integrate Twitter API in your web application.
To start with Twitter API, first create your Twitter developer account. Then create a new Application. Now you can get Consumer API keys from the generated Application in Keys and tokens
tab. Don't forget to set appropriate permissions also from the permission
tab. Here is the Twitter API request flow.
Now from your code side, to create any Twitter API curl request, first create a new curl request to get bearer token and pass it in request header.
To get bearer token, first encode consumer key and consumer secret in RFC 1738 method
. Then from consumers credential now encode them in base64 format
.
And you will get string like this:
eHZ6MWV2RlM0d0VFUFRHRUZQSEJvZzpMOHFxOVBaeVJnNmllS0dFS2hab2xHQzB2SldMdzhpRUo4OERSZHlPZw==
Now you all you need to do is get bearer token with post CURL request with https://api.twitter.com/oauth2/token?grant_type=client_credentials this URL.
Be sure to set header with
and you will get Bearer Token in bellow json format
{
"token_type": "bearer",
"access_token": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA%2FAAAAAAAAAAAAAAAAAAAA%3DAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
}
Here is my request code in PHP language:
<?php
$consumer_api_key = rawurldecode('xvz1evFS4wEEPTGEFPHBog');
$consumer_api_secret = rawurldecode('L8qq9PZyRg6ieKGEKhZolGC0vJWLw8iEJ88DRdyOg');
$base64_key = base64_encode($consumer_api_key.':'.$consumer_api_secret);
$url = 'https://api.twitter.com/oauth2/token?grant_type=client_credentials';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"authorization: Basic ".$base64_key,
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
Now you have get access_teken
which you can pass in header request in any Twitter API. Here is all lists of Twitter API.
For example, if you want to get User data by screen name or id, just send GET request in https://api.twitter.com/1.1/users/show.json?screen_name=here-comes-twitter-screen-name
URL.
Here is my request code in PHP:
<?php
$url = 'https://api.twitter.com/1.1/users/show.json?screen_name=twitterdev';
$access_token = 'AAAAAAAAAAAAAAAAAAAAAJEvBgEAAAAALuTinPbWuLuDuWA0NRr7ZE%3DCNtMfu1gXJYW8MQzAOBnlIqUz73oIWuRFjUSetGNFN3cYCANj8';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer ".$access_token,
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
You can also invalidate the generated token by the following request.
<?php
$consumer_api_key = rawurldecode('xvz1evFS4wEEPTGEFPHBog');
$consumer_api_secret = rawurldecode('L8qq9PZyRg6ieKGEKhZolGC0vJWLw8iEJ88DRdyOg');
$base64_key = base64_encode($consumer_api_key.':'.$consumer_api_secret);
$access_token = '$access_token';
$url = 'https://api.twitter.com/oauth2/invalidate_token?access_token='.$access_token;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"authorization: Basic ".$base64_key,
'Accept: */*',
'Content-Length: 119',
'Content-Type: application/x-www-form-urlencoded'
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
This way, you can create all Twitter API request from the access_token
. I hope this article will help new developers to use Twitter API. Thank you all and have a happy twitting.
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to Get Query Parameters from URL route in Angular
Getting current URL and parameters is ba...Increment and Decrement Column Value in Laravel
In this article we will share with you h...Laravel 7.x - Multiple Images Upload with Preview Example
In this article, i will let you ken ajax...Laravel 8 Listen Model Observers Tutorial Example
If you want to listen many events for an...How to Convert JS Object to JSON String
Use the JSON.stringify() Method You c...