Commit | Line | Data |
---|---|---|
7f254ad8 AE |
1 | <?php |
2 | ||
3 | /** | |
4 | * An Object Oriented interface to PHP's cURL extension | |
5 | * | |
6 | * PHP version 5.1.0+ | |
7 | * | |
8 | * Copyright (c) 2007, The PEAR Group | |
9 | * | |
10 | * All rights reserved. | |
11 | * | |
12 | * Redistribution and use in source and binary forms, with or without | |
13 | * modification, are permitted provided that the following conditions are met: | |
14 | * | |
15 | * - Redistributions of source code must retain the above copyright notice, | |
16 | * this list of conditions and the following disclaimer. | |
17 | * - Redistributions in binary form must reproduce the above copyright notice, | |
18 | * this list of conditions and the following disclaimer in the documentation | |
19 | * and/or other materials provided with the distribution. | |
20 | * - Neither the name of the The PEAR Group nor the names of its contributors | |
21 | * may be used to endorse or promote products derived from this software | |
22 | * without specific prior written permission. | |
23 | * | |
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
34 | * POSSIBILITY OF SUCH DAMAGE. | |
35 | * | |
36 | * @category Net | |
37 | * @package Net_Curl | |
38 | * @author David Costa <gurugeek@php.net> | |
39 | * @author Sterling Hughes <sterling@php.net> | |
40 | * @author Joe Stump <joe@joestump.net> | |
41 | * @author Philippe Jausions <jausions@php.net> | |
42 | * @copyright 1997-2008 The PHP Group | |
43 | * @license http://www.opensource.org/licenses/bsd-license.php New BSD License | |
44 | * @version CVS: $Revision: 1.15 $ | |
45 | * @link http://pear.php.net/package/Net_Curl | |
46 | */ | |
47 | ||
48 | /** | |
49 | * Include PEAR package for error handling | |
50 | */ | |
51 | require_once 'PEAR.php'; | |
52 | ||
53 | /** | |
54 | * Object-oriented implementation of the Curl extension | |
55 | * | |
56 | * @category Net | |
57 | * @package Net_Curl | |
58 | * @author David Costa <gurugeek@php.net> | |
59 | * @author Sterling Hughes <sterling@php.net> | |
60 | * @author Joe Stump <joe@joestump.net> | |
61 | * @author Philippe Jausions <jausions@php.net> | |
62 | * @license http://www.opensource.org/licenses/bsd-license.php New BSD License | |
63 | * @link http://pear.php.net/package/Net_Curl | |
64 | */ | |
65 | class Net_Curl | |
66 | { | |
67 | // {{{ Public Properties | |
68 | /** | |
69 | * The URL for cURL to work with | |
70 | * | |
71 | * @var string $url | |
72 | * @access public | |
73 | */ | |
74 | var $url; | |
75 | ||
76 | /** | |
77 | * The Username for standard HTTP Authentication | |
78 | * | |
79 | * @var string $username | |
80 | * @access public | |
81 | */ | |
82 | var $username = ''; | |
83 | ||
84 | /** | |
85 | * The Password for standard HTTP Authentication | |
86 | * | |
87 | * @var string $password | |
88 | * @access public | |
89 | */ | |
90 | var $password = ''; | |
91 | ||
92 | /** | |
93 | * The SSL version for the transfer | |
94 | * | |
95 | * @var integer $sslVersion | |
96 | * @access public | |
97 | */ | |
98 | var $sslVersion; | |
99 | ||
100 | /** | |
101 | * The filename of the SSL certificate | |
102 | * | |
103 | * @var string $sslCert | |
104 | * @access public | |
105 | */ | |
106 | var $sslCert; | |
107 | ||
108 | /** | |
109 | * The password corresponding to the certificate | |
110 | * in the $sslCert property | |
111 | * | |
112 | * @var string $sslCertPasswd | |
113 | * @access public | |
114 | */ | |
115 | var $sslCertPasswd; | |
116 | ||
117 | /** | |
118 | * User Agent string when making an HTTP request | |
119 | * | |
120 | * @var string $userAgent | |
121 | * @access public | |
122 | */ | |
123 | var $userAgent; | |
124 | ||
125 | /** | |
126 | * Whether or not to include the header in the results | |
127 | * of the CURL transfer | |
128 | * | |
129 | * @var boolean $header | |
130 | */ | |
131 | var $header = false; | |
132 | ||
133 | /** | |
134 | * Whether or not to output debug information while executing a | |
135 | * curl transfer | |
136 | * | |
137 | * @var boolean $verbose | |
138 | * @access public | |
139 | */ | |
140 | var $verbose = false; | |
141 | ||
142 | /** | |
143 | * Whether or not to display a progress meter for the current transfer | |
144 | * | |
145 | * @var boolean $progress | |
146 | * @access public | |
147 | */ | |
148 | var $progress = false; | |
149 | ||
150 | /** | |
151 | * Whether or not to suppress error messages | |
152 | * | |
153 | * @var boolean $mute | |
154 | * @access public | |
155 | */ | |
156 | var $mute = false; | |
157 | ||
158 | /** | |
159 | * Whether or not to follow HTTP Location headers. | |
160 | * | |
161 | * @var boolean $followLocation | |
162 | * @access public | |
163 | */ | |
164 | var $followLocation = true; | |
165 | ||
166 | /** | |
167 | * Whether or not to follow HTTP Location headers. | |
168 | * | |
169 | * @var boolean $follow_location | |
170 | * @access public | |
171 | * @deprecated | |
172 | */ | |
173 | var $follow_location = false; | |
174 | ||
175 | /** | |
176 | * Time allowed for current transfer, in seconds. 0 means no limit | |
177 | * | |
178 | * @var int $timeout | |
179 | * @access public | |
180 | */ | |
181 | var $timeout = 0; | |
182 | ||
183 | /** | |
184 | * Whether or not to return the results of the | |
185 | * current transfer | |
186 | * | |
187 | * @var boolean $returnTransfer | |
188 | * @access public | |
189 | */ | |
190 | var $returnTransfer = true; | |
191 | ||
192 | /** | |
193 | * Whether or not to return the results of the | |
194 | * current transfer | |
195 | * | |
196 | * @var boolean $return_transfer | |
197 | * @access public | |
198 | * @deprecated | |
199 | */ | |
200 | var $return_transfer = false; | |
201 | ||
202 | /** | |
203 | * The type of transfer to perform (ie. 'POST', 'GET', 'PUT', etc) | |
204 | * | |
205 | * @var string $type | |
206 | * @access public | |
207 | */ | |
208 | var $type; | |
209 | ||
210 | /** | |
211 | * The file to upload (PUT, or FTP methods) | |
212 | * | |
213 | * @var string $file | |
214 | * @access public | |
215 | */ | |
216 | var $file; | |
217 | ||
218 | /** | |
219 | * The file size of the file pointed to by the $file | |
220 | * property | |
221 | * | |
222 | * @var integer $fileSize | |
223 | * @access public | |
224 | */ | |
225 | var $fileSize; | |
226 | ||
227 | /** | |
228 | * The file size of the file pointed to by the $file | |
229 | * property | |
230 | * | |
231 | * @var integer $file_size | |
232 | * @access public | |
233 | * @deprecated | |
234 | */ | |
235 | var $file_size = false; | |
236 | ||
237 | ||
238 | /** | |
239 | * The cookies to send to the remote site | |
240 | * | |
241 | * @var array $cookies | |
242 | * @access public | |
243 | */ | |
244 | var $cookies = array(); | |
245 | ||
246 | /** | |
247 | * Additional HTTP headers to send to the remote site | |
248 | * | |
249 | * @var array $httpHeaders | |
250 | * @access public | |
251 | */ | |
252 | var $httpHeaders = null; | |
253 | ||
254 | /** | |
255 | * Additional HTTP headers to send to the remote site | |
256 | * | |
257 | * @var array $http_headers | |
258 | * @access public | |
259 | * @deprecated | |
260 | */ | |
261 | var $http_headers = false; | |
262 | ||
263 | /** | |
264 | * The fields to send in a 'POST' request | |
265 | * | |
266 | * @var array $fields | |
267 | * @access public | |
268 | */ | |
269 | var $fields; | |
270 | ||
271 | /** | |
272 | * The proxy server to go through | |
273 | * | |
274 | * @var string $proxy | |
275 | * @access public | |
276 | */ | |
277 | var $proxy; | |
278 | ||
279 | /** | |
280 | * The username for the Proxy server | |
281 | * | |
282 | * @var string $proxyUser | |
283 | * @access public | |
284 | */ | |
285 | var $proxyUser; | |
286 | ||
287 | /** | |
288 | * The password for the Proxy server | |
289 | * | |
290 | * @var string $proxyPassword | |
291 | * @access public | |
292 | */ | |
293 | var $proxyPassword; | |
294 | ||
295 | /** | |
296 | * $verifyPeer | |
297 | * | |
298 | * FALSE to stop CURL from verifying the peer's certificate. | |
299 | * Alternate certificates to verify against can be specified | |
300 | * with the CURLOPT_CAINFO option or a certificate directory | |
301 | * can be specified with the CURLOPT_CAPATH option. | |
302 | * CURLOPT_SSL_VERIFYHOST may also need to be TRUE or FALSE | |
303 | * if CURLOPT_SSL_VERIFYPEER is disabled (it defaults to 2). | |
304 | * | |
305 | * @var boolean $verifyPeer | |
306 | * @access public | |
307 | */ | |
308 | var $verifyPeer = true; | |
309 | ||
310 | /** | |
311 | * $verifyHost | |
312 | * | |
313 | * 0 : to stop CURL from verifying the host's certificate. | |
314 | * 1 : to check the existence of a common name in the SSL peer certificate. | |
315 | * 2 : to check the existence of a common name and also verify that it | |
316 | * matches the hostname provided. | |
317 | * | |
318 | * @var bool $verifyHost | |
319 | * @access public | |
320 | */ | |
321 | var $verifyHost = 2; | |
322 | ||
323 | /** | |
324 | * $caInfo | |
325 | * | |
326 | * Set value for CURLOPT_CAINFO. The name of a file holding one or more | |
327 | * certificates to verify the peer with. This only makes sense when used | |
328 | * in combination with CURLOPT_SSL_VERIFYPEER. curl-ca-bundle.crt is | |
329 | * avaible on the Curl website http://curl.haxx.se/ for download inside | |
330 | * the packages. | |
331 | * | |
332 | * @var string $caInfo | |
333 | * @access public | |
334 | */ | |
335 | var $caInfo = ''; | |
336 | ||
337 | /** | |
338 | * $caPath | |
339 | * | |
340 | * Set value for CURLOPT_CAPATH. A directory that holds multiple CA | |
341 | * certificates. Use this option alongside CURLOPT_SSL_VERIFYPEER. | |
342 | * | |
343 | * @var string $caPath | |
344 | * @access public | |
345 | */ | |
346 | var $caPath; | |
347 | // }}} | |
348 | // {{{ Private Properties | |
349 | /** | |
350 | * The current curl handle | |
351 | * | |
352 | * @var resource $_ch | |
353 | * @access private | |
354 | * @see Net_Curl::create() | |
355 | */ | |
356 | var $_ch = null; | |
357 | ||
358 | /** | |
359 | * The file upload resource | |
360 | * | |
361 | * The CURLOPT_INFILE requires a file resource and not just a file name. | |
362 | * This is used by execute to open the file. | |
363 | * | |
364 | * @var resource $_fp | |
365 | * @access private | |
366 | * @see Net_Curl::execute() | |
367 | */ | |
368 | var $_fp = null; | |
369 | // }}} | |
370 | ||
371 | // {{{ __construct($url = '', $userAgent = '') | |
372 | /** | |
373 | * The Net_Curl PHP 5.x constructor, called when a new Net_Curl object | |
374 | * is initialized (also called via 4.x constructor) | |
375 | * | |
376 | * @param string $url The URL to fetch (can be set using the $url | |
377 | * property as well) | |
378 | * @param string $userAgent The userAgent string (can be set using the | |
379 | * $userAgent property as well) | |
380 | * | |
381 | * @access public | |
382 | * @author Joe Stump <joe@joestump.net> | |
383 | * @return void | |
384 | */ | |
385 | function __construct($url = '', $userAgent = '') | |
386 | { | |
387 | if (is_string($url) && strlen($url)) { | |
388 | $this->url = $url; | |
389 | } | |
390 | ||
391 | if (is_string($userAgent) && strlen($userAgent)) { | |
392 | $this->userAgent = $userAgent; | |
393 | } | |
394 | } | |
395 | // }}} | |
396 | ||
397 | // {{{ Net_Curl($url = '', $userAgent = '') | |
398 | /** | |
399 | * Net_Curl | |
400 | * | |
401 | * PHP 4.x constructor. | |
402 | * | |
403 | * @param string $url The URL to fetch (can be set using the $url | |
404 | * property as well) | |
405 | * @param string $userAgent The userAgent string (can be set using the | |
406 | * $userAgent property as well) | |
407 | * | |
408 | * @access public | |
409 | * @return void | |
410 | */ | |
411 | function Net_Curl($url = '', $userAgent = '') | |
412 | { | |
413 | $this->__construct($url, $userAgent); | |
414 | } | |
415 | // }}} | |
416 | ||
417 | // {{{ execute() | |
418 | /** | |
419 | * Executes a prepared CURL transfer | |
420 | * | |
421 | * Run this function to execute your cURL request. If all goes well you | |
422 | * should get a string (the output from the remote host regarding your | |
423 | * request) or true (if you choose to output directly to the browser). If | |
424 | * something fails then PEAR_Error is returned. | |
425 | * | |
426 | * <code> | |
427 | * <?php | |
428 | * require_once 'Net/Curl.php'; | |
429 | * | |
430 | * $curl = new Net_Curl('http://www.example.com'); | |
431 | * $curl->fields = array('foo' => '1', 'bar' => 'apple'); | |
432 | * $result = $curl->execute(); | |
433 | * if (!PEAR::isError($result)) { | |
434 | * echo $result; | |
435 | * } | |
436 | * ?> | |
437 | * </code> | |
438 | * | |
439 | * @access public | |
440 | * @author Sterling Hughes <sterling@php.net> | |
441 | * @author Joe Stump <joe@joestump.net> | |
442 | * @return PEAR_Error on failure, true/result on success | |
443 | * @since PHP 4.0.5 | |
444 | */ | |
445 | function execute() | |
446 | { | |
447 | // Create cURL handle if it hasn't already been created | |
448 | if (!is_resource($this->_ch)) { | |
449 | $result = $this->create(); | |
450 | if (PEAR::isError($result)) { | |
451 | return $result; | |
452 | } | |
453 | } | |
454 | ||
455 | // Map the deprecated variables and throw a bunch of errors | |
456 | $this->_mapDeprecatedVariables(); | |
457 | ||
458 | // Default return value is true. | |
459 | $ret = true; | |
460 | ||
461 | // Basic stuff | |
462 | $ret = curl_setopt($this->_ch, CURLOPT_URL, $this->url); | |
463 | $ret = curl_setopt($this->_ch, CURLOPT_HEADER, $this->header); | |
464 | ||
465 | // Whether or not to return the transfer contents | |
466 | if ($this->returnTransfer === true || $this->mute === true) { | |
467 | $ret = curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, true); | |
468 | } | |
469 | ||
470 | // HTTP Authentication | |
471 | if ($this->username != '') { | |
472 | $ret = curl_setopt($this->_ch, | |
473 | CURLOPT_USERPWD, | |
474 | $this->username . ':' . $this->password); | |
475 | } | |
476 | ||
477 | // SSL Checks | |
478 | if (isset($this->sslVersion)) { | |
479 | $ret = curl_setopt($this->_ch, | |
480 | CURLOPT_SSLVERSION, | |
481 | $this->sslVersion); | |
482 | } | |
483 | ||
484 | if (isset($this->sslCert)) { | |
485 | $ret = curl_setopt($this->_ch, CURLOPT_SSLCERT, $this->sslCert); | |
486 | } | |
487 | ||
488 | if (isset($this->sslCertPasswd)) { | |
489 | $ret = curl_setopt($this->_ch, | |
490 | CURLOPT_SSLCERTPASSWD, | |
491 | $this->sslCertPasswd); | |
492 | } | |
493 | ||
494 | // Proxy Related checks | |
495 | if (isset($this->proxy)) { | |
496 | $ret = curl_setopt($this->_ch, CURLOPT_PROXY, $this->proxy); | |
497 | } | |
498 | ||
499 | if (isset($this->proxyUser) || isset($this->proxyPassword)) { | |
500 | $ret = curl_setopt($this->_ch, | |
501 | CURLOPT_PROXYUSERPWD, | |
502 | $this->proxyUser . ':' . $this->proxyPassword); | |
503 | } | |
504 | ||
505 | if (is_bool($this->verifyPeer)) { | |
506 | if (!$this->setOption(CURLOPT_SSL_VERIFYPEER, $this->verifyPeer)) { | |
507 | return PEAR::raiseError('Error setting CURLOPT_SSL_VERIFYPEER'); | |
508 | } | |
509 | } | |
510 | ||
511 | if (is_numeric($this->verifyHost) && $this->verifyHost >= 0 && | |
512 | $this->verifyHost <= 2) { | |
513 | if (!$this->setOption(CURLOPT_SSL_VERIFYHOST, $this->verifyHost)) { | |
514 | return PEAR::raiseError('Error setting CURLOPT_SSL_VERIFYPEER'); | |
515 | } | |
516 | } | |
517 | ||
518 | if (is_bool($this->verifyPeer) && $this->verifyPeer == true) { | |
519 | if (isset($this->caInfo) && strlen($this->caInfo)) { | |
520 | if (file_exists($this->caInfo)) { | |
521 | if (!$this->setOption(CURLOPT_CAINFO, $this->caInfo)) { | |
522 | return PEAR::raiseError('Error setting CURLOPT_CAINFO'); | |
523 | } | |
524 | } else { | |
525 | return PEAR::raiseError('Could not find CA info: '. | |
526 | $this->caInfo); | |
527 | } | |
528 | } | |
529 | ||
530 | if (isset($this->caPath) && is_string($this->caPath)) { | |
531 | if (!$this->setOption(CURLOPT_CAPATH, $this->caPath)) { | |
532 | return PEAR::raiseError('Error setting CURLOPT_CAPATH'); | |
533 | } | |
534 | } | |
535 | } | |
536 | ||
537 | // Transfer type | |
538 | if (isset($this->type)) { | |
539 | switch (strtolower($this->type)) { | |
540 | case 'post': | |
541 | $ret = curl_setopt($this->_ch, CURLOPT_POST, true); | |
542 | break; | |
543 | case 'put': | |
544 | $ret = curl_setopt($this->_ch, CURLOPT_PUT, true); | |
545 | break; | |
546 | } | |
547 | } | |
548 | ||
549 | // Transfer upload, etc. related | |
550 | if (isset($this->file)) { | |
551 | if (!file_exists($this->file)) { | |
552 | return PEAR::raiseError('File does not exist: '.$this->file); | |
553 | } | |
554 | ||
555 | $this->_fp = fopen($this->file, 'r'); | |
556 | if (!is_resource($this->_fp)) { | |
557 | return PEAR::raiseError('Could not open file: '.$this->file); | |
558 | } | |
559 | ||
560 | if (!isset($this->fileSize)) { | |
561 | $this->fileSize = filesize($this->file); | |
562 | } | |
563 | ||
564 | $ret = curl_setopt($this->_ch, CURLOPT_INFILE, $this->_fp); | |
565 | $ret = curl_setopt($this->_ch, CURLOPT_INFILESIZE, $this->fileSize); | |
566 | $ret = curl_setopt($this->_ch, CURLOPT_UPLOAD, true); | |
567 | } | |
568 | ||
569 | if (isset($this->fields)) { | |
570 | $sets = null; | |
571 | if (!isset($this->type)) { | |
572 | $this->type = 'post'; | |
573 | $ret = curl_setopt($this->_ch, CURLOPT_POST, true); | |
574 | } | |
575 | ||
576 | // If fields is an array then turn it into a string. Sometimes | |
577 | // cURL doesn't like fields as an array. | |
578 | // Exception: if a value is prefixed with "@" and the rest of the | |
579 | // value resolves to an existing file, then pass | |
580 | // the values as the original array. | |
581 | if (is_array($this->fields)) { | |
582 | $sets = array(); | |
583 | foreach ($this->fields as $key => $val) { | |
584 | if (strlen($val) > 1 && $val{0} == '@') { | |
585 | $file = substr($val, 1); | |
586 | if (is_file($file) && is_readable($file)) { | |
587 | $sets = null; | |
588 | break; | |
589 | } | |
590 | } | |
591 | $sets[] = urlencode($key) . '=' . urlencode($val); | |
592 | } | |
593 | } | |
594 | ||
595 | if (!is_null($sets)) { | |
596 | $fields = implode('&', $sets); | |
597 | } else { | |
598 | $fields = $this->fields; | |
599 | } | |
600 | $ret = curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $fields); | |
601 | } | |
602 | ||
603 | // Error related | |
604 | if ($this->progress === true) { | |
605 | $ret = curl_setopt($this->_ch, CURLOPT_PROGRESS, true); | |
606 | } | |
607 | ||
608 | if ($this->verbose === true) { | |
609 | $ret = curl_setopt($this->_ch, CURLOPT_VERBOSE, true); | |
610 | } | |
611 | ||
612 | // If a Location: header is passed then follow it | |
613 | $ret = curl_setopt($this->_ch, | |
614 | CURLOPT_FOLLOWLOCATION, | |
615 | $this->followLocation); | |
616 | ||
617 | // If a timeout is set and is greater then zero then set it | |
618 | if (is_numeric($this->timeout) && $this->timeout > 0) { | |
619 | $ret = curl_setopt($this->_ch, CURLOPT_TIMEOUT, $this->timeout); | |
620 | } | |
621 | ||
622 | if (isset($this->userAgent)) { | |
623 | $ret = curl_setopt($this->_ch, CURLOPT_USERAGENT, $this->userAgent); | |
624 | } | |
625 | ||
626 | // Cookies | |
627 | if (is_array($this->cookies) && count($this->cookies)) { | |
628 | $cookieData = ''; | |
629 | foreach ($this->cookies as $name => $value) { | |
630 | $cookieData .= $name . '=' . $value . ';'; | |
631 | } | |
632 | ||
633 | $ret = curl_setopt($this->_ch, CURLOPT_COOKIE, $cookieData); | |
634 | } | |
635 | ||
636 | // Other HTTP headers | |
637 | if ($this->httpHeaders !== null) { | |
638 | if (is_array($this->httpHeaders)) { | |
639 | $ret = curl_setopt($this->_ch, | |
640 | CURLOPT_HTTPHEADER, | |
641 | $this->httpHeaders); | |
642 | } else { | |
643 | return PEAR::raiseError('Net_Curl::$httpHeaders must be an array'); | |
644 | } | |
645 | } | |
646 | ||
647 | $ret = curl_exec($this->_ch); | |
648 | ||
649 | // Close the file before we return anything | |
650 | if (is_resource($this->_fp)) { | |
651 | fclose($this->_fp); | |
652 | } | |
653 | ||
654 | if (curl_errno($this->_ch)) { | |
655 | return PEAR::raiseError(curl_error($this->_ch), curl_errno($this->_ch)); | |
656 | } | |
657 | ||
658 | // Check to make sure we get a 2XX/3XX code and not a 404 or something. | |
659 | $info = $this->getInfo(); | |
660 | if (!isset($info['http_code'])) { | |
661 | return PEAR::raiseError('Unknown or invalid HTTP response'); | |
662 | } else { | |
663 | $type = substr($info['http_code'], 0, 1); | |
664 | if ($type != 2 && $type != 3) { | |
665 | return PEAR::raiseError('Unexpected HTTP code: ' . | |
666 | $info['http_code']); | |
667 | } | |
668 | } | |
669 | ||
670 | return $ret; | |
671 | } | |
672 | // }}} | |
673 | ||
674 | // {{{ setOption($option, $value) | |
675 | /** | |
676 | * Sets an option for your cURL session. Please note that the cURL handler | |
677 | * is NOT created before execute(). This is for error checking purposes. | |
678 | * You should use setOption() in the following manner: | |
679 | * | |
680 | * <code> | |
681 | * <?php | |
682 | * | |
683 | * require_once 'Net/Curl.php'; | |
684 | * $curl = new Net_Curl('http://www.example.com'); | |
685 | * $check = $curl->create(); | |
686 | * if (!PEAR::isError($check)) { | |
687 | * $curl->setOption(CURLOPT_FOO, 'bar'); | |
688 | * $result = $curl->execute(); | |
689 | * if (!PEAR::isError($result)) { | |
690 | * echo $result; | |
691 | * } | |
692 | * } | |
693 | * | |
694 | * ?> | |
695 | * </code> | |
696 | * | |
697 | * @param int $option cURL constant (ie. CURLOPT_URL) | |
698 | * @param mixed $value The option's value | |
699 | * | |
700 | * @author Joe Stump <joe@joestump.net> | |
701 | * @access public | |
702 | * @return boolean | |
703 | */ | |
704 | function setOption($option, $value) | |
705 | { | |
706 | if (is_resource($this->_ch)) { | |
707 | return curl_setopt($this->_ch, $option, $value); | |
708 | } | |
709 | ||
710 | return false; | |
711 | } | |
712 | // }}} | |
713 | ||
714 | // {{{ getInfo() | |
715 | /** | |
716 | * Returns the info from the cURL session. PEAR_Error if you try and run | |
717 | * this before you execute the session. | |
718 | * | |
719 | * @author Joe Stump <joe@joestump.net> | |
720 | * @access public | |
721 | * @return mixed PEAR_Error if there is no resource, info on success | |
722 | */ | |
723 | function getInfo() | |
724 | { | |
725 | if (is_resource($this->_ch)) { | |
726 | return curl_getinfo($this->_ch); | |
727 | } | |
728 | ||
729 | return PEAR::isError('cURL handler does not exist!'); | |
730 | } | |
731 | // }}} | |
732 | ||
733 | // {{{ create() | |
734 | /** | |
735 | * Creates a cURL resource. If curl_init() doesn't exist or we could not | |
736 | * create a resource it will error out. | |
737 | * | |
738 | * @author Joe Stump <joe@joestump.net> | |
739 | * @return boolean TRUE on success, PEAR_Error on failure | |
740 | */ | |
741 | function create() | |
742 | { | |
743 | if (!PEAR::loadExtension('curl')) { | |
744 | return PEAR::raiseError('CURL extension is not available'); | |
745 | } | |
746 | if (!function_exists('curl_init')) { | |
747 | return PEAR::raiseError('Function curl_init() not found'); | |
748 | } | |
749 | ||
750 | $this->_ch = curl_init(); | |
751 | if (!is_resource($this->_ch)) { | |
752 | return PEAR::raiseError('Could not initialize cURL handler'); | |
753 | } | |
754 | ||
755 | return true; | |
756 | } | |
757 | // }}} | |
758 | ||
759 | // {{{ verboseAll() | |
760 | /** | |
761 | * Sets verbose output | |
762 | * | |
763 | * Turns on super debugging mode by not suppressing errors, turning on | |
764 | * verbose mode, showing headers and displaying progress. | |
765 | * | |
766 | * @access public | |
767 | * @author David Costa <gurugeek@php.net> | |
768 | * @return void | |
769 | */ | |
770 | function verboseAll() | |
771 | { | |
772 | $this->verbose = true; | |
773 | $this->mute = false; | |
774 | $this->header = true; | |
775 | $this->progress = true; | |
776 | } | |
777 | // }}} | |
778 | ||
779 | // {{{ verbose_all() | |
780 | /** | |
781 | * Sets verbose output | |
782 | * | |
783 | * @access public | |
784 | * @author David Costa <gurugeek@php.net> | |
785 | * @return void | |
786 | * @deprecated | |
787 | */ | |
788 | function verbose_all() | |
789 | { | |
790 | $this->verboseAll(); | |
791 | PEAR::raiseError('Net_Curl::verbose_all() is deprecated! Please use Net_Curl::verboseAll()'." <br />\n", null, PEAR_ERROR_PRINT); | |
792 | } | |
793 | // }}} | |
794 | ||
795 | // {{{ close() | |
796 | /** | |
797 | * Closes the curl transfer and finishes the object (kinda ;) | |
798 | * | |
799 | * @access public | |
800 | * @author Sterling Hughes <sterling@php.net> | |
801 | * @return void | |
802 | * @since PHP 4.0.5 | |
803 | */ | |
804 | function close() | |
805 | { | |
806 | if (is_resource($this->_ch)) { | |
807 | curl_close($this->_ch); | |
808 | } | |
809 | } | |
810 | // }}} | |
811 | ||
812 | // {{{ _mapDeprecatedVariables() | |
813 | /** | |
814 | * Maps deprecated variables into the appropriate places. It also throws | |
815 | * the necessary notices. | |
816 | * | |
817 | * @author Joe Stump <joe@joestump.net> | |
818 | * @access private | |
819 | * @return void | |
820 | */ | |
821 | function _mapDeprecatedVariables() | |
822 | { | |
823 | $bad = array(); | |
824 | if ($this->follow_location !== false) { | |
825 | if ($this->follow_location > 0) { | |
826 | $this->followLocation = true; | |
827 | } else { | |
828 | $this->followLocation = false; | |
829 | } | |
830 | ||
831 | $bad[] = array('follow_location', 'followLocation'); | |
832 | } | |
833 | ||
834 | if ($this->return_transfer !== false) { | |
835 | if ($this->return_transfer > 0) { | |
836 | $this->returnTransfer = true; | |
837 | } else { | |
838 | $this->returnTransfer = false; | |
839 | } | |
840 | ||
841 | $bad[] = array('return_transfer', 'returnTransfer'); | |
842 | } | |
843 | ||
844 | if ($this->file_size !== false) { | |
845 | $this->fileSize = $this->file_size; | |
846 | $bad[] = array('file_size', 'fileSize'); | |
847 | } | |
848 | ||
849 | if ($this->http_headers !== false) { | |
850 | $this->httpHeaders = $this->http_headers; | |
851 | $bad[] = array('http_headers', 'httpHeaders'); | |
852 | } | |
853 | ||
854 | foreach ($bad as $map) { | |
855 | PEAR::raiseError('Net_Curl::$'. $map[0]. ' is deprecated! Please use Net_Curl::$'.$map[1]." instead! <br />\n", null, PEAR_ERROR_PRINT); | |
856 | } | |
857 | } | |
858 | // }}} | |
859 | ||
860 | // {{{ __destruct() | |
861 | /** | |
862 | * PHP 5.x destructor. | |
863 | * | |
864 | * Runs Net_Curl::close() to make sure we close our cURL connection. | |
865 | * | |
866 | * @author Joe Stump <joe@joestump.net> | |
867 | * @see Net_Curl::close() | |
868 | */ | |
869 | function __destruct() | |
870 | { | |
871 | $this->close(); | |
872 | } | |
873 | // }}} | |
874 | } | |
875 | ||
876 | ?> |