• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP封装CURL扩展类实例_PHP

php 搞代码 4年前 (2022-01-25) 12次浏览 已收录 0个评论

本文实例讲述了PHP封装CURL扩展类。分享给大家供大家参考。具体如下:

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 (strpo<mark>(本文来)源gaodaimacom搞#^代%!码&网(</mark><pre>搞gaodaima代码

s($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_;}}

希望本文所述对大家的php程序设计有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP封装CURL扩展类实例_PHP
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址