现在的位置: 首页 > 综合 > 正文

PHP CURL HTTP 研究笔记

2014年03月05日 ⁄ 综合 ⁄ 共 2894字 ⁄ 字号 评论关闭

转自: http://www.jz123.cn/text/2137183.html

 1 
2  <?php
  3 /**
  4 * CURL HTTP请求工具,
  5 * 作者:袁维
  6 * 参考:郑GG的BLOG
  7 * 支持以下功能:
  8 * 1:支持ssl连接和proxy代理连接
  9 * 2: 对cookie的自动支持
10 * 3: 简单的GET/POST常规操作
11 * 4: 支持单个文件上传或同字段的多文件上传,支持相对路径或绝对路径.
12 * 5: 支持返回发送请求前和请求后所有的服务器信息和服务器Header信息
13 * 6: 自动支持lighttpd服务器
14 * 7: 支持自动设置 REFERER 引用页
15 * 8: 自动支持服务器301跳转或重写问题(谢谢郑GG)
16 * 9: 其它可选项,如自定义端口,超时时间,USERAGENT,Gzip压缩等.
17 */
18 class Curl{
19 
20 //CURL句柄
21 private $ch = null;
22 //CURL执行前后所设置或服务器端返回的信息
23 private $info = array();
24 //CURL
SETOPT 信息

25 private $setopt = array(
26 //访问的端口,http默认是
80

27 'port'=>80,
28 //客户端
USERAGENT,如:"Mozilla/4.0",为空则使用用户的浏览器

29 'userAgent'=>'',
30 //连接超时时间
31 'timeOut'=>30,
32 //是否使用
COOKIE 建议打开,因为一般网站都会用到

33 'useCookie'=>true,
34 //是否支持SSL
35 'ssl'=>false,
36 //客户端是否支持
gzip压缩

37 'gzip'=>true,
38 
39 //是否使用代理
40 'proxy'=>false,
41 //代理类型,可选择
HTTP 或 SOCKS5

42 'proxyType'=>'HTTP',
43 //代理的主机地址,如果是
HTTP 方式则要写成URL形式如:"http://www.proxy.com"
44 //SOCKS5 方式则直接写主机域名为IP的形式,如:"192.168.1.1"
45 'proxyHost'=>'http://www.proxy.com',
46 //代理主机的端口
47 'proxyPort'=>1234,
48 //代理是否要身份认证(HTTP方式时)
49 'proxyAuth'=>false,
50 //认证的方式.可选择
BASIC 或 NTLM 方式

51 'proxyAuthType'=>'BASIC',
52 //认证的用户名和密码
53 'proxyAuthUser'=>'user',
54 'proxyAuthPwd'=>'password',
55 );
56 
57 /**
58 * 构造函数
59 *
60 * @param array $setopt :请参考
private $setopt 来设置
61 */
62 public function __construct($setopt=array())
63 {
64 //合并用户的设置和系统的默认设置
65 $this->setopt = array_merge($this->setopt,$setopt);
66 //如果没有安装CURL则终止程序
67 function_exists('curl_init'|| die('CURL
Library Not Loaded
');
68 //初始化
69 $this->ch = curl_init();
70 //设置CURL连接的端口
71 curl_setopt($this->ch, CURLOPT_PORT, $this->setopt['port']);
72 //使用代理
73 if($this->setopt['proxy']){
74 $proxyType = $this->setopt['proxyType']=='HTTP' ? CURLPROXY_HTTP : CURLPROXY_SOCKS5;
75 curl_setopt($this->ch, CURLOPT_PROXYTYPE, $proxyType);
76 curl_setopt($this->ch, CURLOPT_PROXY, $this->setopt['proxyHost']);
77 curl_setopt($this->ch, CURLOPT_PROXYPORT, $this->setopt['proxyPort']);
78 //代理要认证
79 if($this->setopt['proxyAuth']){
80 $proxyAuthType = $this->setopt['proxyAuthType']=='BASIC' ? CURLAUTH_BASIC : CURLAUTH_NTLM;
81 curl_setopt($this->ch, CURLOPT_PROXYAUTH, $proxyAuthType);
82 $user = "[{$this->setopt['proxyAuthUser']}]:[{$this->setopt['proxyAuthPwd']}]";
83 curl_setopt($this->ch, CURLOPT_PROXYUSERPWD, $user);
84 }
85 }
86 //启用时会将服务器服务器返回的“Location:”放在header中递归的返回给服务器
87 curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
88 //打开的支持SSL
89 if($this->setopt['ssl']){
90 //不对认证证书来源的检查
91 curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
92 //从证书中检查SSL加密算法是否存在
93 curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, true);
94 }
95 //设置http头,支持lighttpd服务器的访问
96 $header[]= 'Expect:';
97 curl_setopt($this->ch, CURLOPT_HTTPHEADER, $header);
98 //设置
HTTP USERAGENT

99 $userAgent = $this->setopt['userAgent'? $this->setopt['userAgent': $_SERVER['HTTP_USER_AGENT'];
100 curl_setopt($this->ch, CURLOPT_USERAGENT, $userAgent);
101 //设置连接等待时间,0不等待
102 curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT,

抱歉!评论已关闭.