Last active
November 22, 2018 02:41
-
-
Save m13z/6270524 to your computer and use it in GitHub Desktop.
Super simple PHP twitter oauth request without user context (https://dev.twitter.com/docs/auth/application-only-auth)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* using curl | |
*/ | |
$key = 'YOUR_KEY_HERE'; | |
$secret = 'YOUR_SECRET_HERE'; | |
$api_endpoint = 'https://api.twitter.com/1.1/users/show.json?screen_name=marcosfernandez'; // endpoint must support "Application-only authentication" | |
// request token | |
$basic_credentials = base64_encode($key.':'.$secret); | |
$tk = curl_init('https://api.twitter.com/oauth2/token'); | |
curl_setopt($tk, CURLOPT_HTTPHEADER, array('Authorization: Basic '.$basic_credentials, 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8')); | |
curl_setopt($tk, CURLOPT_POSTFIELDS, 'grant_type=client_credentials'); | |
curl_setopt($tk, CURLOPT_RETURNTRANSFER, true); | |
$token = json_decode(curl_exec($tk)); | |
curl_close($tk); | |
// use token | |
if (isset($token->token_type) && $token->token_type == 'bearer') { | |
$br = curl_init($api_endpoint); | |
curl_setopt($br, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$token->access_token)); | |
curl_setopt($br, CURLOPT_RETURNTRANSFER, true); | |
$data = curl_exec($br); | |
curl_close($br); | |
// do_something_here_with($data); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* using file_get_contents | |
*/ | |
$key = 'YOUR_KEY_HERE'; | |
$secret = 'YOUR_SECRET_HERE'; | |
$api_endpoint = 'https://api.twitter.com/1.1/users/show.json?screen_name=marcosfernandez'; // endpoint must support "Application-only authentication" | |
// request token | |
$basic_credentials = base64_encode($key.':'.$secret); | |
$opts = array('http' => | |
array( | |
'method' => 'POST', | |
'header' => 'Authorization: Basic '.$basic_credentials."\r\n". | |
"Content-type: application/x-www-form-urlencoded;charset=UTF-8\r\n", | |
'content' => 'grant_type=client_credentials' | |
) | |
); | |
$context = stream_context_create($opts); | |
// send request | |
$pre_token = file_get_contents('https://api.twitter.com/oauth2/token', false, $context); | |
$token = json_decode($pre_token, true); | |
if (isset($token["token_type"]) && $token["token_type"] == "bearer"){ | |
$opts = array('http' => | |
array( | |
'method' => 'GET', | |
'header' => 'Authorization: Bearer '.$token["access_token"] | |
) | |
); | |
$context = stream_context_create($opts); | |
$data = file_get_contents($api_endpoint, false, $context); | |
// do_something_here_with($data); | |
} |
What should be required to do twitter OAuth request with user context?
API endpoint is:- https://api.twitter.com/1.1/account_activity/webhooks/:webhook_id/subscriptions.json
Requires Authentication | Yes (user context only)
I know this is super old now but isn't setting
curl_setopt($tk, CURLOPT_SSL_VERIFYPEER, false);
super unsafe?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I want to make a friend using friendship/create, it's possible?