<?PHP/** * @description: Package Curl Extension * @date: 2014-07-28 16:04*/ /** * Code specification * @class class name capitalized, the class name is multiple words, each uppercase letter Eg:class Curl, class curlpage* @variable variable name lowercase, variable name is multiple words, each word lowercase, use underscore _ Cut eg: $curl _result* @function function name is the same as the class name rule eg:function sendrequest* @params function parameter rules are the same as variable names * @class-variable member variables, ending with an underscore, multiple words Use underscores to separate. Eg:private $host _name_*//** * required **/classcurl{/** * The host requested*/Private $host _; /** * @curl handle*/Private $ch _; /** * Timeout limit time*/ConstTime_=5;/** * The setting of the @ request*/Private $options _; /** * Save request header Information*/Private $request _header_; /** * Save response header information*/Private $response _header_; /** * @body_ to save results returned by Curl request*/Private $body _; /** * Read cookies*/Private $cookie _file_; /** * Write Cookie*/Private $cookie _jar_; /** * @todo proxy* @ constructor, initialize Curl reply*/ Public functionStart ($url){$this->ch_ = Curl_init ($url); curl_setopt ($this->ch_, Curlopt_header, 1); curl_setopt ($this->ch_, Curlopt_returntransfer, 1 );} /** * Return response header*/ Public functionResponseheader ($url){if(!function_exists(' Http_parse_headers ')) {functionHttp_parse_headers ($raw _headers){$headers=Array();foreach(Explode("\ n",$raw _headers) as $i=$h) {$h=Explode(‘:‘,$h, 2);if(isset($h[1])) {if(!isset($headers[$h[0]])) {$headers[$h[0]] =Trim($h[1]);} Else if(Is_array($headers[$h[0]])) {$tmp=Array_merge($headers[$h[0]],Array(Trim($h[1])));$headers[$h[0]] =$tmp;} Else {$tmp=Array_merge(Array($headers[$h[0]]),Array(Trim($h[1])));$headers[$h[0]] =$tmp;}}}return $headers;}}$this->start ($url); curl_setopt ($this->ch_, Curlopt_timeout, Curl::time_);$this->body_=$this-execx ();$header _size= Curl_getinfo ($this->ch_,curlinfo_header_size);$this->response_header_ =substr($this->body_,$start= 0,$offset=$header _size);$this->response_header_ = Http_parse_headers ($this-response_header_);Print_r($this-response_header_);return $this->close ($this-body_);}/** * Read cookies*/ Public functionLoadcookie ($url,$cookie _file){$this->start ($url); curl_setopt ($this->ch_, Curlopt_cookie, 1); curl_setopt ($this->ch_, Curlopt_cookiefile,$cookie _file);$this->body_=$this-execx ();return $this->close ($this-body_);} /** * Write Cookie*/ Public functionSavecookie ($url){$this->start ($url); curl_setopt ($this->ch_, Curlopt_cookie, 1); curl_setopt ($this->ch_, Curlopt_cookiefile, ' Cookie.txt '); curl_setopt ($this->ch_, Curlopt_cookiejar, ' Cookie.txt ');$this->body_=$this-execx ();return $this->close ($this-body_);} /** * Set Header*/ Public functionSetHeader ($headers=NULL){if(Is_array($headers) &&Count($headers) > 0) {curl_setopt ($this->ch_, Curlopt_httpheader,$headers);}} /** * @GET request*/ Public functionGet ($url,Array $params=Array()) {if($params) {if(Strpos($url, ‘?‘)) {$url. = "&".Http_build_query($params);}Else {$url.= "?".Http_build_query($params);}}$this->start ($url); curl_setopt ($this->ch_, Curlopt_timeout, Curl::time_);if(Strpos($url, ' https ') = = = 0) {curl_setopt ($this->ch_, Curlopt_ssl_verifyhost, 0); curl_setopt ($this->ch_, Curlopt_ssl_verifypeer, 0);}$this->body_=$this-execx ();return $this->close ($this-body_);} /** * @POST request*/ Public functionPost ($url,Array $params=Array()) {$this->start ($url); curl_setopt ($this->ch_, Curlopt_ssl_verifypeer, 0); curl_setopt ($this->ch_, Curlopt_httpheader,Array("content-type:application/x-www-form-urlencoded")); Curl_setopt ($this->ch_, Curlopt_post,true); curl_setopt ($this->ch_, Curlopt_timeout, Curl::time_);if($params) {curl_setopt ($this->ch_, Curlopt_postfields,Http_build_query($params));}$this->body_=$this-execx ();return $this->close ($this-body_);} /** * @tips: Google http head method*/ Public functionHead ($url,Array $params=Array()) {$this->start ($url); curl_setopt ($this->ch_, Curlopt_timeout, Curl::time_); curl_setopt ($this->ch_, Curlopt_returntransfer, 0); curl_setopt ($this->ch_,curlopt_nobody,true);$this->body_=$this-execx ();return $this->close ($this-body_);} /** * Perform a curl session*/ Public functionexecx () {returnCurl_exec ($this-ch_);} /** * Close curl handle*/ Public functionClose ($body _){if($body _===false) {Echo"CURL Error:". Curl_error ($body _);return false;} Curl_close ($this-Ch_);return $body _;}}
PHP Package Curl Extension