PHP CURL使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:111.222.333.4', 'CLIENT-IP:111.222.333.4')); // 伪造IP
curl_setopt($ch, CURLOPT_REFERER, "http://www.test.com"); // 伪造来源
curl_setopt($ch, CURLOPT_PROXY, "http://111.222.333.4:110"); // 使用代理访问
curl_setopt($ch,CURLOPT_ENCODING , "gzip"); // 解开gzip网页文件
function gunzip($zipped) {
$offset = 0;
if (substr($zipped,0,2) == "\x1f\x8b")
$offset = 2;
if (substr($zipped,$offset,1) == "\x08") {
# file_put_contents("tmp.gz", substr($zipped, $offset - 2));
return gzinflate(substr($zipped, $offset + 8));
}
return "Unknown Format";
}
$headers_enabled = 1;
curl_setopt($c, CURLOPT_HEADER, $headers_enabled)
$ret = curl_exec($c);
if ($headers_enabled) {
# file_put_contents("preungzip.html", $ret);
$sections = explode("\x0d\x0a\x0d\x0a", $ret, 2);
while (!strncmp($sections[1], 'HTTP/', 5)) {
$sections = explode("\x0d\x0a\x0d\x0a", $sections[1], 2);
}
$headers = $sections[0];
$data = $sections[1];
if (preg_match('/^Content-Encoding: gzip/mi', $headers)) {
printf("gzip header found\n");
return gunzip($data);
}
}
return $ret;

参考