ApiConnect.class.php
2.8 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
<?php
class Util_Video_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 => 0.1
);
/**
* 请在下面添加客户端消息头,防止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;
}
// 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
$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;
}
}
?>