我需要通过网络代理从http
和https
协议下载图像。此代理需要身份验证(它需要 username
和 password
)。我该怎么做?目前我使用 php 的 copy
功能下载文件,但我不知道如何为它设置代理。谢谢。
我使用以下代码解决了我的问题:
public static function dlFile($url)
{
$crl = curl_init();
curl_setopt($crl, CURLOPT_PROXY, "IP:PORT");
curl_setopt($crl, CURLOPT_PROXYUSERPWD, "USER:PASS");
curl_setopt ($crl, CURLOPT_URL,$url);
curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, 300);
curl_setopt($crl, CURLOPT_HTTPPROXYTUNNEL, true); //IMPORTANT
$ret = curl_exec($crl);
curl_close($crl);
return $ret;
}