Last active
August 29, 2015 14:09
-
-
Save LukeXF/008f6c60c8a9550fcce0 to your computer and use it in GitHub Desktop.
A simple Gist to load the latest five Twitters from the Twitter API using the PHP SDK by j7mbo. This loads the NirvanaMC Twitter API
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 | |
// Sets the JSON file | |
header('Content-Type: application/json'); | |
// TWITTER STATS | |
// Loads the Twitter SDK system for PHP | |
require_once('TwitterAPI.php'); | |
// Create our Application instance that allows connecting to Twitter. | |
$settings = array( | |
'oauth_access_token' => "545371167-irULMOms3sQrOaNwsL73EWeXDHLfy6PmYWrE2FMU", | |
'oauth_access_token_secret' => "Zv9cS7OsiGXXuARfG92lj6UvO5yQwBjH16e987zEmtpjU", | |
'consumer_key' => "7eJxfBBdxGoYf1BcPetNi9whC", | |
'consumer_secret' => "pXTgOEQO804GWeZxPmXMqFmKTgrSc3KVs8GEH6M9tpHEL9wMpb" | |
); | |
// This call will always work since we are fetching public data. | |
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json'; | |
$getfield = '?screen_name=NirvanaNetwork'; | |
$requestMethod = 'GET'; | |
// Generate responce through the exchange | |
$twitter = new TwitterAPIExchange($settings); | |
// Build in Oauth and request data | |
$response = $twitter | |
->setGetfield($getfield) | |
->buildOauth($url, $requestMethod) | |
->performRequest(); | |
// Decode json file into array | |
$user = json_decode($response); | |
$user = objectToArray($user); | |
// converts the object into an array | |
function objectToArray($d) { | |
if (is_object($d)) { $d = get_object_vars($d); } | |
if (is_array($d)) { return array_map(__FUNCTION__, $d); } else { return $d; } | |
} | |
// API MAKING | |
// Sets the while loop counter | |
$twitterlists = 0; | |
// Creates the compressed API array | |
$twitterJSON = array(); | |
// while there is less than 5 results | |
// retunr the Twitter text and url id | |
while ($twitterlists <= 4) { | |
$twitterJSON[$twitterlists]['text'] = $user[$twitterlists]['text']; | |
$twitterJSON[$twitterlists]['url'] = $user[$twitterlists]['id']; | |
$twitterlists++; | |
} | |
// count the amount of stored results | |
// (there should be five) | |
$checkFive = count($twitterJSON); | |
if ($checkFive == 5) { | |
// if there is 5 then return the encoded array | |
echo json_encode($twitterJSON); | |
} else { | |
// else return an error | |
echo json_encode("There was a error - please report this to a staff member"); | |
} | |
// echo "<pre>"; | |
// print_r($user); | |
// echo "</pre>"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment