Exemple #1
0
 /**
  * Sets CURLOPT_SSL_VERIFYPEER
  *
  * @param bool
  * @return this
  */
 public function verifyPeer($on = true)
 {
     Eden_Curl_Error::i()->argument(1, 'bool');
     $this->_options[CURLOPT_SSL_VERIFYPEER] = $on;
     return $this;
 }
Exemple #2
0
if(!class_exists('Eden_Curl')){class Eden_Curl extends Eden_Class implements ArrayAccess{const PUT='PUT';const DELETE='DELETE';const GET='GET';const POST='POST';protected $_options=array();protected $_meta=array();protected $_query=array();protected $_headers=array();protected static $_setBoolKeys=array( 'AutoReferer'=>CURLOPT_AUTOREFERER,'BinaryTransfer'=>CURLOPT_BINARYTRANSFER,'CookieSession'=>CURLOPT_COOKIESESSION,'CrlF'=>CURLOPT_CRLF,'DnsUseGlobalCache'=>CURLOPT_DNS_USE_GLOBAL_CACHE,'FailOnError'=>CURLOPT_FAILONERROR,'FileTime'=>CURLOPT_FILETIME,'FollowLocation'=>CURLOPT_FOLLOWLOCATION,'ForbidReuse'=>CURLOPT_FORBID_REUSE,'FreshConnect'=>CURLOPT_FRESH_CONNECT,'FtpUseEprt'=>CURLOPT_FTP_USE_EPRT,'FtpUseEpsv'=>CURLOPT_FTP_USE_EPSV,'FtpAppend'=>CURLOPT_FTPAPPEND,'FtpListOnly'=>CURLOPT_FTPLISTONLY,'Header'=>CURLOPT_HEADER,'HeaderOut'=>CURLINFO_HEADER_OUT,'HttpGet'=>CURLOPT_HTTPGET,'HttpProxyTunnel'=>CURLOPT_HTTPPROXYTUNNEL,'Netrc'=>CURLOPT_NETRC,'Nobody'=>CURLOPT_NOBODY,'NoProgress'=>CURLOPT_NOPROGRESS,'NoSignal'=>CURLOPT_NOSIGNAL,'Post'=>CURLOPT_POST,'Put'=>CURLOPT_PUT,'ReturnTransfer'=>CURLOPT_RETURNTRANSFER,'SslVerifyPeer'=>CURLOPT_SSL_VERIFYPEER,'TransferText'=>CURLOPT_TRANSFERTEXT,'UnrestrictedAuth'=>CURLOPT_UNRESTRICTED_AUTH,'Upload'=>CURLOPT_UPLOAD,'Verbose'=>CURLOPT_VERBOSE);protected static $_setIntegerKeys=array( 'BufferSize'=>CURLOPT_BUFFERSIZE,'ClosePolicy'=>CURLOPT_CLOSEPOLICY,'ConnectTimeout'=>CURLOPT_CONNECTTIMEOUT,'ConnectTimeoutMs'=>CURLOPT_CONNECTTIMEOUT_MS,'DnsCacheTimeout'=>CURLOPT_DNS_CACHE_TIMEOUT,'FtpSslAuth'=>CURLOPT_FTPSSLAUTH,'HttpVersion'=>CURLOPT_HTTP_VERSION,'HttpAuth'=>CURLOPT_HTTPAUTH,'InFileSize'=>CURLOPT_INFILESIZE,'LowSpeedLimit'=>CURLOPT_LOW_SPEED_LIMIT,'LowSpeedTime'=>CURLOPT_LOW_SPEED_TIME,'MaxConnects'=>CURLOPT_MAXCONNECTS,'MaxRedirs'=>CURLOPT_MAXREDIRS,'Port'=>CURLOPT_PORT,'ProxyAuth'=>CURLOPT_PROXYAUTH,'ProxyPort'=>CURLOPT_PROXYPORT,'ProxyType'=>CURLOPT_PROXYTYPE,'ResumeFrom'=>CURLOPT_RESUME_FROM,'SslVerifyHost'=>CURLOPT_SSL_VERIFYHOST,'SslVersion'=>CURLOPT_SSLVERSION,'TimeCondition'=>CURLOPT_TIMECONDITION,'Timeout'=>CURLOPT_TIMEOUT,'TimeoutMs'=>CURLOPT_TIMEOUT_MS,'TimeValue'=>CURLOPT_TIMEVALUE);protected static $_setStringKeys=array( 'CaInfo'=>CURLOPT_CAINFO,'CaPath'=>CURLOPT_CAPATH,'Cookie'=>CURLOPT_COOKIE,'CookieFile'=>CURLOPT_COOKIEFILE,'CookieJar'=>CURLOPT_COOKIEJAR,'CustomRequest'=>CURLOPT_CUSTOMREQUEST,'EgdSocket'=>CURLOPT_EGDSOCKET,'Encoding'=>CURLOPT_ENCODING,'FtpPort'=>CURLOPT_FTPPORT,'Interface'=>CURLOPT_INTERFACE,'Krb4Level'=>CURLOPT_KRB4LEVEL,'PostFields'=>CURLOPT_POSTFIELDS,'Proxy'=>CURLOPT_PROXY,'ProxyUserPwd'=>CURLOPT_PROXYUSERPWD,'RandomFile'=>CURLOPT_RANDOM_FILE,'Range'=>CURLOPT_RANGE,'Referer'=>CURLOPT_REFERER,'SslCipherList'=>CURLOPT_SSL_CIPHER_LIST,'SslCert'=>CURLOPT_SSLCERT,'SslCertPassword'=>CURLOPT_SSLCERTPASSWD,'SslCertType'=>CURLOPT_SSLCERTTYPE,'SslEngine'=>CURLOPT_SSLENGINE,'SslEngineDefault'=>CURLOPT_SSLENGINE_DEFAULT,'Sslkey'=>CURLOPT_SSLKEY,'SslKeyPasswd'=>CURLOPT_SSLKEYPASSWD,'SslKeyType'=>CURLOPT_SSLKEYTYPE,'Url'=>CURLOPT_URL,'UserAgent'=>CURLOPT_USERAGENT,'UserPwd'=>CURLOPT_USERPWD);protected static $_setArrayKeys=array( 'Http200Aliases'=>CURLOPT_HTTP200ALIASES,'HttpHeader'=>CURLOPT_HTTPHEADER,'PostQuote'=>CURLOPT_POSTQUOTE,'Quote'=>CURLOPT_QUOTE);protected static $_setFileKeys=array( 'File'=>CURLOPT_FILE,'InFile'=>CURLOPT_INFILE,'StdErr'=>CURLOPT_STDERR,'WriteHeader'=>CURLOPT_WRITEHEADER);protected static $_setCallbackKeys=array( 'HeaderFunction'=>CURLOPT_HEADERFUNCTION,'ReadFunction'=>CURLOPT_READFUNCTION,'WriteFunction'=>CURLOPT_WRITEFUNCTION);public static function i(){return self::_getMultiple(__CLASS__);}public function __call($name,$args){if(strpos($name,'set')===0){$method=substr($name,3);if(isset(self::$_setBoolKeys[$method])){Eden_Curl_Error::i()->vargument($name,$args,1,'bool');$key=self::$_setBoolKeys[$method];$this->_options[$key]=$args[0];return $this;}if(isset(self::$_setIntegerKeys[$method])){Eden_Curl_Error::i()->vargument($name,$args,1,'int');$key=self::$_setIntegerKeys[$method];$this->_options[$key]=$args[0];return $this;}if(isset(self::$_setStringKeys[$method])){Eden_Curl_Error::i()->vargument($name,$args,1,'string');$key=self::$_setStringKeys[$method];$this->_options[$key]=$args[0];return $this;}if(isset(self::$_setArrayKeys[$method])){Eden_Curl_Error::i()->vargument($name,$args,1,'array');$key=self::$_setArrayKeys[$method];$this->_options[$key]=$args[0];return $this;}if(isset(self::$_setFileKeys[$method])){$key=self::$_setFileKeys[$method];$this->_options[$key]=$args[0];return $this;}if(isset(self::$_setCallbackKeys[$method])){Eden_Curl_Error::i()->vargument($name,$args,1,'array','string');$key=self::$_setCallbackKeys[$method];$this->_options[$key]=$args[0];return $this;}}parent::__call($name,$args);}public function getDomDocumentResponse(){$this->_meta['response']=$this->getResponse();$xml=new DOMDocument();$xml->loadXML($this->_meta['response']);return $xml;}public function getJsonResponse($assoc=true){$this->_meta['response']=$this->getResponse();Eden_Curl_Error::i()->argument(1,'bool');return json_decode($this->_meta['response'],$assoc);}public function getMeta($key=NULL){Eden_Curl_Error::i()->argument(1,'string','null');if(isset($this->_meta[$key])){return $this->_meta[$key];}return $this->_meta;}public function getQueryResponse(){$this->_meta['response']=$this->getResponse();parse_str($this->_meta['response'],$response);return $response;}public function getResponse(){$curl=curl_init();$this->_addParameters()->_addHeaders();$this->_options[CURLOPT_RETURNTRANSFER]=true;curl_setopt_array($curl,$this->_options);$response=curl_exec($curl);$this->_meta=array( 'info'=>curl_getinfo($curl,CURLINFO_HTTP_CODE),'error_message'=>curl_errno($curl),'error_code'=>curl_error($curl));curl_close($curl);unset($curl);return $response;}public function getSimpleXmlResponse(){$this->_meta['response']=$this->getResponse();return simplexml_load_string($this->_meta['response']);}public function offsetExists($offset){return isset($this->_option[$offset]);}public function offsetGet($offset){return isset($this->_option[$offset]) ? $this->_option[$offset] : NULL;}public function offsetSet($offset,$value){if (!is_null($offset)){if(in_array($offset,$this->_setBoolKeys)){$method=array_search($offset,$this->_setBoolKeys);$this->_call('set'.$method,array($value));}if(in_array($offset,$this->_setIntegerKeys)){$method=array_search($offset,$this->_setIntegerKeys);$this->_call('set'.$method,array($value));}if(in_array($offset,$this->_setStringKeys)){$method=array_search($offset,$this->_setStringKeys);$this->_call('set'.$method,array($value));}if(in_array($offset,$this->_setArrayKeys)){$method=array_search($offset,$this->_setArrayKeys);$this->_call('set'.$method,array($value));}if(in_array($offset,$this->_setFileKeys)){$method=array_search($offset,$this->_setFileKeys);$this->_call('set'.$method,array($value));}if(in_array($offset,$this->_setCallbackKeys)){$method=array_search($offset,$this->_setCallbackKeys);$this->_call('set'.$method,array($value));}}}public function offsetUnset($offset){unset($this->_option[$offset]);}public function send(){$curl=curl_init();$this->_addParameters()->_addHeaders();curl_setopt_array($curl,$this->_options);curl_exec($curl);$this->_meta=array( 'info'=>curl_getinfo($curl,CURLINFO_HTTP_CODE),'error_message'=>curl_errno($curl),'error_code'=>curl_error($curl));curl_close($curl);unset($curl);return $this;}public function setCustomGet(){$this->setCustomRequest(self::GET);return $this;}public function setCustomPost(){$this->setCustomRequest(self::POST);return $this;}public function setCustomPut(){$this->setCustomRequest(self::PUT);return $this;}public function setCustomDelete(){$this->setCustomRequest(self::DELETE);return $this;}public function setPostFields($fields){Eden_Curl_Error::i()->argument(1,'array','string');$this->_options[CURLOPT_POSTFIELDS]=$fields;return $this;}public function setHeaders($key,$value=NULL){Eden_Curl_Error::i()->argument(1,'array','string')->argument(2,'scalar','null');if(is_array($key)){$this->_headers=$key;return $this;}$this->_headers[]=$key.': '.$value;return $this;}public function setUrlParameter($key,$value=NULL){Eden_Curl_Error::i()->argument(1,'array','string')->argument(2,'scalar');if(is_array($key)){$this->_param=$key;return $this;}$this->_param[$key]=$value;}public function verifyHost($on=true){Eden_Curl_Error::i()->argument(1,'bool');$this->_options[CURLOPT_SSL_VERIFYHOST]=$on ? 1 : 2;return $this;}public function verifyPeer($on=true){Eden_Curl_Error::i()->argument(1,'bool');$this->_options[CURLOPT_SSL_VERIFYPEER]=$on;return $this;}protected function _addHeaders(){if(empty($this->_headers)){return $this;}$this->_options[CURLOPT_HTTPHEADER]=$this->_headers;return $this;}protected function _addParameters(){if(empty($this->_params)){return $this;}$params=http_build_query($this->_params);if($this->_options[CURLOPT_POST]){$this->_options[CURLOPT_POSTFIELDS]=$params;return $this;}if(strpos($this->_options[CURLOPT_URL],'?')===false){$params='?'.$params;}else if(substr($this->_options[CURLOPT_URL],-1,1) !='?'){$params='&'.$params;}$this->_options[CURLOPT_URL].=$params;return $this;}}class Eden_Curl_Error extends Eden_Error{public static function i($message=NULL,$code=0){$class=__CLASS__;return new $class($message,$code);}}}