Skip to content

Instantly share code, notes, and snippets.

@m13z
Last active November 22, 2018 02:41
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save m13z/6270524 to your computer and use it in GitHub Desktop.
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)
/*
* 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);
}
/*
* 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);
}
@lmj0011
Copy link

lmj0011 commented Dec 13, 2015

thanks for this, I had to add these 2 curl options to get it working for me.

in twitter_oauth_curl.php you should add:

curl_setopt($tk, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($tk, CURLOPT_SSL_VERIFYPEER, false);

@geansaturno
Copy link

Thanks man! Helped me a lot.

@GuiAlves2708
Copy link

What's this man for?

@GuiAlves2708
Copy link

I want to make a friend using friendship/create, it's possible?

@dhamecharahul
Copy link

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)

@hhoweson
Copy link

hhoweson commented Jan 4, 2018

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