PHP封装CURL扩展类实例
php  /  管理员 发布于 7年前   141
本文实例讲述了PHP封装CURL扩展类。分享给大家供大家参考。具体如下: 希望本文所述对大家的php程序设计有所帮助。ch_ = curl_init($url);curl_setopt($this->ch_, CURLOPT_HEADER, 1);curl_setopt($this->ch_, CURLOPT_RETURNTRANSFER , 1 );}/*** @返回响应头*/public function ResponseHeader($url){if (!function_exists('http_parse_headers')) {function http_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_);}/*** @读取cookie*/public function LoadCookie($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_);}/*** @写入cookie*/public function SaveCookie($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_);}/*** @设置HEADER*/public function SetHeader($headers = null){if (is_array($headers) && count($headers) > 0) {curl_setopt($this->ch_, CURLOPT_HTTPHEADER, $headers);}}/*** @GET请求*/public function Get($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请求*/public function Post($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 方法*/public function Head($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_);}/*** @执行CURL会话*/public function Execx(){return curl_exec($this->ch_);}/*** @关闭CURL句柄*/public function Close($body_){if ($body_ === false) {echo "CURL Error: " . curl_error($body_);return false;}curl_close($this->ch_);return $body_;}}
您可能感兴趣的文章:
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号