123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- /**
- *
- * @copyright (c) 2012 University of Geneva
- * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
- * @author Laurent Opprecht <laurent@opprecht.info>
- */
- class Curl
- {
- protected static $default_options = array();
- static function get_default_options($options = array())
- {
- if (empty(self::$default_options)) {
- self::$default_options[CURLOPT_HEADER] = false;
- self::$default_options[CURLOPT_RETURNTRANSFER] = true;
- self::$default_options[CURLOPT_SSL_VERIFYPEER] = false;
- }
- $result = self::$default_options;
- foreach ($options as $key => $value) {
- $result[$key] = $value;
- }
- return $result;
- }
- static function set_default_option($key, $value)
- {
- $options = $this->get_options(array($key => $value));
- self::$default_options = $options;
- }
- /**
- *
- * @param string $url
- * @param array $options
- * @return Curl
- */
- static function get($url, $options = array())
- {
- $options[CURLOPT_HTTPGET] = true;
- $result = new self($url, $options);
- return $result;
- }
- /**
- *
- * @param string $url
- * @param array $fields
- * @param array $options
- * @return Curl
- */
- static function post($url, $fields, $options = array())
- {
- $options[CURLOPT_POST] = true;
- $options[CURLOPT_POSTFIELDS] = $fields;
- $result = new self($url, $options);
- return $result;
- }
- protected $url = '';
- protected $options = array();
- protected $content = '';
- protected $info = array();
- protected $error = '';
- protected $error_no = 0;
- function __construct($url, $options = array())
- {
- $this->url = $url;
- $this->options = self::get_default_options($options);
- }
- function url()
- {
- return $this->url;
- }
- function options()
- {
- return $this->options;
- }
- function execute()
- {
- $ch = curl_init();
- $options = $this->options;
- $options[CURLOPT_URL] = $this->url;
- curl_setopt_array($ch, $options);
- $this->content = curl_exec($ch);
- $this->error = curl_error($ch);
- $this->info = curl_getinfo($ch);
- $this->error_no = curl_errno($ch);
- curl_close($ch);
- return $this->content;
- }
- function content()
- {
- return $this->content;
- }
- /**
- * @return array|string
- */
- function info($key = false)
- {
- if ($key) {
- return isset($this->info[$key]) ? $this->info[$key] : false;
- } else {
- return $this->info;
- }
- }
- function error()
- {
- return $this->error;
- }
- function error_no()
- {
- return $this->error_no;
- }
- }
|