- 浏览: 384030 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
zjx20:
...
非阻塞(nonblock)socket接口会否出现EINTR错误 -
mrfeng:
Digest 方式呢?
构建RESTful Web Service - 验证的实现和使用(HTTP 基本认证) -
lck140:
lck140 写道我照着楼主的方法试了一天了,根本就不行。是不 ...
nginx 预压缩(gzip)静态文件 -
lck140:
我照着楼主的方法试了一天了,根本就不行。是不是有哪没弄好,请高 ...
nginx 预压缩(gzip)静态文件 -
iwlk:
chrome 是支持gzip的, 只是添加了sdch~ 大家 ...
nginx 预压缩(gzip)静态文件
cURL是一个可以在命令行下发起http请求的工具,phpl有调用ibcurl的适配器(adapter),所以在php里可以很方便的使用这个工具。
在使用cURL的过程中是否觉得难以使用,选项太多,没有封装,使用不够方便?
为了方便使用,我对php的cURL进行了二次封装,基于组合模式,使用灵活,能满足多数应用场景,经过我长期的实践。
实际上Curl_Manager并没有开发到心目中理想的样子,但并不妨碍使用,仅供各位参考:)
测试例子:
支持cookie:
带参数请求,get/post/put/delete等等使用方式一样
Curl_Manager代码,也可以从附件下载:
在使用cURL的过程中是否觉得难以使用,选项太多,没有封装,使用不够方便?
为了方便使用,我对php的cURL进行了二次封装,基于组合模式,使用灵活,能满足多数应用场景,经过我长期的实践。
- 支持 ssl/proxy
- 支持 cookie,会记录所有请求的cookie并带上(如需获得cookie的值,需要自己解析)
- 支持常见请求方法get/post/put/delete 等等RESTFul所需要的方法
- 支持文件上传
- 支持返回http报文和报头
- 支持带cookie的follow location(如果使用curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, true);cookie并不会自动带上)
- 解决cURL请求lighttpd会产生417 Expectation Failed错误的问题
实际上Curl_Manager并没有开发到心目中理想的样子,但并不妨碍使用,仅供各位参考:)
测试例子:
<?php require_once("Curl_Manager.php"); $manager = new Curl_Manager(); $manager->set_action('gg', 'http://www.google.com/', 'http://willko.iteye.com/');//设置动作,(动作名称, 动作对应url,来源referer) $manager->open()->get('gg'); //打开一个请求,进行get操作 echo $manager->body(); // 获得报文 echo $manager->header(); // 获得报头(需要自己解析) //发起ssl请求 $manager->set_action('taobao', 'https://login.taobao.com/member/login.jhtml?f=top&redirectURL=http://www.taobao.com/', 'http://willko.iteye.com/'); $manager->ssl()->get('taobao'); //使用ssl方法,让当前这个请求支持ssl请求 echo $manager->body(); echo $manager->header();
支持cookie:
$manager->cookie();
带参数请求,get/post/put/delete等等使用方式一样
$manager->post('action', array('k' => 'v', 'a' => 'b')); $manager->post('action', 'k=v&a=b'); $manager->post('action', array('k' => 'v', '@a' => '/home/www/avatar.gif')); //上传,需要使用绝对路径,参数名称加"@"。
Curl_Manager代码,也可以从附件下载:
<?php // +----------------------------------------------------------------------+ // | Copyright (c) 2008-2010 Willko Cheng | // +----------------------------------------------------------------------+ // | Authors: Willko Cheng <willko@foxmail.com> | // | Blog: http://willko.iteye.com/ | // +----------------------------------------------------------------------+ class Curl_Manager { private $_is_temp_cookie = false; private $_header; private $_body; private $_ch; private $_proxy; private $_proxy_port; private $_proxy_type = 'HTTP'; // or SOCKS5 private $_proxy_auth = 'BASIC'; // or NTLM private $_proxy_user; private $_proxy_pass; protected $_cookie; protected $_options; protected $_url = array (); protected $_referer = array (); public function __construct($options = array()) { $defaults = array (); $defaults ['timeout'] = 30; $defaults ['temp_root'] = sys_get_temp_dir (); $defaults ['user_agent'] = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-CN; rv:1.8.1.20) Gecko/20081217 Firefox/2.0.0.20'; $this->_options = array_merge ( $defaults, $options ); } public function open() { $this->_ch = curl_init (); //curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt ( $this->_ch, CURLOPT_HEADER, true ); curl_setopt ( $this->_ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt ( $this->_ch, CURLOPT_USERAGENT, $this->_options ['user_agent'] ); curl_setopt ( $this->_ch, CURLOPT_CONNECTTIMEOUT, $this->_options ['timeout'] ); curl_setopt ( $this->_ch, CURLOPT_HTTPHEADER, array('Expect:') ); // for lighttpd 417 Expectation Failed $this->_header = ''; $this->_body = ''; return $this; } public function close() { if (is_resource ( $this->_ch )) { curl_close ( $this->_ch ); } if (isset ( $this->_cookie ) && $this->_is_temp_cookie && is_file ( $this->_cookie )) { unlink ( $this->_cookie ); } } public function cookie() { if (! isset ( $this->_cookie )) { if (! empty ( $this->_cookie ) && $this->_is_temp_cookie && is_file ( $this->_cookie )) { unlink ( $this->_cookie ); } $this->_cookie = tempnam ( $this->_options ['temp_root'], 'curl_manager_cookie_' ); $this->_is_temp_cookie = true; } curl_setopt ( $this->_ch, CURLOPT_COOKIEJAR, $this->_cookie ); curl_setopt ( $this->_ch, CURLOPT_COOKIEFILE, $this->_cookie ); return $this; } public function ssl() { curl_setopt ( $this->_ch, CURLOPT_SSL_VERIFYPEER, false ); return $this; } public function proxy($host = null, $port = null, $type = null, $user = null, $pass = null, $auth = null) { $this->_proxy = isset ( $host ) ? $host : $this->_proxy; $this->_proxy_port = isset ( $port ) ? $port : $this->_proxy_port; $this->_proxy_type = isset ( $type ) ? $type : $this->_proxy_type; $this->_proxy_auth = isset ( $auth ) ? $auth : $this->_proxy_auth; $this->_proxy_user = isset ( $user ) ? $user : $this->_proxy_user; $this->_proxy_pass = isset ( $pass ) ? $pass : $this->_proxy_pass; if (! empty ( $this->_proxy )) { curl_setopt ( $this->_ch, CURLOPT_PROXYTYPE, $this->_proxy_type == 'HTTP' ? CURLPROXY_HTTP : CURLPROXY_SOCKS5 ); curl_setopt ( $this->_ch, CURLOPT_PROXY, $this->_proxy ); curl_setopt ( $this->_ch, CURLOPT_PROXYPORT, $this->_proxy_port ); } if (! empty ( $this->_proxy_user )) { curl_setopt ( $this->_ch, CURLOPT_PROXYAUTH, $this->_proxy_auth == 'BASIC' ? CURLAUTH_BASIC : CURLAUTH_NTLM ); curl_setopt ( $this->_ch, CURLOPT_PROXYUSERPWD, "[{$this->_proxy_user}]:[{$this->_proxy_pass}]" ); } return $this; } public function post($action, $query = array()) { if (is_array($query)) { foreach ($query as $key => $val) { if ($val{0} != '@') { $encode_key = urlencode($key); if ($encode_key != $key) { unset($query[$key]); } $query[$encode_key] = urlencode($val); } } } curl_setopt ( $this->_ch, CURLOPT_POST, true ); curl_setopt ( $this->_ch, CURLOPT_URL, $this->_url [$action] ); curl_setopt ( $this->_ch, CURLOPT_REFERER, $this->_referer [$action] ); curl_setopt ( $this->_ch, CURLOPT_POSTFIELDS, $query ); $this->_requrest (); return $this; } public function get($action, $query = array()) { $url = $this->_url [$action]; if (! empty ( $query )) { $url .= strpos ( $url, '?' ) === false ? '?' : '&'; $url .= is_array ( $query ) ? http_build_query ( $query ) : $query; } curl_setopt ( $this->_ch, CURLOPT_URL, $url ); curl_setopt ( $this->_ch, CURLOPT_REFERER, $this->_referer [$action] ); $this->_requrest (); return $this; } public function put($action, $query = array()) { curl_setopt ( $this->_ch, CURLOPT_CUSTOMREQUEST, 'PUT' ); return $this->post ( $action, $query ); } public function delete($action, $query = array()) { curl_setopt ( $this->_ch, CURLOPT_CUSTOMREQUEST, 'DELETE' ); return $this->post ( $action, $query ); } public function head($action, $query = array()) { curl_setopt ( $this->_ch, CURLOPT_CUSTOMREQUEST, 'HEAD' ); return $this->post ( $action, $query ); } public function options($action, $query = array()) { curl_setopt ( $this->_ch, CURLOPT_CUSTOMREQUEST, 'OPTIONS' ); return $this->post ( $action, $query ); } public function trace($action, $query = array()) { curl_setopt ( $this->_ch, CURLOPT_CUSTOMREQUEST, 'TRACE' ); return $this->post ( $action, $query ); } public function connect() { } public function follow_location() { preg_match ( '#Location:\s*(.+)#i', $this->header (), $match ); if (isset ( $match [1] )) { $this->set_action ( 'auto_location_gateway', $match [1], $this->effective_url () ); $this->get ( 'auto_location_gateway' )->follow_location (); } return $this; } public function set_action($action, $url, $referer = '') { $this->_url [$action] = $url; $this->_referer [$action] = $referer; return $this; } public function header() { return $this->_header; } public function body() { return $this->_body; } public function effective_url() { return curl_getinfo ( $this->_ch, CURLINFO_EFFECTIVE_URL ); } public function http_code() { return curl_getinfo($this->_ch, CURLINFO_HTTP_CODE); } private function _requrest() { $response = curl_exec ( $this->_ch ); $errno = curl_errno ( $this->_ch ); if ($errno > 0) { throw new Curl_Manager_Exception ( curl_error ( $this->_ch ), $errno ); } $header_size = curl_getinfo ( $this->_ch, CURLINFO_HEADER_SIZE ); $this->_header = substr ( $response, 0, $header_size ); $this->_body = substr ( $response, $header_size ); } public function __destruct() { $this->close (); } } class Curl_Manager_Exception extends Exception { }
- Curl_Manager.rar (2.1 KB)
- 下载次数: 273
发表评论
-
PHP实时输出报文到浏览器
2010-06-06 15:09 8002Yahoo的前端优化实践中 ... -
PHP多Web服务器的Session问题
2010-05-11 21:30 0当Web服务器有多台的时候,会遇到一个Session共享不共享 ... -
构建RESTful Web Service - 验证的实现和使用(HTTP 基本认证)
2010-04-07 11:40 15809因为RESTful的无状态特性,导致无法知道当前的请求方是否可 ... -
也谈谈PHP是否需要模板机制?
2010-03-30 19:37 0最近有点时间,重温了一下设计模式,设计模式和OO真令人费解,博 ... -
很好的序列化方式 Igbinary
2009-11-21 19:29 0如果你使用PHP做开发,那有些时候需要序列化PHP数据,不管是 ... -
10进制和64进制相互转换 php版
2009-11-05 11:28 6984使用:表示10以及使用;表示11的原因是根据ascii的排列来 ... -
使用HTML Purifier解决XSS问题
2009-09-23 22:33 12412在php里解决XSS最简单的方法是使用htmlspecialc ... -
php里json_decode无法解析的情况,返回空(null)
2009-08-04 21:08 31447我的版本是5.2.x,php的json解析也太烂了,兼容性太差 ... -
激动人心的 APC 新特性
2009-07-27 23:22 1786来自facebook的大牛在OSCON2009上分享了APC的 ... -
php-fpm配置文件中文文档
2009-06-29 09:30 0配置文件 这个php-fpm.conf文件, 可 ... -
一次查询完成基于“父id"的无限分类
2009-06-25 18:00 3126无限分类谈得太多了,无非三种方法“路径”“左右值”“父id”, ... -
我看php 5.3 的新特性
2009-03-13 23:34 6535从php4到php5,从个人框 ... -
php 实现简单 sso 初步方案
2009-02-24 18:37 4919首先,声明一点,sso和任何语言没有关系,因为我只懂php,为 ... -
feed设计
2009-02-21 23:46 5280对于sns来说,动态就是 ... -
PHP-FPM高负载技巧 (PHP-FPM on highload tips)
2009-02-15 23:18 8460只供参考,英语不好,多多练习。。 这些技巧都比较基本的。。 ... -
Tokyo Tyrant 在 php 上不能自动反序列化解决方法
2009-02-15 02:11 7502Tokyo Tyrant使用memcached协议连接,但是用 ... -
FYI:初学php时写的php邮件发送类
2009-02-08 11:28 2431<?php /** * 功能: 发送邮件 ... -
PHP 加密/解密函数 dencrypt(动态密文,带压缩功能,支持中文)
2009-02-08 10:39 8529// +------------------------- ... -
discuz 经典php加密解密函数 authcode 解析
2009-02-08 10:38 11503康盛的 authcode 函数可以说对中国的PHP界作出了重大 ... -
求相对路径的函数
2009-02-08 10:36 2462网上某面试题, 写一个函数,算出两个文件的相对路径 如 $a ...
相关推荐
关于php的curl二次封装的类,免费下载
项目中自己使用的时候,自己封装的类库,请大家多多指教
ios编译COCOS2DX时,有些版本会出现以下错误,'__curl_rule_01__' declared as an array with a negative size 只需把所有的.h文件替换掉引擎的,再把libcurl.a添加到工程中,即可解决这问题
curl-curl-7_47_1.zip
curl工具,可以方便调试url,简单易用的url调试工具
一个基于PHP cURL的开源HTTP类库,支持链式操作,省去繁杂的cURL使用方法。
php-httplib是一个基于php curl库封装的http类库,简单易用的同时兼顾性能及功能,可完美解决您的web请求需求!
php-httplib是一个基于php curl库封装的http类库,简单易用的同时兼顾性能及功能,可完美解决您的web请求需求!
curl-curl-7_53_1
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
curl_http,作为curl一个工具类,以便创建访问对话,采集网站数据
记录curl 自定义进度条 CURLOPT_PROGRESSFUNCTION 相关代码
4.15的版本的www-curl,老一点的版本,根据自己选择下载
libcurl移植到乐鑫ESP32芯片上,包含使用demo
php的curl封装类用法实例_.docx
在C++下,Curl获取网络时间的源代码,适合新手小白使用。
最新版本的www-curl,下载解压就可以,非常好用。
windows下C开发CURL的Demo
强大的 CURL多线程类库 ,性能瓶颈在IO读写,如果不是IO瓶颈,性能会更快。欢迎伙伴们测试 内置7个demo示例
分享给大家供大家参考,具体如下: php中curl_init()的作用很大,尤其是在抓取网页内容或文件信息的时候,例如之前文章《php使用curl获取header检测开启GZip压缩》就介绍到curl_init()的强大。 curl_init()处理事物...