Overview

Namespaces

  • TwitterOAuth
    • Auth
    • Common
    • Exception
    • Serializer

Classes

  • ApplicationOnlyAuth
  • AuthAbstract
  • SingleUserAuth
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * TwitterOAuth - https://github.com/ricardoper/TwitterOAuth
  5:  * PHP library to communicate with Twitter OAuth API version 1.1
  6:  *
  7:  * @author Ricardo Pereira <github@ricardopereira.es>
  8:  * @copyright 2014
  9:  */
 10: 
 11: namespace TwitterOAuth\Auth;
 12: 
 13: class SingleUserAuth extends AuthAbstract
 14: {
 15:     /**
 16:      * Expects the follow parameters:
 17:      *   - consumer_key         Twitter API key               * Required
 18:      *   - consumer_secret      Twitter API secret            * Required
 19:      *   - oauth_token          Twitter Access token
 20:      *   - oauth_token_secret   Twitter Access token secret
 21:      */
 22:     protected $requiredCredentials = array(
 23:         'consumer_key',
 24:         'consumer_secret',
 25:     );
 26: 
 27:     protected $urls = array(
 28:         'domain' => 'https://api.twitter.com/',
 29:         'upload' => 'https://upload.twitter.com/',
 30:         'api' => '1.1/',
 31:     );
 32: 
 33: 
 34:     /**
 35:      * Gets the Twitter Access token
 36:      *
 37:      * @return string
 38:      */
 39:     public function getAccessToken()
 40:     {
 41:         if (empty($this->credentials['oauth_token'])) {
 42:             return null;
 43:         }
 44: 
 45:         return $this->credentials['oauth_token'];
 46:     }
 47: 
 48:     /**
 49:      * Gets the Twitter Access token secret
 50:      *
 51:      * @return null|string
 52:      */
 53:     public function getAccessTokenSecret()
 54:     {
 55:         if (empty($this->credentials['oauth_token_secret'])) {
 56:             return null;
 57:         }
 58: 
 59:         return $this->credentials['oauth_token_secret'];
 60:     }
 61: 
 62:     /**
 63:      * Send a POST call to Twitter API via OAuth
 64:      *
 65:      * @param string $call Twitter resource string
 66:      * @param array $postParams POST parameters to send
 67:      * @param array $getParams GET parameters to send
 68:      * @return mixed  Output with selected format
 69:      * @throws \TwitterOAuth\Exception\TwitterException
 70:      */
 71:     public function post($call, array $postParams = null, array $getParams = null)
 72:     {
 73:         $this->resetCallState();
 74: 
 75:         $this->call = $call;
 76: 
 77:         $this->method = 'POST';
 78: 
 79:         if ($postParams !== null && is_array($postParams)) {
 80:             $this->postParams = $postParams;
 81:         }
 82: 
 83:         if ($getParams !== null && is_array($getParams)) {
 84:             $this->getParams = $getParams;
 85:         }
 86: 
 87:         $response = $this->getResponse();
 88: 
 89:         $this->findExceptions($response);
 90: 
 91:         $this->headers = $response['headers'];
 92: 
 93:         unset($call, $postParams, $getParams);
 94: 
 95:         return $this->serializer->format($response['body']);
 96:     }
 97: 
 98:     /**
 99:      * Send a POST call with media upload to Twitter API via OAuth
100:      *
101:      * @param string $call Twitter resource string
102:      * @param string $filename File location to upload
103:      * @return mixed  Output with selected format
104:      * @throws \TwitterOAuth\Exception\CurlException
105:      * @throws \TwitterOAuth\Exception\TwitterException
106:      */
107:     public function postMedia($call, $filename)
108:     {
109:         $this->resetCallState();
110: 
111:         $this->call = $call;
112: 
113:         $this->method = 'POST';
114: 
115:         $this->withMedia = true;
116: 
117:         $mimeBoundary = sha1($call . microtime());
118: 
119:         $params = array(
120:             'post' => $this->buildMultipart($mimeBoundary, $filename),
121:             'headers' => $this->buildUploadMediaHeader($mimeBoundary),
122:         );
123: 
124:         $response = $this->curl->send($this->getUrl(), $params);
125: 
126:         $obj = json_decode($response['body']);
127: 
128:         if (!$obj || !isset($obj->token_type) || $obj->token_type != 'bearer') {
129:             $this->findExceptions($response);
130:         }
131: 
132:         $this->headers = $response['headers'];
133: 
134:         $this->withMedia = null;
135: 
136:         unset($call, $filename, $mimeBoundary, $params, $obj);
137: 
138:         return $this->serializer->format($response['body']);
139:     }
140: 
141: 
142:     /**
143:      * Getting OAuth parameters to be used in request headers
144:      *
145:      * @return array  OAuth parameters
146:      */
147:     protected function getOauthParameters()
148:     {
149:         $time = time();
150: 
151:         return array(
152:             'oauth_consumer_key' => $this->getConsumerKey(),
153:             'oauth_nonce' => trim(base64_encode($time), '='),
154:             'oauth_signature_method' => 'HMAC-SHA1',
155:             'oauth_timestamp' => $time,
156:             'oauth_token' => $this->getAccessToken(),
157:             'oauth_version' => '1.0'
158:         );
159:     }
160: 
161:     /**
162:      * Converting all parameters agetrrays to a single string with encoded values
163:      *
164:      * @return string  Single string with encoded values
165:      */
166:     protected function getRequestString()
167:     {
168:         $params = array_merge($this->getParams, $this->postParams, $this->getOauthParameters());
169: 
170:         $params = $this->curl->getParams($params);
171: 
172:         return rawurlencode($params);
173:     }
174: 
175:     /**
176:      * Getting OAuth signature base string
177:      *
178:      * @return string  OAuth signature base string
179:      */
180:     protected function getSignatureBaseString()
181:     {
182:         $method = strtoupper($this->method);
183: 
184:         $url = rawurlencode($this->getUrl());
185: 
186:         return $method . '&' . $url . '&' . $this->getRequestString();
187:     }
188: 
189:     /**
190:      * Getting a signing key
191:      *
192:      * @return string  Signing key
193:      */
194:     protected function getSigningKey()
195:     {
196:         return $this->getConsumerSecret() . '&' . $this->getAccessTokenSecret();
197:     }
198: 
199:     /**
200:      * Calculating the signature
201:      *
202:      * @return string  Signature
203:      */
204:     protected function calculateSignature()
205:     {
206:         return base64_encode(hash_hmac('sha1', $this->getSignatureBaseString(), $this->getSigningKey(), true));
207:     }
208: 
209:     /**
210:      * Converting OAuth parameters array to a single string with encoded values
211:      *
212:      * @return string  Single string with encoded values
213:      */
214:     protected function getOauthString()
215:     {
216:         $oauth = array_merge($this->getOauthParameters(), array('oauth_signature' => $this->calculateSignature()));
217: 
218:         ksort($oauth);
219: 
220:         $values = array();
221: 
222:         foreach ($oauth as $key => $value) {
223:             $values[] = $key . '="' . rawurlencode($value) . '"';
224:         }
225: 
226:         $oauth = implode(', ', $values);
227: 
228:         unset($values, $key, $value);
229: 
230:         return $oauth;
231:     }
232: 
233:     /**
234:      * Building request HTTP headers
235:      *
236:      * @return array  HTTP headers
237:      */
238:     protected function buildRequestHeader()
239:     {
240:         return array(
241:             'Authorization: OAuth ' . $this->getOauthString(),
242:             'Expect:'
243:         );
244:     }
245: 
246:     /**
247:      * Building upload media headers
248:      *
249:      * @param string $mimeBoundary MIME boundary ID
250:      * @return array  HTTP headers
251:      */
252:     protected function buildUploadMediaHeader($mimeBoundary)
253:     {
254:         return array(
255:             'Authorization: OAuth ' . $this->getOauthString(),
256:             'Content-Type: multipart/form-data; boundary=' . $mimeBoundary,
257:             'Expect:'
258:         );
259:     }
260: }
TwitterOAuth (for v1.1 API) API documentation generated by ApiGen