PHP封装CURL扩展类实例


Posted in PHP onJuly 28, 2015

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

<?php
/**
* @description: 封装CURL扩展
* @date: 2014-07-28 16:04
*/
/**
* @编码规范
* @class 类名首字母大写,类名为多个单词, 每个大字首字母大写 eg: class Curl , class CurlPage
* @variable 变量名小写, 变量名为多个单词, 每个单词小写,使用下划线_分割 eg: $curl_result
* @function 函数名与类名规则相同 eg: function SendRequest
* @params 函数形参规则与变量名相同
* @class-variable 成员变量,以下划线结尾,多个单词使用下划线分隔. eg: private $host_name_
*/
/**
* @要求
*
*/
class Curl{
/**
* @请求的host
*/
private $host_;
/**
* @curl 句柄
*/
private $ch_;
/**
* @超时限制时间
*/
const time_=5;
/**
* @请求的设置
*/
private $options_;
/**
* @保存请求头信息
*/
private $request_header_;
/**
* @保存响应头信息
*/
private $response_header_;
/**
* @body_ 用于保存curl请求返回的结果
*/
private $body_;
/**
* @读取cookie
*/
private $cookie_file_;
/**
* @写入cookie
*/
private $cookie_jar_;
/**
* @todo proxy
* @构造函数,初始化CURL回话
*/
public function Start($url){
$this->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_;
}
}

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

PHP 相关文章推荐
Smarty安装配置方法
Apr 10 PHP
Pain 全世界最小最简单的PHP模板引擎 (普通版)
Oct 23 PHP
php中explode与split的区别介绍
Oct 03 PHP
基于php socket(fsockopen)的应用实例分析
Jun 02 PHP
php去除HTML标签实例
Nov 06 PHP
PHP删除数组中空值的方法介绍
Apr 14 PHP
php+xml编程之xpath的应用实例
Jan 24 PHP
10个对初学者非常有用的PHP技巧
Apr 06 PHP
浅谈php和js中json的编码和解码
Oct 24 PHP
PHP之认识(二)关于Traits的用法详解
Apr 11 PHP
PHP判断一个变量是否为整数、正整数的方法示例
Sep 11 PHP
Laravel + Elasticsearch 实现中文搜索的方法
Feb 02 PHP
php图像处理类实例
Jul 28 #PHP
图文介绍PHP添加Redis模块及连接
Jul 28 #PHP
PHP生成树的方法
Jul 28 #PHP
php计算税后工资的方法
Jul 28 #PHP
怎样搭建PHP开发环境
Jul 28 #PHP
php递归实现无限分类的方法
Jul 28 #PHP
php类自动加载器实现方法
Jul 28 #PHP
You might like
PHP form 表单传参明细研究
2009/07/17 PHP
PHP保留两位小数的几种方法
2019/07/24 PHP
JavaScript 对象链式操作测试代码
2010/04/25 Javascript
使用jQuery向asp.net Mvc传递复杂json数据-ModelBinder篇
2010/05/07 Javascript
JavaScript 获取当前时间戳的代码
2010/08/05 Javascript
纯JS实现动态时间显示代码
2014/02/08 Javascript
从零学jquery之如何使用回调函数
2014/05/16 Javascript
JavaScript模块化开发之SeaJS
2015/12/13 Javascript
微信开发 微信授权详解
2016/10/21 Javascript
js中利用cookie实现记住密码功能
2020/08/20 Javascript
JS中判断null的方法分析
2016/11/21 Javascript
手动初始化Angular的模块与控制器
2016/12/26 Javascript
ReactJs实现树形结构的数据显示的组件的示例
2017/08/18 Javascript
p5.js入门教程之图片加载
2018/03/20 Javascript
vue项目开发中setTimeout等定时器的管理问题
2018/09/13 Javascript
vue项目中,main.js,App.vue,index.html的调用方法
2018/09/20 Javascript
Nodejs中怎么实现函数的串行执行
2019/03/02 NodeJs
微信公众号平台接口开发 获取微信服务器IP地址方法解析
2019/08/14 Javascript
a标签调用js的方法总结
2019/09/05 Javascript
JavaScript实现Tab选项卡切换
2020/02/13 Javascript
JS面向对象实现飞机大战
2020/08/26 Javascript
[01:48]2018DOTA2亚洲邀请赛主赛事第二日五佳镜头 VG完美团战逆转TNC
2018/04/05 DOTA
深入理解Django中内置的用户认证
2017/10/06 Python
Python学习笔记之自定义函数用法详解
2019/06/08 Python
python的scipy实现插值的示例代码
2019/11/12 Python
pytorch 状态字典:state_dict使用详解
2020/01/17 Python
Django admin组件的使用
2020/10/24 Python
移动端Html5中百度地图的点击事件
2019/01/31 HTML / CSS
连卡佛中国官网:Lane Crawford中文站
2018/01/27 全球购物
阿德的梦教学反思
2014/02/06 职场文书
平面设计专业大学生职业规划书
2014/03/12 职场文书
客户经理竞聘演讲稿
2014/05/15 职场文书
保护动物的标语
2014/06/11 职场文书
求职信的正确写法
2014/07/10 职场文书
教师学习群众路线心得体会
2014/11/04 职场文书
Python中的turtle画箭头,矩形,五角星
2022/03/16 Python