Overview

Namespaces

  • TwitterOAuth
    • Auth
    • Common
    • Exception
    • Serializer

Classes

  • Curl
  • 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:  * Connecting to Twitter API using SSL
 11:  * https://dev.twitter.com/overview/api/ssl
 12:  */
 13: 
 14: namespace TwitterOAuth\Common;
 15: 
 16: use TwitterOAuth\Exception\CurlException;
 17: 
 18: class Curl
 19: {
 20:     /**
 21:      * Send a request
 22:      *
 23:      * @param string $url Request URL
 24:      * @param array $params Configuration array
 25:      * @return array  Headers & Body
 26:      * @throws CurlException
 27:      */
 28:     public function send($url, array $params = array())
 29:     {
 30:         $out = array();
 31: 
 32:         $default = array(
 33:             'get' => array(),
 34:             'post' => array(),
 35:             'headers' => array(),
 36:             'cookies' => false,
 37:             'gzip' => true,
 38:             'ua' => 0,
 39:         );
 40: 
 41:         $params = array_merge($default, $params);
 42: 
 43: 
 44:         // Get Params //
 45:         if (is_array($params['get']) && !empty($params['get'])) {
 46:             $getParams = $this->getParams($params['get']);
 47: 
 48:             if (!empty($getParams)) {
 49:                 $url = $url . '?' . $getParams;
 50:             }
 51:         } elseif (is_string($params['get']) && !empty($params['get'])) {
 52:             $url = $url . '?' . $params['get'];
 53:         }
 54: 
 55: 
 56:         // Curl Options //
 57:         $options = array(
 58:             CURLOPT_URL => $url,
 59:             CURLOPT_HEADER => true,
 60:             CURLOPT_TIMEOUT => 60,
 61:             CURLOPT_CONNECTTIMEOUT => 60,
 62:             CURLOPT_FOLLOWLOCATION => true,
 63:             CURLOPT_RETURNTRANSFER => true,
 64:             CURLOPT_SSL_VERIFYPEER => true,
 65:             CURLOPT_SSL_VERIFYHOST => 2,
 66:             CURLOPT_CAINFO => dirname(__DIR__) . '/Certificates/rootca.pem',
 67:             CURLOPT_USERAGENT => $this->getUserAgent($params['ua']),
 68: 
 69:             // FOR DEBUG ONLY - PROXY SETTINGS //
 70:             //CURLOPT_PROXY => '127.0.0.1',
 71:             //CURLOPT_PROXYPORT => 8888,
 72:         );
 73: 
 74: 
 75:         // Post Params //
 76:         if (is_array($params['post']) && !empty($params['post'])) {
 77:             $options[CURLOPT_POST] = count($params['post']);
 78:             $options[CURLOPT_POSTFIELDS] = $this->getParams($params['post']);
 79:         } elseif (is_string($params['post']) && !empty($params['post'])) {
 80:             $options[CURLOPT_POST] = 1;
 81:             $options[CURLOPT_POSTFIELDS] = $params['post'];
 82:         }
 83: 
 84: 
 85:         // Headers //
 86:         if (is_array($params['headers']) && !empty($params['headers'])) {
 87:             $options[CURLOPT_HTTPHEADER] = $params['headers'];
 88:         }
 89: 
 90: 
 91:         // Cookies Filename //
 92:         if ($params['cookies'] !== false) {
 93:             $options[CURLOPT_COOKIEJAR] = $params['cookies'];
 94:             $options[CURLOPT_COOKIEFILE] = $params['cookies'];
 95:         }
 96: 
 97: 
 98:         // Gzip & Deflate //
 99:         if ($params['gzip'] === true) {
100:             $options[CURLOPT_ENCODING] = 'gzip,deflate';
101:         }
102: 
103: 
104:         // Run Curl //
105:         $c = curl_init();
106: 
107:         curl_setopt_array($c, $options);
108: 
109:         $response = curl_exec($c);
110: 
111:         if (curl_errno($c) !== 0) {
112:             throw new CurlException(curl_error($c), curl_errno($c));
113:         }
114: 
115:         $cInfo = curl_getinfo($c);
116: 
117:         curl_close($c);
118: 
119: 
120:         // Process Response //
121:         $out['headers'] = $this->processHeaders(substr($response, 0, $cInfo['header_size']));
122: 
123:         $out['body'] = trim(substr($response, $cInfo['header_size']));
124: 
125:         unset($params, $options, $c, $cInfo, $response);
126: 
127:         return $out;
128:     }
129: 
130:     /**
131:      * Converting parameters array to a single string with encoded values
132:      *
133:      * @param array $params Input parameters
134:      * @return string  Single string with encoded values
135:      */
136:     public function getParams(array $params)
137:     {
138:         $r = '';
139: 
140:         ksort($params);
141: 
142:         foreach ($params as $key => $value) {
143:             $r .= '&' . $key . '=' . rawurlencode($value);
144:         }
145: 
146:         unset($params, $key, $value);
147: 
148:         return trim($r, '&');
149:     }
150: 
151:     /**
152:      * Get User Agent ID
153:      *
154:      * @param null $key
155:      *     If null return random UA
156:      *     If integer then return UA from array index
157:      * @return string  User Agent ID
158:      */
159:     protected function getUserAgent($key = null)
160:     {
161:         $ua = array(
162:             // Chrome //
163:             'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36',
164:             'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1664.3 Safari/537.36',
165:             'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36',
166:             // Firefox //
167:             'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:26.0) Gecko/20100101 Firefox/26.0',
168:             'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:26.0) Gecko/20100101 Firefox/26.0',
169:             'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0',
170:             // IE //
171:             'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)',
172:             'Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0))',
173:             'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64)',
174:         );
175: 
176:         if ($key !== null) {
177:             return $ua[$key];
178:         }
179: 
180:         unset($key);
181: 
182:         return $ua[(int)mt_rand(0, count($ua) - 1)];
183:     }
184: 
185:     /**
186:      * Returns response headers as array
187:      *
188:      * This can be useful to avoid extra requests for rate-limit info
189:      *    x-rate-limit-limit      (max request per period)
190:      *    x-rate-limit-remaining  (remaining this period)
191:      *    x-rate-limit-reset      (start of next period, UTC timestamp)
192:      *
193:      * @param array $headers Headers string
194:      * @return array  Headers array
195:      */
196:     protected function processHeaders($headers)
197:     {
198:         $out = array();
199: 
200:         $headers = explode("\r\n", trim($headers));
201: 
202:         foreach ($headers as $header) {
203:             if (strpos($header, ':') !== false) {
204:                 $tmp = explode(':', $header);
205: 
206:                 $out[reset($tmp)] = end($tmp);
207:             } else {
208:                 if (!isset($out['http-code'])) {
209:                     $out['http-code'] = $header;
210:                 }
211:             }
212:         }
213: 
214:         unset($headers, $header, $tmp);
215: 
216:         return $out;
217:     }
218: }
219: 
TwitterOAuth (for v1.1 API) API documentation generated by ApiGen