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程序
Oct 09 PHP
PHP远程连接MYSQL数据库非常慢的解决方法
Jul 05 PHP
关于php fread()使用技巧
Jan 22 PHP
php更改目录及子目录下所有的文件后缀的代码
Sep 24 PHP
php数据库密码的找回的步骤
Jan 12 PHP
解析dedecms空间迁移步骤详解
May 15 PHP
php对二维数组按指定键值key排序示例代码
Nov 26 PHP
PHP实现获取FLV文件的时间
Feb 10 PHP
腾讯CMEM的PHP扩展编译安装方法
Sep 25 PHP
PHP使用MPDF类生成PDF的方法
Dec 08 PHP
Docker 如何布置PHP开发环境
Jun 21 PHP
实例分析PHP中PHPMailer发邮件
Dec 13 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中正确的使用json
2013/08/06 PHP
PHP实现把MySQL数据库导出为.sql文件实例(仿PHPMyadmin导出功能)
2014/05/10 PHP
页面利用渐进式JPEG来提升用户体验度
2014/12/01 PHP
实例讲解yii2.0在php命令行中运行的步骤
2015/12/01 PHP
php获取数据库结果集方法(推荐)
2017/06/01 PHP
YII框架模块化处理操作示例
2019/04/26 PHP
php更新cookie内容的详细方法
2019/09/30 PHP
javascript学习笔记(七) js函数介绍
2012/06/19 Javascript
移除AngularJS下URL中的#字符的方法
2015/06/19 Javascript
浅谈window对象的scrollBy()方法
2015/07/15 Javascript
jquery获取select选中值的方法分析
2015/12/22 Javascript
Bootstrap3 内联单选和多选框
2016/12/29 Javascript
小程序扫描普通链接二维码跳转小程序指定界面方法
2019/05/07 Javascript
浅谈redux, koa, express 中间件实现对比解析
2019/05/23 Javascript
Vue中util的工具函数实例详解
2019/07/08 Javascript
jQuery实现移动端下拉展现新的内容回弹动画
2020/06/24 jQuery
electron踩坑之remote of undefined的解决
2020/10/06 Javascript
Python多线程学习资料
2012/12/19 Python
浅谈Python爬虫基本套路
2019/03/25 Python
windows 10 设定计划任务自动执行 python 脚本的方法
2019/09/11 Python
python flask搭建web应用教程
2019/11/19 Python
20行Python代码实现视频字符化功能
2020/04/13 Python
PyPDF2读取PDF文件内容保存到本地TXT实例
2020/05/12 Python
tensorflow 大于某个值为1,小于为0的实例
2020/06/30 Python
TensorFlow的环境配置与安装方法
2021/02/20 Python
Snapfish英国:在线照片打印和个性化照片礼品
2017/01/13 全球购物
马来西亚银饰品牌:JEOEL
2017/12/15 全球购物
搬家公司的创业计划书
2014/01/01 职场文书
社团活动总结书
2014/06/27 职场文书
园林技术专业求职信
2014/07/28 职场文书
四风问题对照检查材料整改措施
2014/09/27 职场文书
导游词之蜀山胜景瓦屋山
2019/11/29 职场文书
浅谈如何提高PHP代码质量之端到端集成测试
2021/05/28 PHP
MySQL 常见的数据表设计误区汇总
2021/06/07 MySQL
使用SQL实现车流量的计算的示例代码
2022/02/28 SQL Server
在python中读取和写入CSV文件详情
2022/06/28 Python