ProxyUtils.php
1008 Bytes
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
<?php
class ProxyUtils {
static function addProxyForCurl(&$ch) {
if ( defined(PROXY_HOST) ) {
$proxy_host = PROXY_HOST;
if (!empty($proxy_host)) {
curl_setopt($ch, CURLOPT_PROXY, "http://".$proxy_host);
$proxy_userpwd = PROXY_USERPWD;
if (!empty($proxy_userpwd)) {
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy_userpwd);
}
}
}
}
static function getStreamContextWithProxy() {
if (!defined(PROXY_HOST))
return;
$proxy_host = PROXY_HOST;
if (empty($proxy_host))
return;
$options = array(
'http' => array(
'method' => 'GET',
'timeout' => '5',
'user_agent' => 'PHP libxml agent',
'ignore_errors' => true,
)
);
$options['http']['proxy'] = 'tcp://'.$proxy_host;
$options['http']['request_fulluri'] = TRUE;
$proxy_userpwd = PROXY_USERPWD;
if (!empty($proxy_userpwd)) {
$options['http']['header'] = "Proxy-Authorization: Basic ".base64_encode($proxy_userpwd);
}
return stream_context_create($options);
}
}
?>