|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Oauth.php |
| 4 | + * |
| 5 | + * Created by Jon Hurlock (@jonhurlock) 2013-03-20. |
| 6 | + * Extended by Sam Tuke (@samtuke) on 2014-10-09. |
| 7 | + * |
| 8 | + * Jon Hurlock's Twitter Application-only Authentication App by Jon Hurlock |
| 9 | + * is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. |
| 10 | + * Permissions beyond the scope of this license may be available at http://www.jonhurlock.com/. |
| 11 | + */ |
| 12 | + |
| 13 | +class twitterSearch |
| 14 | +{ |
| 15 | + // url to send data to for authentication |
| 16 | + const AUTH_URL = 'https://api.twitter.com/oauth2/token'; |
| 17 | + const SEARCH_URL = 'https://api.twitter.com/1.1/search/tweets.json'; |
| 18 | + CONST INVALIDATE_AUTH_URL = 'https://api.twitter.com/oauth2/invalidate_token'; |
| 19 | + |
| 20 | + /** |
| 21 | + * @brief Set app authentication data |
| 22 | + * @param string $consumerKey Your personal key from Twitter |
| 23 | + * @param string $consumerSecret Your personal secret from Twitter |
| 24 | + */ |
| 25 | + public function __construct( $consumerKey, $consumerSecret ) |
| 26 | + { |
| 27 | + // fill in your consumer key and consumer secret below |
| 28 | + $this->consumerKey = $consumerKey; |
| 29 | + $this->consumerSecret = $consumerSecret; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @brief Authenticate a session with Twitter using OAuth |
| 34 | + * @note https://dev.twitter.com/docs/auth/application-only-auth |
| 35 | + */ |
| 36 | + function authenticate() |
| 37 | + { |
| 38 | + // Step 1 |
| 39 | + // step 1.1 - url encode the consumer_key and consumer_secret in accordance with RFC 1738 |
| 40 | + $encoded_consumer_key = urlencode($this->consumerKey); |
| 41 | + $encoded_consumer_secret = urlencode($this->consumerSecret); |
| 42 | + // step 1.2 - concatinate encoded consumer, a colon character and the encoded consumer secret |
| 43 | + $bearer_token = $encoded_consumer_key.':'.$encoded_consumer_secret; |
| 44 | + // step 1.3 - base64-encode bearer token |
| 45 | + $base64_encoded_bearer_token = base64_encode($bearer_token); |
| 46 | + // step 2 |
| 47 | + $headers = array( |
| 48 | + "POST /oauth2/token HTTP/1.1", |
| 49 | + "Host: api.twitter.com", |
| 50 | + "User-Agent: jonhurlock Twitter Application-only OAuth App v.1", |
| 51 | + "Authorization: Basic ".$base64_encoded_bearer_token."", |
| 52 | + "Content-Type: application/x-www-form-urlencoded;charset=UTF-8", |
| 53 | + "Content-Length: 29" |
| 54 | + ); |
| 55 | + |
| 56 | + $ch = curl_init(); // setup a curl |
| 57 | + curl_setopt($ch, CURLOPT_URL, SELF::AUTH_URL); // set url to send to |
| 58 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers |
| 59 | + curl_setopt($ch, CURLOPT_POST, 1); // send as post |
| 60 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return output |
| 61 | + curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); // post body/fields to be sent |
| 62 | + $header = curl_setopt($ch, CURLOPT_HEADER, 1); // send custom headers |
| 63 | + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 64 | + $response = curl_exec ($ch); // execute the curl |
| 65 | + curl_close($ch); // close the curl |
| 66 | + $output = explode("\n", $response);var_dump($output); |
| 67 | + $bearer_token = ''; |
| 68 | + |
| 69 | + foreach ( $output as $line ) { |
| 70 | + if ( $line === false ) { |
| 71 | + // there was no bearer token |
| 72 | + } else { |
| 73 | + $bearer_token = $line; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // decode token |
| 78 | + $bearer_token = json_decode( $bearer_token ); |
| 79 | + |
| 80 | + // set token for reuse |
| 81 | + $this->token = $bearer_token->{'access_token'}; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @brief Invalidate the Bearer Token, end the session |
| 86 | + * @note should the bearer token become compromised or need to be invalidated for any reason, call this method/function. |
| 87 | + */ |
| 88 | + function unauthenticate () |
| 89 | + { |
| 90 | + $encoded_consumer_key = urlencode($this->consumerKey); |
| 91 | + $encoded_consumer_secret = urlencode($this->consumerSecret); |
| 92 | + $consumer_token = $encoded_consumer_key.':'.$encoded_consumer_secret; |
| 93 | + $base64_encoded_consumer_token = base64_encode($consumer_token); |
| 94 | + |
| 95 | + // step 2 |
| 96 | + $headers = array( |
| 97 | + "POST /oauth2/invalidate_token HTTP/1.1", |
| 98 | + "Host: api.twitter.com", |
| 99 | + "User-Agent: jonhurlock Twitter Application-only OAuth App v.1", |
| 100 | + "Authorization: Basic ".$base64_encoded_consumer_token."", |
| 101 | + "Accept: */*", |
| 102 | + "Content-Type: application/x-www-form-urlencoded", |
| 103 | + "Content-Length: ".(strlen($this->token)+13)."" |
| 104 | + ); |
| 105 | + |
| 106 | + $ch = curl_init(); // setup a curl |
| 107 | + curl_setopt($ch, CURLOPT_URL, SELF::INVALIDATE_AUTH_URL); // set url to send to |
| 108 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers |
| 109 | + curl_setopt($ch, CURLOPT_POST, 1); // send as post |
| 110 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return output |
| 111 | + curl_setopt($ch, CURLOPT_POSTFIELDS, "access_token=".$this->token.""); // post body/fields to be sent |
| 112 | + $header = curl_setopt($ch, CURLOPT_HEADER, 1); // send custom headers |
| 113 | + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 114 | + $retrievedhtml = curl_exec ($ch); // execute the curl |
| 115 | + curl_close($ch); // close the curl |
| 116 | + |
| 117 | + return $retrievedhtml; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @brief search for tweets with a given term |
| 122 | + * @param string $query Search term to look for (needle) |
| 123 | + * @param string $result_type Twitter compatible result type filter |
| 124 | + * @param int $count Limit for tweets to return |
| 125 | + * @return StdClass parsed from JSON with array of statuses ($returned->statuses[1]) |
| 126 | + * @note on https://dev.twitter.com/docs/api/1.1/get/search/tweets |
| 127 | + */ |
| 128 | + function search( $query, $result_type='mixed', $count='15' ) |
| 129 | + { |
| 130 | + $q = urlencode(trim($query)); // query term |
| 131 | + $formed_url ='?q='.$q; // fully formed url |
| 132 | + |
| 133 | + // result type - mixed(default), recent, popular |
| 134 | + if ( $result_type!='mixed' ) { |
| 135 | + $formed_url = $formed_url.'&result_type='.$result_type; |
| 136 | + } |
| 137 | + |
| 138 | + // results per page - defaulted to 15 |
| 139 | + if( $count!='15' ) { |
| 140 | + $formed_url = $formed_url.'&count='.$count; |
| 141 | + } |
| 142 | + |
| 143 | + $formed_url = $formed_url.'&include_entities=true'; // makes sure the entities are included, note @mentions are not included see documentation |
| 144 | + $headers = array( |
| 145 | + "GET /1.1/search/tweets.json".$formed_url." HTTP/1.1", |
| 146 | + "Host: api.twitter.com", |
| 147 | + "User-Agent: jonhurlock Twitter Application-only OAuth App v.1", |
| 148 | + "Authorization: Bearer ".$this->token."", |
| 149 | + ); |
| 150 | + |
| 151 | + $ch = curl_init(); // setup a curl |
| 152 | + curl_setopt($ch, CURLOPT_URL,SELF::SEARCH_URL.$formed_url); // set url to send to |
| 153 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers |
| 154 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return output |
| 155 | + $response = curl_exec ($ch); // execute the curl |
| 156 | + curl_close($ch); // close the curl |
| 157 | + |
| 158 | + $decoded = json_decode( $response ); |
| 159 | + |
| 160 | + return $decoded; |
| 161 | + } |
| 162 | +} |
0 commit comments