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 相关文章推荐
PHP 危险函数全解析
Sep 09 PHP
PHP is_dir() 判断给定文件名是否是一个目录
May 10 PHP
php数组函数序列之array_combine() - 数组合并函数使用说明
Oct 29 PHP
解析php做推送服务端实现ios消息推送
Jul 01 PHP
如何使用PHP获取指定日期所在月的开始日期与结束日期
Aug 01 PHP
PHP IE中下载附件问题解决方法
Jan 07 PHP
ThinkPHP查询中的魔术方法简述
Jun 25 PHP
php输入数据统一类实例
Feb 23 PHP
php使用Jpgraph绘制3D饼状图的方法
Jun 10 PHP
Yii中的cookie的发送和读取
Jul 27 PHP
PHP目录与文件操作技巧总结(创建,删除,遍历,读写,修改等)
Sep 11 PHP
php异常处理捕获错误整理
Sep 23 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数组函数序列 之shuffle()和array_rand() 随机函数使用介绍
2011/10/29 PHP
用JavaScript事件串连执行多个处理过程的方法
2007/03/09 Javascript
jQuery textarea的长度进行验证
2009/05/06 Javascript
多个jquery.datatable共存,checkbox全选异常的快速解决方法
2013/12/10 Javascript
JS实现兼容各浏览器解析XML文档数据的方法
2015/06/01 Javascript
配置Grunt的Task时通配符支持和动态生成文件名问题
2015/09/06 Javascript
AngularJS使用ng-options指令实现下拉框
2016/08/23 Javascript
懒加载实现的分页&amp;&amp;网站footer自适应
2016/12/21 Javascript
JavaScript获取ul中li个数的方法
2017/02/13 Javascript
JS实现两周内自动登录功能
2017/03/23 Javascript
在vue中实现简单页面逆传值的方法
2017/11/27 Javascript
python3编写C/S网络程序实例教程
2014/08/25 Python
跟老齐学Python之大话题小函数(2)
2014/10/10 Python
Python中使用SAX解析xml实例
2014/11/21 Python
Python中with及contextlib的用法详解
2017/06/08 Python
Python中字典和集合学习小结
2017/07/07 Python
python requests 使用快速入门
2017/08/31 Python
Python3视频转字符动画的实例代码
2019/08/29 Python
opencv3/Python 稠密光流calcOpticalFlowFarneback详解
2019/12/11 Python
python中with语句结合上下文管理器操作详解
2019/12/19 Python
TFRecord格式存储数据与队列读取实例
2020/01/21 Python
浅谈JupyterNotebook导出pdf解决中文的问题
2020/04/22 Python
python上下文管理器异常问题解决方法
2021/02/07 Python
利用CSS3参考手册和CSS3代码生成工具加速来学习网页制
2012/07/11 HTML / CSS
html5录音功能实战示例
2019/03/25 HTML / CSS
Superdry瑞典官网:英国日本街头风品牌
2017/05/17 全球购物
新西兰珠宝品牌:Michael Hill
2017/09/16 全球购物
澳大利亚鞋仓库:Shoe Warehouse
2019/07/25 全球购物
高中生物教学反思
2014/02/05 职场文书
教师个人自我评价范文
2014/04/13 职场文书
平安工地建设方案
2014/05/06 职场文书
泸县召开党的群众路线教育实践活动总结大会新闻稿
2014/10/21 职场文书
2015年四年级班主任工作总结
2015/10/22 职场文书
《时代广场的蟋蟀》读后感:真挚友情,温暖世界!
2020/01/08 职场文书
Redis 操作多个数据库的配置的方法实现
2022/03/23 Redis
【海涛dota】偶遇拉娜娅 质量局德鲁伊第一视角解说
2022/04/01 DOTA