Created
July 14, 2013 11:27
-
-
Save NiceGuyNimni/5993981 to your computer and use it in GitHub Desktop.
A PHP class to handle Twitter API version 1.1. requests without the need for user authentication
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
<?php | |
class twitterConsole { | |
// This Class handles sending requests to Twitter's API without user authentication (See example at the end of the file) | |
private $twitterKey = 'ENTER_YOUR_KEY'; | |
private $twitterSecret = 'ENTER_YOUR_SECRET'; | |
private $keyConcatination =''; // Will be generated while constructing the class (See Below) | |
private $token = ''; // Will be generated while constructing the class (See Below) | |
public function __construct() { | |
$this->prepareRequestVariables(); | |
$this->token = $this->requestToken(); | |
} | |
private function prepareRequestVariables() { | |
$this->twitterKey = rawurlencode($this->twitterKey); // Uncodeing the data | |
$this->twitterSecret = rawurlencode($this->twitterSecret); // Uncodeing the data | |
$this->keyConcatination = base64_encode($this->twitterKey . ":" . $this->twitterSecret); | |
} | |
private function requestToken() { | |
// This function will request an application token to be used later | |
$url = 'https://api.twitter.com/oauth2/token'; // The API that will provide a reponse with the valid Token | |
$request = array('http' => | |
array( | |
'method' => 'POST', | |
'header' => "Authorization: Basic {$this->keyConcatination}\r\n". | |
"Content-Type: application/x-www-form-urlencoded;charset=UTF-8", | |
'content' => "grant_type=client_credentials" | |
)); | |
$context = stream_context_create($request); | |
$result = file_get_contents($url, false, $context); | |
$resultJSON = json_decode($result); | |
if(is_object($resultJSON)) { | |
$token = $resultJSON->access_token; | |
} | |
else { | |
$token = false; | |
} | |
return $token; | |
} | |
public function request($apiAddress, $dataArray) { | |
// This function will handle requests to Twitter's API | |
// Input: The full (incl. https) URL of the API and an Array of values to be sent | |
// Output: JSON object that holds the response | |
if (!$this->token) { // We have no Token from Twitter | |
return false; | |
} | |
$generatedURLRequest = $apiAddress . "?" . http_build_query($dataArray); | |
$request = array('http' => | |
array('header' => "Authorization: Bearer {$this->token}")); | |
$context = stream_context_create($request); | |
$result = file_get_contents($generatedURLRequest, false, $context); | |
$resultJSON = json_decode($result); | |
return $resultJSON; | |
} | |
} | |
/* | |
* Example for requesting the ID of a user based on his screen_name | |
* | |
* $twitter = new twitterConsole; | |
* | |
* $response = $twitter->request("https://api.twitter.com/1.1/users/show.json", array("screen_name" => "guynimni")); | |
* | |
* echo "This is the ID: " . $response->id; | |
* | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment