Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ncbateman
Created May 27, 2015 09:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ncbateman/c8119b411383bbeeaf82 to your computer and use it in GitHub Desktop.
Save ncbateman/c8119b411383bbeeaf82 to your computer and use it in GitHub Desktop.
Twitter Api oAuth 1.1 simple
<?php
$consumerKey = /*CONSUMER KEY*/;
$consumerSecret = /* CONSUMER SECRET*/;
$authContext = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Authorization: Basic " . base64_encode(($consumerKey).':'.($consumerSecret)) . "\r\n".
"Content-type: application/x-www-form-urlencoded;charset=UTF-8\r\n".
"Content-Length: 29\r\n".
"\r\n".
"grant_type=client_credentials",
),
));
$authResponse = file_get_contents("https://api.twitter.com/oauth2/token", false, $authContext);
var_dump($authResponse);
$decodedAuth = json_decode($authResponse, true);
$bearerToken = $decodedAuth["access_token"];
$data = ['id' => '594023646139654144'];
$context = stream_context_create(array(
'http' => array(
'method' => 'GET',
'header' => "Authorization: Bearer " . $bearerToken . "\r\n".
"\r\n".
"grant_type=client_credentials",
),
));
$encodedData = file_get_contents('https://api.twitter.com/1.1/statuses/show.json?id='/*TWEET ID HERE*/, false, $context);
var_dump(json_decode($encodedData));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment