Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00036 include_once('Services/WebServices/Curl/classes/class.ilCurlConnectionException.php');
00037
00038 class ilCurlConnection
00039 {
00040 protected $url = '';
00041 protected $ch = null;
00042
00051 public function __construct($a_url = '')
00052 {
00053 $this->url = $a_url;
00054
00055 if(!self::_isCurlExtensionLoaded())
00056 {
00057 throw new ilCurlConnectionException('Curl extension not enabled.');
00058 }
00059 }
00060
00068 public static final function _isCurlExtensionLoaded()
00069 {
00070 if(!function_exists('curl_init'))
00071 {
00072 return false;
00073 }
00074 return true;
00075 }
00076
00084 public final function init()
00085 {
00086 if(strlen($this->url))
00087 {
00088 $this->ch = curl_init($this->url);
00089 }
00090 else
00091 {
00092 $this->ch = curl_init();
00093 }
00094 if(!$this->ch)
00095 {
00096 throw new ilCurlConnectionException('Cannot init curl connection.');
00097 }
00098 if(curl_errno($this->ch))
00099 {
00100 throw new ilCurlConnectionException(curl_error($this->ch),curl_errno($this->ch));
00101 }
00102
00103 return true;
00104 }
00105
00115 public final function setOpt($a_option,$a_value)
00116 {
00117 if(!@curl_setopt($this->ch,$a_option,$a_value))
00118 {
00119 throw new ilCurlConnectionException('Invalid option given for: '.$a_option,curl_errno($this->ch));
00120 }
00121 return true;
00122 }
00123
00131 public final function exec()
00132 {
00133 if((@$res = curl_exec($this->ch)) === false)
00134 {
00135 if(strlen($err = curl_error($this->ch)))
00136 {
00137 throw new ilCurlConnectionException($err,curl_errno($this->ch));
00138 }
00139 else
00140 {
00141 throw new ilCurlConnectionException('Error calling curl_exec().');
00142 }
00143 }
00144 return $res;
00145 }
00146
00153 public final function close()
00154 {
00155 if($this->ch != null)
00156 {
00157 curl_close($this->ch);
00158 }
00159 }
00160
00168 public function __destruct()
00169 {
00170 $this->close();
00171 }
00172 }
00173
00174 ?>