Created
May 13, 2013 02:43
-
-
Save here/5565880 to your computer and use it in GitHub Desktop.
How to update jQuery $.getJSON twitter API requests from 1.0 to 1.1
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 | |
/* code for getting bearer key -- in case this breaks | |
// from https://dev.twitter.com/docs/auth/application-only-auth | |
// and http://stackoverflow.com/questions/15503710/twitter-application-only-authentication-php-oauth-error | |
$consumer_key = '...'; | |
$consumer_secret = '...'; | |
// step 1 | |
// step 1.1 - url encode the consumer_key and consumer_secret in accordance with RFC 1738 | |
$encoded_consumer_key = urlencode($consumer_key); | |
$encoded_consumer_secret = urlencode($consumer_secret); | |
// step 1.2 - concatinate encoded consumer, a colon character and the encoded consumer secr et | |
$bearer_token = $encoded_consumer_key.':'.$encoded_consumer_secret; | |
// step 1.3 - base64-encode bearer token | |
$base64_encoded_bearer_token = base64_encode($bearer_token); | |
// step 2 | |
$url = "https://api.twitter.com/oauth2/token"; // url to send data to for authentication | |
$headers = array( | |
"POST /oauth2/token HTTP/1.1", | |
"Host: api.twitter.com", | |
"User-Agent: my Twitter App v.1", | |
//"Authorization: Basic ".$base64_encoded_bearer_token."", | |
"Authorization: Basic ".$base64_encoded_bearer_token."", | |
"Content-Type: application/x-www-form-urlencoded;charset=UTF-8", | |
"Content-Length: 29", | |
); | |
echo $base64_encoded_bearer_token; | |
$ch = curl_init(); // setup a curl | |
curl_setopt($ch, CURLOPT_URL,$url); // set url to send to | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers | |
curl_setopt($ch, CURLOPT_POST, 1); // send as post | |
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); // post body/fields to be sent | |
$header = curl_setopt($ch, CURLOPT_HEADER, 1); // send custom headers | |
//$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
$result = curl_exec($ch); // run the curl | |
curl_close($ch); // stop curling | |
echo $result; | |
*/ | |
// code for API request using bearer and PHP curl | |
$bearer = '...'; | |
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json?count=3&screen_name=username"; | |
$headers = array( | |
"Authorization: Bearer ".$bearer."", | |
); | |
$ch = curl_init(); // setup a curl | |
curl_setopt($ch, CURLOPT_URL, $url); // set url to send to | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return data reather than echo | |
$result = curl_exec($ch); // run the curl | |
curl_close($ch); // stop curling | |
//echo $result; | |
?> | |
<script> | |
var r = <?=$result?>; | |
// old 1.0 no auth technique | |
// $.getJSON("https://api.twitter.com/1/statuses/user_timeline.json?screen_name=username&count=3&callback=?", | |
// function(data){ ... | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment