php中使用http代理、 Socks代理

某server上绑定上新浪开放平台的渠道接口,设置了一个ip,在本地开发的时候,使用了代理来进行测试,通过ssh转发,在本地映射了一个8086端口,请求api服务的时候,加一个proxy来进行处理。

[php]
$auth = base64_encode(‘LOGIN:PASSWORD’);

$aContext = array(
‘http’ => array(
‘proxy’ => ‘tcp://127.0.0.1:8086’,
‘request_fulluri’ => true,
‘header’ => "Proxy-Authorization: Basic $auth",
),
);
$cxContext = stream_context_create($aContext);

$sFile = file_get_contents("http://www.google.com", False, $cxContext);

echo $sFile;
[/php]
[php]
$ch = curl_init(‘http://iframe.ip138.com/city.asp’);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);

// Socks5
curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:8086");
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);

curl_exec($ch);
curl_close($ch);
[/php]