3
3
* Curl Class
4
4
*
5
5
* @author http://weibo.com/yakeing
6
- * @version 2.1
6
+ * @version 2.2
7
7
* Prompt: CertUrl The server may not contain root / chain SSL certificates, causing authentication errors
8
- * 注意: CertUrl 服务器可能不包含根 /链SSL证书,导致身份验证错误
8
+ * 注意: 服务器证书可能不包含根 /链SSL证书,导致身份验证错误
9
9
* Prompt: When you open the {HeadOut} option, GET/POST returns only false
10
10
* 注意:当您打开{HeadOut}选项时,GET / POST仅返回false
11
11
*
@@ -19,32 +19,33 @@ class curl{
19
19
public $ Body = '' ;
20
20
public $ HttpCode = 0 ;
21
21
public $ HttpError = '' ;
22
- public $ HttpUrl = '' ; //有效发送地址(最后一个)
22
+ public $ HttpUrl = '' ; //最后一个有效发送地址
23
23
public $ HeadersOut = '' ; //发送的头部
24
24
public $ HttpCookie = array (); //返回 Cookie
25
25
26
- public $ Cookie = array (); //设置Cookie
26
+ public $ CurlOpt = array (); //自定义 Curl 选项
27
+ public $ Cookie = array (); //设置 Cookie
27
28
public $ Timeout = 5 ; //运行时间
28
29
public $ Referer = '' ; //伪装来源
29
- public $ CertUrl = '' ; //HTTPS地址SSL证书
30
+ public $ CertUrl = '' ; //HTTPS地址SSL证书路径
30
31
public $ HeadersOn = false ; //输出头部信息
31
- public $ HeaderFunction = false ; //回调函数(头部 )
32
+ public $ HeaderFunction = false ; //回调函数(返回头部 )
32
33
public $ HeadOut = false ; //回调函数(只输出头部)
33
- public $ LocationQuantity = 0 ; //自动重定向次数(301/302)
34
+ public $ LocationQuantity = 0 ; //自动重定向次数(HTTP 301/302)
34
35
public $ Encoding = 'deflate, gzip ' ; //压缩 1.identity、2.deflate, gzip
35
- public $ UserAgent = 'Mozilla/5.0 (compatible; Baiduspider /2.0 ; +http://www.baidu .com/search/spider .html) ' ;
36
+ public $ UserAgent = 'Mozilla/5.0 (compatible; Googlebot /2.1 ; +http://www.google .com/bot .html ) ' ;
36
37
37
38
//Constructor
38
39
public function __construct (){
39
40
if (function_exists ('curl ' )){
40
- throw new Exception ('php需要Curl函数支持或被关闭 ' );
41
+ throw new Exception ('Curl not defined ' );
41
42
}
42
43
} //END __construct
43
44
44
45
//Get Network Resources
45
46
public function Get ($ Url , $ Header = array ()){
46
47
$ Options = $ this ->Options ($ Url , $ Header );
47
- //$Options[CURLOPT_HTTPGET] = true; //默认
48
+ //$Options[CURLOPT_HTTPGET] = true; //default setting
48
49
return $ this ->Http ($ Options );
49
50
} //NDE Get
50
51
@@ -59,52 +60,63 @@ public function Post($Url, $Vars, $Header = array()){
59
60
//Curl Options
60
61
private function Options ($ Url , $ Header ){
61
62
$ Options = array (
62
- CURLOPT_URL => $ Url , //URL 地址
63
- CURLOPT_USERAGENT => $ this ->UserAgent , //客户UA标示
64
- CURLOPT_TIMEOUT => $ this ->Timeout , //运行秒
65
- CURLOPT_CONNECTTIMEOUT => $ this ->Timeout , //连接秒
66
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1 , //http 1.1版本
67
- CURLOPT_RETURNTRANSFER => true , //返回文件流
63
+ CURLOPT_URL => $ Url , //Destination address(URL)
64
+ CURLOPT_USERAGENT => $ this ->UserAgent , //Customer UA mark
65
+ CURLOPT_TIMEOUT => $ this ->Timeout , //Running time (sec)
66
+ CURLOPT_CONNECTTIMEOUT => $ this ->Timeout , //Connection time (sec)
67
+ CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1 , //http 1.1 Edition
68
+ CURLOPT_RETURNTRANSFER => true , //Return file stream
68
69
//CURLOPT_COOKIESESSION => true, //session cookie
69
- CURLINFO_HEADER_OUT => true //追踪句柄的请求
70
+ CURLINFO_HEADER_OUT => true //Request for trace handle
70
71
);
71
- //不输出Body(无法接收cookie)
72
+ //Closing Body will not be able to receive cookie information
72
73
//$Options[CURLOPT_NOBODY] = true;
73
- if (!empty ($ Header )){//设置头部信息
74
+ if (!empty ($ Header )){//Set header information
74
75
$ Options [CURLOPT_HTTPHEADER ] = $ Header ;
75
76
}
76
- if (strrpos ($ Url , 'https:// ' ) === 0 ){//https模式
77
+ if (strrpos ($ Url , 'https:// ' ) === 0 ){//https mode
77
78
if (empty ($ this ->CertUrl )){
78
- $ Options [CURLOPT_SSL_VERIFYPEER ] = false ;//https安全
79
- $ Options [CURLOPT_SSL_VERIFYHOST ] = false ;//证书检查
79
+ $ Options [CURLOPT_SSL_VERIFYPEER ] = false ;//https peer’s certificate OFF
80
+ $ Options [CURLOPT_SSL_VERIFYHOST ] = false ;//No inspection certificate
80
81
}else {
81
- $ Options [CURLOPT_SSL_VERIFYPEER ] = true ;//https安全
82
- $ Options [CURLOPT_SSL_VERIFYHOST ] = 2 ;//严格认证
83
- $ Options [CURLOPT_CAINFO ] = $ this ->CertUrl ;//证书地址
82
+ $ Options [CURLOPT_SSL_VERIFYPEER ] = true ;//Validation of peer’s certificate
83
+ $ Options [CURLOPT_SSL_VERIFYHOST ] = 2 ;//Does the Certificate Common Name match the hostname (Strict certification)
84
+ $ Options [CURLOPT_CAINFO ] = $ this ->CertUrl ;//Certificate address
84
85
}
85
86
}
86
- if (is_int ($ this ->LocationQuantity ) && $ this ->LocationQuantity > 0 ){//递归 自动重定向次数( 301/302)
87
- $ Options [CURLOPT_FOLLOWLOCATION ] = true ;//重定向
88
- $ Options [CURLOPT_AUTOREFERER ] = true ;//重定向自动设置Referer:信息
89
- $ Options [CURLOPT_MAXREDIRS ] = $ this ->LocationQuantity ; //重定向次数
87
+ if (is_int ($ this ->LocationQuantity ) && $ this ->LocationQuantity > 0 ){//redirection (HTTP 301/302)
88
+ $ Options [CURLOPT_FOLLOWLOCATION ] = true ;//Open redirection
89
+ $ Options [CURLOPT_AUTOREFERER ] = true ;//Redirection automatically sets Referer information
90
+ $ Options [CURLOPT_MAXREDIRS ] = $ this ->LocationQuantity ; //Redirection times
90
91
}
91
- if (!empty ($ this ->Cookie )){//Cookie信息
92
- $ Options [CURLOPT_COOKIE ] = $ this ->Cookie ;
92
+ if (!empty ($ this ->Cookie )){//Cookie information
93
+ if (is_array ($ this ->Cookie )){
94
+ $ cookie = '' ;
95
+ foreach ($ this ->Cookie as $ key => $ value ) {
96
+ $ cookie .= $ key .'= ' .$ value .'; ' ;
97
+ }
98
+ }else { //is_string($this->Cookie)
99
+ $ cookie = $ this ->Cookie ;
100
+ }
101
+ $ Options [CURLOPT_COOKIE ] = $ cookie ;
93
102
}
94
- if (!empty ($ this ->Referer )){//伪装来源
103
+ if (!empty ($ this ->Referer )){//Camouflage source
95
104
$ Options [CURLOPT_REFERER ] = $ this ->Referer ;
96
105
}
97
- if (!empty ($ this ->Encoding )){//压缩
106
+ if (!empty ($ this ->Encoding )){//compress
98
107
$ Options [CURLOPT_ENCODING ] = $ this ->Encoding ;
99
108
}
100
- if ($ this ->HeadersOn === true ){//返回头部
109
+ if ($ this ->HeadersOn === true ){//Back header
101
110
$ Options [CURLOPT_HEADER ] = true ;
102
111
}
103
- if ($ this ->HeaderFunction === true ){//回调函数(获取头部 )
112
+ if ($ this ->HeaderFunction === true ){//callback (Get header )
104
113
$ Options [CURLOPT_HEADERFUNCTION ] = array ($ this , 'HeaderFunction ' );
105
114
}
115
+ if (count ($ this ->CurlOpt )>0 ){//Add or change Curl Options
116
+ $ Options = array_merge ($ Options , $ this ->CurlOpt );
117
+ }
106
118
return $ Options ;
107
- }
119
+ } //END Options
108
120
109
121
// Header Function
110
122
private function HeaderFunction ($ thising , $ header ){
@@ -131,15 +143,15 @@ private function Http($options){
131
143
$ ch = curl_init ();
132
144
curl_setopt_array ($ ch , $ options );
133
145
$ ResUlt = curl_exec ($ ch );
134
- //获取 header 部分的大小
146
+ //Gets the size of the header
135
147
$ Header_size = curl_getinfo ($ ch , CURLINFO_HEADER_SIZE );
136
- //最后一个有效的URL地址
148
+ //Gets the last valid URL
137
149
$ this ->HttpUrl = curl_getinfo ($ ch , CURLINFO_EFFECTIVE_URL );
138
- //发送的头部请求
150
+ //Send header request
139
151
$ this ->HeadersOut = curl_getinfo ($ ch , CURLINFO_HEADER_OUT );
140
- //获取最后一个代码
152
+ //Get the last valid code
141
153
$ this ->HttpCode = (int )curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
142
- //获取Content -Type:值
154
+ //Get Content -Type value
143
155
//$ContentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE );
144
156
if ($ ResUlt === false || curl_errno ($ ch ) !== 0 ){
145
157
$ this ->error = curl_error ($ ch );
0 commit comments