微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP封装CURL扩展类实例

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

rush: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,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_,'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_,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_,array("Content-Type: application/x-www-form-urlencoded")); curl_setopt($this->ch_,CURLOPT_POST,true); curl_setopt($this->ch_,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,Curl::time_); curl_setopt($this->ch_,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程序设计有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返回预支付订单号的接口,目前微信支付所有场景均使用这一接口。下面介绍的是其中NATIVE的支付实现流程与PC端实现扫码支付流程
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返回预支付订单号的接口,目前微信支付所有场景均使用这一接口。下面介绍的是其中APP的支付的配置与实现流程
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户信息这个功能的开发流程。 配置 1.首先得在微信公众平台申请一下微信小程序账号并获取到小程序的AppID和AppSecret https://mp.weixin.qq.com/cgi-bin/loginpage?url=%2Fwxamp%2F
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一款开源且免费商用的后台开发框架,它基于ThinkPHP和Bootstrap两大主流技术构建的极速后台开发框架,它有着非常完善且强大的功能和便捷的开发体验,使我逐渐喜欢上了它。
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛,就离不开通讯了,然后我就想到了长连接。这里本人用的是GatewayWorker框架。
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返回预支付订单号的接口,目前微信支付所有场景均使用这一接口。下面介绍的是其中JSAPI的支付实现流程
服务器优化必备:深入了解PHP8底层开发原理
Golang的网络编程:如何快速构建高性能的网络应用?
Golang和其他编程语言的对比:为什么它的开发效率更高?
PHP8底层开发原理揭秘:如何利用新特性创建出色的Web应用