ApiConnect.class.php
4.75 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
class Util_Product_ApiConnect
{
private static $mDefaultOptions = array(
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0',
CURLOPT_CONNECTTIMEOUT => 0,
CURLOPT_TIMEOUT => 2,
CURLOPT_DNS_USE_GLOBAL_CACHE => false
);
/**
* 请在下面添加客户端消息头,防止API被视频网站封杀...
*/
private static function _rand_agent()
{
$useragents = array(
'Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0',
'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.15 (KHTML, like Gecko) Chrome/10.0.608.0 Safari/534.15',
'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 6.1;)',
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 6.1;)',
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1;)',
'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1;)',
'Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1;)',
'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 Safari/534.30',
'Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.13',
'Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.13'
);
self::$mDefaultOptions[CURLOPT_USERAGENT] = array_rand($useragents, 1);
}
public static function get($url, $options = NULL)
{
self::_rand_agent();
if($options === NULL)
{
// Use default options
$options = self::$mDefaultOptions;
}
else
{
// Add default options
$options = $options + self::$mDefaultOptions;
}
if(isset($options[CURLOPT_FOLLOWLOCATION]))
{
$isFollow = true;
unset($options[CURLOPT_FOLLOWLOCATION]);
}
else
{
$isFollow = false;
}
// The transfer must always be returned
$options[CURLOPT_RETURNTRANSFER] = true;
// Open a new remote connection
$remote = curl_init($url);
// Set connection options
if(!curl_setopt_array($remote, $options))
{
return false;
}
// Get the response
if($isFollow)
{
$response = self::curl_exec_follow($remote);
}
else
{
$response = curl_exec($remote);
}
// Get the response information
$code = curl_getinfo($remote, CURLINFO_HTTP_CODE);
if($code && $code < 200 || $code > 299)
{
$error = $response;
}
elseif($response === false)
{
$error = curl_error($remote);
}
// Close the connection
curl_close($remote);
return $response;
}
private static function curl_exec_follow( $ch, &$maxredirect = 50)
{
$mr = $maxredirect === null ? 5 : intval($maxredirect);
if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off'))
{
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, $mr);
}
else
{
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
if ($mr > 0)
{
$newurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$rch = curl_copy_handle($ch);
curl_setopt($rch, CURLOPT_HEADER, true);
curl_setopt($rch, CURLOPT_NOBODY, true);
curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
curl_setopt($rch, CURLOPT_RETURNTRANSFER, true);
do {
curl_setopt($rch, CURLOPT_URL, $newurl);
$header = curl_exec($rch);
if (curl_errno($rch))
{
$code = 0;
}
else
{
$code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
if ($code == 301 || $code == 302)
{
preg_match('/Location:(.*?)\n/', $header, $matches);
$newurl = trim(array_pop($matches));
}
else
{
$code = 0;
}
}
} while ($code && --$mr);
curl_close($rch);
if (!$mr)
{
if ($maxredirect === null)
{
return false;
}
else
{
$maxredirect = 0;
}
return false;
}
curl_setopt($ch, CURLOPT_URL, $newurl);
}
}
return curl_exec($ch);
}
}
?>