CURL函数扩展及测试
来源:互联网 发布:锤子移动的效果js 编辑:程序博客网 时间:2024/06/06 03:24
原创:http://www.phpthinking.com/archives/220
001
<?php
002
/**
003
* @description: 封装CURL扩展
004
* @date: 2014-07-28 16:04
005
*/
006
007
/**
008
* @编码规范
009
* @class 类名首字母大写,类名为多个单词, 每个大字首字母大写 eg: class Curl , class CurlPage
010
* @variable 变量名小写, 变量名为多个单词, 每个单词小写,使用下划线_分割 eg: $curl_result
011
* @function 函数名与类名规则相同 eg: function SendRequest
012
* @params 函数形参规则与变量名相同
013
* @class-variable 成员变量,以下划线结尾,多个单词使用下划线分隔. eg: private $host_name_
014
*/
015
/**
016
* @要求
017
*
018
*/
019
class
Curl{
020
021
/**
022
* @请求的host
023
*/
024
private
$host_
;
025
026
/**
027
* @curl 句柄
028
*/
029
private
$ch_
;
030
031
/**
032
* @请求的设置
033
*/
034
private
$options_
;
035
036
/**
037
* @保存请求头信息
038
*/
039
private
$request_header_
;
040
041
/**
042
* @保存响应头信息
043
*/
044
private
$response_header_
;
045
046
/**
047
* @body_ 用于保存curl请求返回的结果
048
*/
049
private
$body_
;
050
051
/**
052
* @读取cookie
053
*/
054
private
$cookie_file_
;
055
056
/**
057
* @写入cookie
058
*/
059
private
$cookie_jar_
;
060
061
/**
062
* @todo proxy
063
* @构造函数,初始化CURL回话
064
*/
065
public
function
__construct(){
066
$this
->ch_ = curl_init();
067
curl_setopt(
$this
->ch_, CURLOPT_COOKIE, 1);
068
curl_setopt(
$this
->ch_, CURLOPT_HEADER, 1);
069
curl_setopt(
$this
->ch_, CURLOPT_RETURNTRANSFER , 1 );
070
}
071
072
/**
073
* @返回响应头
074
*/
075
public
function
ResponseHeader(
$time_
=5){
076
if
(!function_exists(
'http_parse_headers'
)) {
077
function
http_parse_headers (
$raw_headers
){
078
$headers
=
array
();
079
foreach
(
explode
(
"\n"
,
$raw_headers
)
as
$i
=>
$h
) {
080
$h
=
explode
(
':'
,
$h
, 2);
081
if
(isset(
$h
[1])) {
082
if
(!isset(
$headers
[
$h
[0]])) {
083
$headers
[
$h
[0]] = trim(
$h
[1]);
084
}
else
if
(
is_array
(
$headers
[
$h
[0]])) {
085
$tmp
=
array_merge
(
$headers
[
$h
[0]],
array
(trim(
$h
[1])));
086
$headers
[
$h
[0]] =
$tmp
;
087
}
else
{
088
$tmp
=
array_merge
(
array
(
$headers
[
$h
[0]]),
array
(trim(
$h
[1])));
089
$headers
[
$h
[0]] =
$tmp
;
090
}
091
}
092
}
093
return
$headers
;
094
}
095
}
096
curl_setopt(
$this
->ch_, CURLOPT_TIMEOUT,
$time_
);
097
$header_size
= curl_getinfo(
$this
->ch_, CURLINFO_HEADER_SIZE);
098
$this
->response_header_ =
substr
(
$this
->body_,
$start
= 0,
$offset
=
$header_size
);
099
$this
->response_header_ = http_parse_headers(
$this
->response_header_);
100
print_r(
$this
->response_header_);
101
}
102
/**
103
* @读取cookie
104
*/
105
public
function
SetCookieFile(
$url
,
$cookie_file
){
106
curl_setopt(
$this
->ch_, CURLOPT_URL,
$url
);
107
curl_setopt(
$this
->ch_, CURLOPT_COOKIEFILE ,
$cookie_file
);
108
$this
->body_=
$this
->Execut();
109
return
$this
->Close(
$this
->body_);
110
}
111
112
/**
113
* @写入cookie
114
*/
115
public
function
SetCookieJar(
$url
,
$cookie_jar
){
116
curl_setopt(
$this
->ch_, CURLOPT_URL,
$url
);
117
curl_setopt(
$this
->ch_, CURLOPT_COOKIEJAR ,
$cookie_jar
);
118
$this
->body_=
$this
->Execut();
119
return
$this
->Close(
$this
->body_);
120
}
121
122
/**
123
* @设置HEADER
124
*/
125
126
public
function
SetHeader(
$headers
= null){
127
if
(
is_array
(
$headers
) &&
count
(
$headers
) > 0) {
128
curl_setopt(
$this
->ch_, CURLOPT_HTTPHEADER,
$headers
);
129
}
130
}
131
132
/**
133
* @GET请求
134
*/
135
public
function
Get(
$url
,
$time_
=5,
$response
=false,
array
$params
=
array
()) {
136
if
(
$params
) {
137
if
(
strpos
(
$url
,
'?'
)) {
138
$url
.=
"&"
.http_build_query(
$params
);
139
}
140
else
{
141
$url
.=
"?"
.http_build_query(
$params
);
142
}
143
}
144
curl_setopt(
$this
->ch_, CURLOPT_URL,
$url
);
145
curl_setopt(
$this
->ch_, CURLOPT_TIMEOUT,
$time_
);
146
if
(
strpos
(
$url
,
'https'
) === 0) {
147
curl_setopt(
$this
->ch_, CURLOPT_SSL_VERIFYHOST, 0);
148
curl_setopt(
$this
->ch_, CURLOPT_SSL_VERIFYPEER, 0);
149
}
150
$this
->body_=
$this
->Execut();
151
if
(
$response
){
152
$this
->ResponseHeader(
$time_
);
153
}
154
return
$this
->Close(
$this
->body_);
155
}
156
157
/**
158
* @POST请求
159
*/
160
public
function
Post(
$url
,
$time_
=5,
$response
=false,
array
$params
=
array
()) {
161
curl_setopt(
$this
->ch_, CURLOPT_URL,
$url
);
162
curl_setopt(
$this
->ch_, CURLOPT_SSL_VERIFYPEER, 0);
163
curl_setopt(
$this
->ch_, CURLOPT_HTTPHEADER,
array
(
"Content-Type: application/x-www-form-urlencoded"
));
164
curl_setopt(
$this
->ch_, CURLOPT_POST, true);
165
curl_setopt(
$this
->ch_, CURLOPT_TIMEOUT,
$time_
);
166
if
(
$params
) {
167
curl_setopt(
$this
->ch_, CURLOPT_POSTFIELDS, http_build_query(
$params
));
168
}
169
$this
->body_=
$this
->Execut();
170
if
(
$response
){
171
$this
->ResponseHeader(
$time_
);
172
}
173
return
$this
->Close(
$this
->body_);
174
}
175
176
/**
177
* @tips: google http head 方法
178
*/
179
public
function
Head(
$url
,
$time_
=5,
$response
=false,
array
$params
=
array
()) {
180
curl_setopt(
$this
->ch_, CURLOPT_URL,
$url
);
181
curl_setopt(
$this
->ch_, CURLOPT_TIMEOUT,
$time_
);
182
curl_setopt(
$this
->ch_, CURLOPT_RETURNTRANSFER , 0);
183
curl_setOpt(
$this
->ch_,CURLOPT_NOBODY, true);
184
$this
->body_=
$this
->Execut();
185
if
(
$response
){
186
$this
->ResponseHeader(
$time_
);
187
}
188
return
$this
->Close(
$this
->body_);
189
}
190
191
/**
192
* @执行CURL会话
193
*/
194
public
function
Execut(){
195
return
curl_exec(
$this
->ch_);
196
}
197
198
/**
199
* @关闭CURL句柄
200
*/
201
public
function
Close(
$body_
){
202
if
(
$body_
=== false) {
203
$fh
=
fopen
(
'log_error.txt'
,
"w"
);
204
$log_error
=curl_error(
$this
->ch_);
205
fwrite(
$fh
,
"CURL Error: "
.
$log_error
);
206
fclose(
$fh
);
207
return
false;
208
}
209
curl_close(
$this
->ch_);
210
return
$body_
;
211
}
212
}
213
214
//测试数据
215
216
$x
=
new
Curl();
217
218
$x
->Post(
'http://www.baidu.com'
,
''
,true);
219
//$x->SetCookieJar("http://www.baidu.com/index.php?tn=19045005_27_pg",'');
220
//$x->ResponseHeader('http://www.baidu.com');
221
//$x->Head('http://www.baidu.com');
222
//$x->Get('http://www.baidu.com');
223
//$x->Post('http://www.baidu.com');
2 0
- CURL函数扩展及测试
- php curl扩展安装及使用
- PHP CURL模拟GET及POST函数
- PHP CURL模拟GET及POST函数
- linux下curl作用及重要函数
- vs2010编译curl为static库及测试
- 关于测试curl 与file_get_contents抓取内容效率及稳定性
- vs2010编译curl为static库及测试
- Linux php5 curl 扩展
- php 开启curl扩展
- PHP扩展:curl
- php开启curl扩展
- PHP安装CURL扩展
- PHP封装CURL扩展
- php curl扩展
- PHP安装CURL扩展
- php curl 扩展使用
- 关于PHP扩展CURL
- java TheadLocal 多线程资源使用
- 遍历josn的三种方式
- nyoj243交换输出
- 多线程读写数据方法之读写锁方法与shared_ptr+互斥锁方法的比较
- GoF之开-闭原则
- CURL函数扩展及测试
- PAT 1039. Course List for Student (25)
- NMF(非负矩阵分解)的SGD(随机梯度下降)实现
- 算法导论——堆排序(Heap-Sort)
- 字体相关知识
- nyoj260数数小木块
- Android Fragment详解(二):Fragment创建及其生命周期
- 唐常杰--一篇 "它引" 上万的大牛论文 与 数据血统论-- 趣味数据挖掘之三
- NMF(非负矩阵分解)的SGD(随机梯度下降)实现