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中通过ADODB库实现调用Access数据库之修正版本
Dec 31 PHP
php adodb分页实现代码
Mar 19 PHP
PHP在字符断点处截断文字的实现代码
Apr 21 PHP
php控制linux服务器常用功能 关机 重启 开新站点等
Sep 05 PHP
简单谈谈php中ob_flush和flush的区别
Nov 27 PHP
php获取QQ头像并显示的方法
Dec 23 PHP
php实现utf-8转unicode函数分享
Jan 06 PHP
Linux+Nginx+MySQL下配置论坛程序Discuz的基本教程
Dec 23 PHP
php ucwords() 函数将字符串中每个单词的首字符转换为大写(实现代码)
May 12 PHP
phpinfo()中Loaded Configuration File(none)的解决方法
Jan 16 PHP
PHP学习笔记之session
May 06 PHP
YII框架实现自定义第三方扩展操作示例
Apr 26 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 和 MySQL 基础教程(二)
2006/10/09 PHP
PHP语法速查表
2007/01/02 PHP
php实现session自定义会话处理器的方法
2015/01/27 PHP
PHP内核探索:哈希表碰撞攻击原理
2015/07/31 PHP
PHP连接数据库实现注册页面的增删改查操作
2016/03/27 PHP
laravel5实现微信第三方登录功能
2018/12/06 PHP
JS取文本框中最小值的简单实例
2013/11/29 Javascript
使用jQuery Rotare实现微信大转盘抽奖功能
2016/06/20 Javascript
angular select 默认值设置方法
2017/06/23 Javascript
Nodejs之TCP服务端与客户端聊天程序详解
2017/07/07 NodeJs
微信小程序实现省市区三级地址选择
2020/06/21 Javascript
ES6入门教程之Array.from()方法
2019/03/23 Javascript
JS实现马赛克图片效果完整示例
2019/04/13 Javascript
JS使用setInterval计时器实现挑战10秒
2020/11/08 Javascript
原生js实现表格循环滚动
2020/11/24 Javascript
[01:32:22]DOTA2-DPC中国联赛 正赛 Ehome vs VG BO3 第一场 2月5日
2021/03/11 DOTA
使用python实现递归版汉诺塔示例(汉诺塔递归算法)
2014/04/08 Python
python将字典内容存入mysql实例代码
2018/01/18 Python
简述Python2与Python3的不同点
2018/01/21 Python
基于Python socket的端口扫描程序实例代码
2018/02/09 Python
Python SMTP发送邮件遇到的一些问题及解决办法
2018/10/24 Python
python使用pip安装模块出现ReadTimeoutError: HTTPSConnectionPool的解决方法
2019/10/04 Python
python 如何停止一个死循环的线程
2020/11/24 Python
HTML5中视频音频的使用详解
2017/07/07 HTML / CSS
美国奢侈品购物平台:Orchard Mile
2018/05/02 全球购物
Brydge英国:适用于Apple iPad和Microsoft Surface Pro的蓝牙键盘
2019/05/16 全球购物
先进班级集体事迹材料
2014/01/30 职场文书
住宅使用说明书
2014/05/09 职场文书
2014年学校国庆主题活动方案
2014/09/16 职场文书
学习党的群众路线对照检查材料
2014/09/29 职场文书
公司放假通知范文
2015/04/14 职场文书
幼儿园亲子活动通知
2015/04/24 职场文书
建党伟业观后感
2015/06/01 职场文书
运动会闭幕式通讯稿
2015/07/18 职场文书
幼儿园小班开学寄语(2016秋季)
2015/12/03 职场文书
详解解Django 多对多表关系的三种创建方式
2021/08/23 Python