|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | +A Codeigniter libray to make requests to the Twitter API |
| 5 | +Copyright (C) 2013 Christopher Pattle |
| 6 | +
|
| 7 | +This program is free software: you can redistribute it and/or modify |
| 8 | +it under the terms of the GNU General Public License as published by |
| 9 | +the Free Software Foundation, either version 3 of the License, or any later version. |
| 10 | +
|
| 11 | +This program is distributed in the hope that it will be useful, |
| 12 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +GNU General Public License for more details. |
| 15 | +
|
| 16 | +You should have received a copy of the GNU General Public License |
| 17 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 21 | + |
| 22 | +class Twitter |
| 23 | +{ |
| 24 | + private $_consumerKey; |
| 25 | + private $_consumerSecret; |
| 26 | + private $_oAuthToken; |
| 27 | + private $_oAuthSecret; |
| 28 | + |
| 29 | + private $_oAuthNonce; |
| 30 | + private $_oAuthSignature; |
| 31 | + private $_oAuthSignatureMethod; |
| 32 | + private $_oAuthTimeStamp; |
| 33 | + private $_oAuthVersion; |
| 34 | + |
| 35 | + private $_httpMethod; |
| 36 | + private $_baseUrl; |
| 37 | + private $_requestFormat; |
| 38 | + |
| 39 | + private $_aParams; |
| 40 | + |
| 41 | + function __construct() |
| 42 | + { |
| 43 | + $this->_consumerKey = 'Enter your consumer key here'; |
| 44 | + $this->_consumerSecret = 'Enter your consumer secret here'; |
| 45 | + $this->_oAuthToken = 'Enter your access token here'; |
| 46 | + $this->_oAuthSecret = 'Enter your access token secret here'; |
| 47 | + $this->_oAuthNonce = $this->generateNonce(); |
| 48 | + $this->_oAuthSignature = ''; |
| 49 | + $this->_oAuthSignatureMethod = 'HMAC-SHA1'; |
| 50 | + $this->_oAuthTimeStamp = date('U'); |
| 51 | + $this->_oAuthVersion = '1.0'; |
| 52 | + } |
| 53 | + |
| 54 | + /* |
| 55 | + * generateNonce() |
| 56 | + * Generates a random alphanumeric string containing upper and lower case |
| 57 | + * characters of a set length |
| 58 | + * |
| 59 | + * @author Chris Pattle |
| 60 | + * |
| 61 | + * @param $numChars INT The number of characters the string should be |
| 62 | + * |
| 63 | + * @return STRING Returns |
| 64 | + */ |
| 65 | + function generateNonce($numChars = 24) |
| 66 | + { |
| 67 | + //Create multidimentional array of random characters |
| 68 | + $aChars[] = range('a', 'z'); |
| 69 | + $aChars[] = range('A', 'Z'); |
| 70 | + $aChars[] = range('0', '9'); |
| 71 | + |
| 72 | + $nonce = ''; |
| 73 | + |
| 74 | + //Create a loop until we reach our target number of characters |
| 75 | + while(strlen($nonce) < $numChars) |
| 76 | + { |
| 77 | + $num = rand(0, 2); |
| 78 | + |
| 79 | + if($num < 2) |
| 80 | + $num2 = rand(0, 24); |
| 81 | + else |
| 82 | + $num2 = rand(0,8); |
| 83 | + |
| 84 | + $nonce .= $aChars[$num][$num2]; |
| 85 | + } |
| 86 | + |
| 87 | + return $nonce; |
| 88 | + } |
| 89 | + |
| 90 | + /* |
| 91 | + * encodeRequestParams() |
| 92 | + * Percent encodes the request parameters |
| 93 | + * |
| 94 | + * @author Chris Pattle |
| 95 | + * |
| 96 | + * @param $aRequestParams ARRAY An array of request parameters |
| 97 | + */ |
| 98 | + function encodeRequestParams($aRequestParams) |
| 99 | + { |
| 100 | + $this->_aRequestParams = array(); |
| 101 | + |
| 102 | + foreach($aRequestParams as $paramKey => $paramValue) |
| 103 | + { |
| 104 | + $this->_aRequestParams[rawurlencode($paramKey)] = rawurlencode($paramValue); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /* |
| 109 | + * buildParams() |
| 110 | + * Builds an array of parameters needed for the request |
| 111 | + * |
| 112 | + * @author Chris Pattle |
| 113 | + * |
| 114 | + * @param $aRequestParams ARRAY An array of request parameters |
| 115 | + * |
| 116 | + * @return ARRAY Returns the array of parameters |
| 117 | + */ |
| 118 | + function buildParams() |
| 119 | + { |
| 120 | + $this->_aParams = array( |
| 121 | + rawurlencode('oauth_consumer_key') => rawurlencode($this->_consumerKey), |
| 122 | + rawurlencode('oauth_token') => rawurlencode($this->_oAuthToken), |
| 123 | + rawurlencode('oauth_nonce') => rawurlencode($this->_oAuthNonce), |
| 124 | + rawurlencode('oauth_signature_method') => rawurlencode($this->_oAuthSignatureMethod), |
| 125 | + rawurlencode('oauth_timestamp') => rawurlencode($this->_oAuthTimeStamp), |
| 126 | + rawurlencode('oauth_version') => rawurlencode($this->_oAuthVersion) |
| 127 | + ); |
| 128 | + |
| 129 | + $this->_aParams[rawurlencode('oauth_signature')] = $this->getSignature(); |
| 130 | + } |
| 131 | + |
| 132 | + /* |
| 133 | + * getSignature() |
| 134 | + * Gerenates a signature |
| 135 | + * |
| 136 | + * @author Chris Pattle |
| 137 | + * @api https://dev.twitter.com/docs/auth/creating-signature |
| 138 | + * |
| 139 | + * @return STRING Returns the oauth_signature |
| 140 | + */ |
| 141 | + function getSignature() |
| 142 | + { |
| 143 | + $paramString = ''; |
| 144 | + |
| 145 | + $aParams = array_merge($this->_aParams, $this->_aRequestParams); |
| 146 | + ksort($aParams); |
| 147 | + |
| 148 | + foreach($aParams as $paramKey => $paramValue) |
| 149 | + { |
| 150 | + if($paramString != '') |
| 151 | + $paramString .= '&'; |
| 152 | + |
| 153 | + $paramString .= $paramKey . '=' . $paramValue; |
| 154 | + } |
| 155 | + |
| 156 | + $signatureBaseString = rawurlencode(strtoupper($this->_httpMethod)) . '&' . rawurlencode($this->_baseUrl) . '&' . rawurlencode($paramString); |
| 157 | + |
| 158 | + $signingKey = rawurlencode($this->_consumerSecret) . '&' . rawurlencode($this->_oAuthSecret); |
| 159 | + |
| 160 | + $signature = base64_encode(hash_hmac('sha1', $signatureBaseString, $signingKey, TRUE)); |
| 161 | + |
| 162 | + return $signature; |
| 163 | + } |
| 164 | + |
| 165 | + /* |
| 166 | + * buildParamString() |
| 167 | + * Build the a string of request parameters. |
| 168 | + * This is either sent via POST or append to the end of the URL for GET's |
| 169 | + * |
| 170 | + * @author Chris Pattle |
| 171 | + * |
| 172 | + * @return STRING Returns the string of parameters |
| 173 | + */ |
| 174 | + function buildParamString() |
| 175 | + { |
| 176 | + $paramString = ''; |
| 177 | + |
| 178 | + foreach($this->_aRequestParams as $paramKey => $paramValue) |
| 179 | + { |
| 180 | + if($paramString != '') |
| 181 | + $paramString .= '&'; |
| 182 | + |
| 183 | + $paramString .= $paramKey . '=' . $paramValue; |
| 184 | + } |
| 185 | + |
| 186 | + return $paramString; |
| 187 | + } |
| 188 | + |
| 189 | + /* |
| 190 | + * buildAuthHeader() |
| 191 | + * Create the authorization header needed to let Twitter know who the |
| 192 | + * request is coming from |
| 193 | + * |
| 194 | + * @author Chris Pattle |
| 195 | + * |
| 196 | + * @return STRING Returns the authorization header |
| 197 | + */ |
| 198 | + function buildAuthHeader() |
| 199 | + { |
| 200 | + $authHeader = 'OAuth '; |
| 201 | + |
| 202 | + ksort($this->_aParams); |
| 203 | + |
| 204 | + $paramCount = 1; |
| 205 | + |
| 206 | + foreach($this->_aParams as $paramKey => $paramValue) |
| 207 | + { |
| 208 | + $authHeader .= rawurlencode($paramKey) . '="' . rawurlencode($paramValue) . '"'; |
| 209 | + |
| 210 | + if($paramCount != count($this->_aParams)) |
| 211 | + $authHeader .= ', '; |
| 212 | + |
| 213 | + $paramCount++; |
| 214 | + } |
| 215 | + |
| 216 | + return $authHeader; |
| 217 | + } |
| 218 | + |
| 219 | + /* |
| 220 | + * getRequestFormat() |
| 221 | + * Get the format for the request from the base URL |
| 222 | + * This should be either xml or json |
| 223 | + * |
| 224 | + * @author Chris Pattle |
| 225 | + * |
| 226 | + * @return STRING Returns the last part of the base URL which should be the format |
| 227 | + */ |
| 228 | + function getRequestFormat() |
| 229 | + { |
| 230 | + $aURLParts = explode('.', $this->_baseUrl); |
| 231 | + |
| 232 | + return end($aURLParts); |
| 233 | + } |
| 234 | + |
| 235 | + /* |
| 236 | + * request() |
| 237 | + * Makes a request using the Twitter API |
| 238 | + * |
| 239 | + * @author Chris Pattle |
| 240 | + * |
| 241 | + * @param $httpMethod STRING POST/GET etc |
| 242 | + * @param $baseUrl STRING The base URL for the call |
| 243 | + * @param $aRequestParams ARRAY An array of parameters specific to this request |
| 244 | + * |
| 245 | + * @return Returns the response from Twitter. Either XML or JSON |
| 246 | + */ |
| 247 | + function request($httpMethod, $baseUrl, $aRequestParams = array()) |
| 248 | + { |
| 249 | + //Set the httpMethod and base URL for this request |
| 250 | + $this->_httpMethod = $httpMethod; |
| 251 | + $this->_baseUrl = $baseUrl; |
| 252 | + $this->_requestFormat = $this->getRequestFormat(); |
| 253 | + $this->_requestParams = $this->encodeRequestParams($aRequestParams); |
| 254 | + |
| 255 | + //Create an array of parameters |
| 256 | + $this->buildParams(); |
| 257 | + |
| 258 | + //Create the parameter string we will need to send |
| 259 | + $paramString = $this->buildParamString(); |
| 260 | + |
| 261 | + //Build the authorization header so Twitter know its us |
| 262 | + $authHeader = $this->buildAuthHeader(); |
| 263 | + |
| 264 | + //If we are using GET we need to append the parameters to the URL |
| 265 | + if($httpMethod == 'GET') |
| 266 | + $this->_baseUrl .= '?' . $paramString; |
| 267 | + |
| 268 | + //Use cURL to POST the data needed to send the SMS |
| 269 | + $curl = curl_init($this->_baseUrl); |
| 270 | + curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: ' . $authHeader)); |
| 271 | + curl_setopt($curl, CURLOPT_HEADER, FALSE); |
| 272 | + |
| 273 | + //If we are using POST we need to configure cURL to post the parameters |
| 274 | + if($this->_httpMethod == 'POST') |
| 275 | + { |
| 276 | + curl_setopt($curl, CURLOPT_POST, TRUE); |
| 277 | + curl_setopt($curl, CURLOPT_POSTFIELDS, $paramString); |
| 278 | + } |
| 279 | + |
| 280 | + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); |
| 281 | + curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); |
| 282 | + curl_setopt($curl, CURLOPT_TIMEVALUE, 30); |
| 283 | + |
| 284 | + $oReturn = curl_exec($curl); |
| 285 | + |
| 286 | + if($oReturn === FALSE) |
| 287 | + { |
| 288 | + curl_close($curl); |
| 289 | + return FALSE; |
| 290 | + } |
| 291 | + else |
| 292 | + { |
| 293 | + return $oReturn; |
| 294 | + } |
| 295 | + } |
| 296 | +} |
| 297 | + |
| 298 | +/* End of file twitter.php */ |
| 299 | +/* Location: ./application/libraries/twitter.php */ |
0 commit comments