1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: namespace TwitterOAuth\Common;
15:
16: use TwitterOAuth\Exception\CurlException;
17:
18: class Curl
19: {
20: 21: 22: 23: 24: 25: 26: 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:
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:
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:
70:
71:
72: );
73:
74:
75:
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:
86: if (is_array($params['headers']) && !empty($params['headers'])) {
87: $options[CURLOPT_HTTPHEADER] = $params['headers'];
88: }
89:
90:
91:
92: if ($params['cookies'] !== false) {
93: $options[CURLOPT_COOKIEJAR] = $params['cookies'];
94: $options[CURLOPT_COOKIEFILE] = $params['cookies'];
95: }
96:
97:
98:
99: if ($params['gzip'] === true) {
100: $options[CURLOPT_ENCODING] = 'gzip,deflate';
101: }
102:
103:
104:
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:
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: 132: 133: 134: 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: 153: 154: 155: 156: 157: 158:
159: protected function getUserAgent($key = null)
160: {
161: $ua = array(
162:
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:
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:
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: 187: 188: 189: 190: 191: 192: 193: 194: 195:
196: protected function ($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: