RSI helpers  0.1
RSI helpers
Curl.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Wrapper;
4 
5 /**
6  * cURL wrapper.
7  */
8 class Curl{
9 
10  protected $_handle = null;
11  protected $_constants = null;
12  protected $_options = [];
13  protected $_infos = [];
14 
15  public function __construct($url = null,$return_transfer = true,$follow_location = true){
16  if($this->_handle = curl_init($url)){
17  if($return_transfer) $this->returntransfer = true;
18  if($follow_location) $this->followlocation = true;
19  }
20  }
21 
22  public function __destruct(){
23  $this->close();
24  }
25 
26  public function close(){
27  if($this->_handle) curl_close($this->_handle);
28  $this->_handle = null;
29  }
30 
31  protected function getConstants(){
32  if($this->_constants === null) $this->_constants = \Rsi\Record::get(get_defined_constants(true),'curl');
33  return $this->_constants;
34  }
35 
36  public function getOptions(){
37  if(!$this->_options) foreach($this->getConstants() as $name => $value)
38  if(substr($name,0,8) == 'CURLOPT_') $this->_options[lcfirst(\Rsi\Str::camel(strtolower(substr($name,8))))] = $value;
39  return $this->_options;
40  }
41 
42  public function getInfos(){
43  if(!$this->_infos) foreach($this->getConstants() as $name => $value)
44  if(substr($name,0,9) == 'CURLINFO_') $this->_infos[lcfirst(\Rsi\Str::camel(strtolower(substr($name,9))))] = $value;
45  return $this->_infos;
46  }
47 
48  public function __set($key,$value){
49  if(!array_key_exists($key,$this->getOptions())) throw new \Exception("Unknown option '$key'");
50  $this->setopt($this->_options[$key],$value);
51  }
52 
53  public function __get($key){
54  if(!array_key_exists($key,$this->getInfos())) throw new \Exception("Unknown info '$key'");
55  return $this->getinfo($this->_infos[$key]);
56  }
57 
58  public function __call($func_name,$params){
59  return call_user_func_array('curl_' . \Rsi\Str::snake($func_name),array_merge([$this->_handle],$params));
60  }
61 
62 }
__construct($url=null, $return_transfer=true, $follow_location=true)
Definition: Curl.php:15
__get($key)
Definition: Curl.php:53
Definition: Color.php:3
static camel($str, $delimiters=' -_')
Converts a delimited string to CamelCase.
Definition: Str.php:184
static snake($str, $delimiter='_')
Converts a CamelCased string to snake_case.
Definition: Str.php:193
__call($func_name, $params)
Definition: Curl.php:58
cURL wrapper.
Definition: Curl.php:8
__set($key, $value)
Definition: Curl.php:48