This commit is contained in:
Kai Hendry
2013-07-14 20:37:03 +08:00
parent 535373068b
commit f20496642b
2 changed files with 35 additions and 48 deletions

81
Oauth.php Normal file → Executable file
View File

@@ -4,25 +4,12 @@
if (empty($argv[1])) { exit(1); } if (empty($argv[1])) { exit(1); }
$urlargs = $argv[1]; $urlargs = $argv[1];
/**
* Oauth.php
*
* Created by Jon Hurlock on 2013-03-20.
*
* Jon Hurlock's Twitter Application-only Authentication App by Jon Hurlock (@jonhurlock)
* is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
* Permissions beyond the scope of this license may be available at http://www.jonhurlock.com/.
*/
/* /*
* Get the Bearer Token, this is an implementation of steps 1&2 * Get the Bearer Token, this is an implementation of steps 1&2
* from https://dev.twitter.com/docs/auth/application-only-auth * from https://dev.twitter.com/docs/auth/application-only-auth
*/ */
function get_bearer_token(){ function get_bearer_token(){
require("secret.php"); require("secret.php");
echo $consumer_key;
// Step 1 // Step 1
// step 1.1 - url encode the consumer_key and consumer_secret in accordance with RFC 1738 // step 1.1 - url encode the consumer_key and consumer_secret in accordance with RFC 1738
$encoded_consumer_key = urlencode($consumer_key); $encoded_consumer_key = urlencode($consumer_key);
@@ -33,14 +20,14 @@ function get_bearer_token(){
$base64_encoded_bearer_token = base64_encode($bearer_token); $base64_encoded_bearer_token = base64_encode($bearer_token);
// step 2 // step 2
$url = "https://api.twitter.com/oauth2/token"; // url to send data to for authentication $url = "https://api.twitter.com/oauth2/token"; // url to send data to for authentication
$headers = array( $headers = array(
"POST /oauth2/token HTTP/1.1", "POST /oauth2/token HTTP/1.1",
"Host: api.twitter.com", "Host: api.twitter.com",
"User-Agent: jonhurlock Twitter Application-only OAuth App v.1", "User-Agent: jonhurlock Twitter Application-only OAuth 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-Type: application/x-www-form-urlencoded;charset=UTF-8",
"Content-Length: 29" "Content-Length: 29"
); );
$ch = curl_init(); // setup a curl $ch = curl_init(); // setup a curl
curl_setopt($ch, CURLOPT_URL,$url); // set url to send to curl_setopt($ch, CURLOPT_URL,$url); // set url to send to
@@ -79,16 +66,16 @@ function invalidate_bearer_token($bearer_token){
$base64_encoded_consumer_token = base64_encode($consumer_token); $base64_encoded_consumer_token = base64_encode($consumer_token);
// step 2 // step 2
$url = "https://api.twitter.com/oauth2/invalidate_token"; // url to send data to for authentication $url = "https://api.twitter.com/oauth2/invalidate_token"; // url to send data to for authentication
$headers = array( $headers = array(
"POST /oauth2/invalidate_token HTTP/1.1", "POST /oauth2/invalidate_token HTTP/1.1",
"Host: api.twitter.com", "Host: api.twitter.com",
"User-Agent: jonhurlock Twitter Application-only OAuth App v.1", "User-Agent: jonhurlock Twitter Application-only OAuth App v.1",
"Authorization: Basic ".$base64_encoded_consumer_token."", "Authorization: Basic ".$base64_encoded_consumer_token."",
"Accept: */*", "Accept: */*",
"Content-Type: application/x-www-form-urlencoded", "Content-Type: application/x-www-form-urlencoded",
"Content-Length: ".(strlen($bearer_token)+13)."" "Content-Length: ".(strlen($bearer_token)+13).""
); );
$ch = curl_init(); // setup a curl $ch = curl_init(); // setup a curl
curl_setopt($ch, CURLOPT_URL,$url); // set url to send to curl_setopt($ch, CURLOPT_URL,$url); // set url to send to
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers
@@ -102,35 +89,35 @@ function invalidate_bearer_token($bearer_token){
return $retrievedhtml; return $retrievedhtml;
} }
/** function fetch($bearer_token, $query){
* Search $url = "https://api.twitter.com/1.1/statuses/user_timeline.json?";
* Basic Search of the Search API $headers = array(
* Based on https://dev.twitter.com/docs/api/1.1/get/search/tweets "GET /1.1/statuses/user_timeline.json?".$query." HTTP/1.1",
*/ "Host: api.twitter.com",
function search_for_a_term($bearer_token, $query, $result_type='mixed', $count='15'){
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$q = urlencode(trim($query)); // query term
$formed_url ='?q='.$q; // fully formed url
if($result_type!='mixed'){$formed_url = $formed_url.'&result_type='.$result_type;} // result type - mixed(default), recent, popular
if($count!='15'){$formed_url = $formed_url.'&count='.$count;} // results per page - defaulted to 15
$formed_url = $formed_url.'&include_entities=true'; // makes sure the entities are included, note @mentions are not included see documentation
$headers = array(
"GET /1.1/search/tweets.json".$formed_url." HTTP/1.1",
"Host: api.twitter.com",
"User-Agent: jonhurlock Twitter Application-only OAuth App v.1", "User-Agent: jonhurlock Twitter Application-only OAuth App v.1",
"Authorization: Bearer ".$bearer_token."", "Authorization: Bearer ".$bearer_token."",
); );
$ch = curl_init(); // setup a curl $ch = curl_init(); // setup a curl
curl_setopt($ch, CURLOPT_URL,$url.$formed_url); // set url to send to curl_setopt($ch, CURLOPT_URL, $url.$query); // set url to send to
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return output curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return output
$retrievedhtml = curl_exec ($ch); // execute the curl curl_setopt($ch, CURLOPT_HEADER, true);
curl_close($ch); // close the curl $content = curl_exec($ch);
return $retrievedhtml; list($header, $json) = explode("\r\n\r\n", $content, 2);
curl_close($ch);
file_put_contents('php://stderr', $header . "\n\n");
// No results returned, Twitter API issue
if (strlen($json) == 2) { exit(1); };
return $json;
} }
// lets run a search. // $bearer_token = get_bearer_token(); // bearer token seems to last
$bearer_token = get_bearer_token(); // get the bearer token require("secret.php");
print search_for_a_term($bearer_token, "test"); // search for the work 'test' echo "bearer token: " . $bearer_token . "\n";
print fetch($bearer_token, $urlargs); // search for the work 'test'
//invalidate_bearer_token($bearer_token); // invalidate the token //invalidate_bearer_token($bearer_token); // invalidate the token
?> ?>

View File

@@ -32,7 +32,7 @@ then
test "$2" && since='&max_id='$(tail -n1 $1.txt | cut -d'|' -f1) # use max_id to get older tweets test "$2" && since='&max_id='$(tail -n1 $1.txt | cut -d'|' -f1) # use max_id to get older tweets
fi fi
while urlargs="screen_name=${1}&count=200&page=${page}${since}&include_rts=1&trim_user=0"; echo $urlargs; $(dirname $0)/oauth.php $urlargs | while urlargs="screen_name=${1}&count=200&page=${page}${since}&include_rts=1&trim_user=0"; echo $urlargs; $(dirname $0)/Oauth.php $urlargs |
$(dirname $0)/json-to-text.php > $temp2; test $(wc -l < $temp2) -gt 0; $(dirname $0)/json-to-text.php > $temp2; test $(wc -l < $temp2) -gt 0;
do do