1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: namespace Kotchasan;
12:
13: 14: 15: 16: 17: 18: 19:
20: class Curl
21: {
22: 23: 24: 25: 26: 27: 28:
29: protected $error = 0;
30: 31: 32: 33: 34:
35: protected $errorMessage = '';
36: 37: 38: 39: 40:
41: protected $headers = array();
42: 43: 44: 45: 46:
47: protected $options = array();
48:
49: 50: 51: 52: 53:
54: public function __construct()
55: {
56: if (!extension_loaded('curl')) {
57: throw new \Exception('cURL library is not loaded');
58: }
59:
60: $this->headers = array(
61: 'Connection' => 'keep-alive',
62: 'Keep-Alive' => '300',
63: 'Accept-Charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
64: 'Accept-Language' => 'en-us,en;q=0.5',
65: );
66: $this->options = array(
67: CURLOPT_TIMEOUT => 30,
68: CURLOPT_RETURNTRANSFER => true,
69: CURLOPT_USERAGENT => 'Googlebot/2.1 (+http://www.google.com/bot.html)',
70: CURLOPT_SSL_VERIFYHOST => false,
71: CURLOPT_SSL_VERIFYPEER => false,
72: );
73: }
74:
75: 76: 77: 78: 79: 80: 81: 82:
83: public function delete($url, $params)
84: {
85: $this->options[CURLOPT_CUSTOMREQUEST] = 'DELETE';
86: if (is_array($params)) {
87: $this->options[CURLOPT_POSTFIELDS] = http_build_query($params, '', '&');
88: } else {
89: $this->options[CURLOPT_POSTFIELDS] = $params;
90: }
91:
92: return $this->execute($url);
93: }
94:
95: 96: 97: 98: 99: 100:
101: public function error()
102: {
103: return $this->error;
104: }
105:
106: 107: 108: 109: 110:
111: public function errorMessage()
112: {
113: return $this->errorMessage;
114: }
115:
116: 117: 118: 119: 120: 121: 122: 123:
124: public function get($url, $params = array())
125: {
126: $this->options[CURLOPT_CUSTOMREQUEST] = 'GET';
127: $this->options[CURLOPT_HTTPGET] = true;
128: if (is_array($params)) {
129: $url .= (strpos($url, '?') === false ? '?' : '&').http_build_query($params, '', '&');
130: } else {
131: $this->options[CURLOPT_POSTFIELDS] = $params;
132: }
133:
134: return $this->execute($url);
135: }
136:
137: 138: 139: 140: 141: 142: 143: 144:
145: public function head($url, $params = array())
146: {
147: $this->options[CURLOPT_CUSTOMREQUEST] = 'HEAD';
148: $this->options[CURLOPT_NOBODY] = true;
149: if (is_array($params)) {
150: $this->options[CURLOPT_POSTFIELDS] = http_build_query($params, '', '&');
151: } else {
152: $this->options[CURLOPT_POSTFIELDS] = $params;
153: }
154:
155: return $this->execute($url);
156: }
157:
158: 159: 160: 161: 162: 163: 164: 165: 166:
167: public function httpauth($username = '', $password = '', $type = 'any')
168: {
169: $this->options[CURLOPT_HTTPAUTH] = constant('CURLAUTH_'.strtoupper($type));
170: $this->options[CURLOPT_USERPWD] = $username.':'.$password;
171:
172: return $this;
173: }
174:
175: 176: 177: 178: 179: 180: 181: 182: 183: 184:
185: public function httpproxy($url = '', $port = 80, $username = null, $password = null)
186: {
187: $this->options[CURLOPT_HTTPPROXYTUNNEL] = true;
188: $this->options[CURLOPT_PROXY] = $url.':'.$port;
189: if ($username !== null && $password !== null) {
190: $this->options[CURLOPT_PROXYUSERPWD] = $username.':'.$password;
191: }
192:
193: return $this;
194: }
195:
196: 197: 198: 199: 200: 201: 202: 203:
204: public function post($url, $params = array())
205: {
206: $this->options[CURLOPT_CUSTOMREQUEST] = 'POST';
207: $this->options[CURLOPT_POST] = true;
208: if (is_array($params)) {
209: $this->options[CURLOPT_POSTFIELDS] = http_build_query($params, '', '&');
210: } else {
211: $this->options[CURLOPT_POSTFIELDS] = $params;
212: }
213:
214: return $this->execute($url);
215: }
216:
217: 218: 219: 220: 221: 222: 223: 224:
225: public function put($url, $params = array())
226: {
227: $this->options[CURLOPT_CUSTOMREQUEST] = 'PUT';
228: if (is_array($params)) {
229: $this->options[CURLOPT_POSTFIELDS] = http_build_query($params, '', '&');
230: } else {
231: $this->options[CURLOPT_POSTFIELDS] = $params;
232: }
233:
234: return $this->execute($url);
235: }
236:
237: 238: 239: 240: 241: 242: 243:
244: public function referer($referrer)
245: {
246: $this->options[CURLOPT_REFERER] = $referrer;
247:
248: return $this;
249: }
250:
251: 252: 253: 254: 255: 256: 257:
258: public function setCookie($cookiePath)
259: {
260: $this->options[CURLOPT_COOKIEFILE] = $cookiePath;
261: $this->options[CURLOPT_COOKIEJAR] = $cookiePath;
262:
263: return $this;
264: }
265:
266: 267: 268: 269: 270: 271: 272:
273: public function setHeaders($headers)
274: {
275: foreach ($headers as $key => $value) {
276: $this->headers[$key] = $value;
277: }
278:
279: return $this;
280: }
281:
282: 283: 284: 285: 286: 287: 288:
289: public function setOptions($options)
290: {
291: foreach ($options as $key => $value) {
292: $this->options[$key] = $value;
293: }
294:
295: return $this;
296: }
297:
298: 299: 300: 301: 302: 303: 304:
305: protected function execute($url)
306: {
307: $ch = curl_init();
308: curl_setopt($ch, CURLOPT_URL, $url);
309: if (!empty($this->headers)) {
310: $headers = array();
311: foreach ($this->headers as $key => $value) {
312: $headers[] = $key.': '.$value;
313: }
314: curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
315: }
316: foreach ($this->options as $key => $value) {
317: curl_setopt($ch, $key, $value);
318: }
319: $response = curl_exec($ch);
320: if (curl_error($ch)) {
321: $this->error = curl_errno($ch);
322: $this->errorMessage = curl_error($ch);
323: }
324: curl_close($ch);
325:
326: return $response;
327: }
328: }
329: