通过HTTPS代理从URL下载文件

时间:2012-08-09 10:18:53

标签: php image proxy download

我需要通过网络代理从httphttps协议下载图片。此代理需要身份验证(需要usernamepassword)。我怎样才能做到这一点?目前我使用php的copy函数下载文件,但我不知道如何为它设置代理。感谢。

1 个答案:

答案 0 :(得分:1)

我使用此代码解决了我的问题:

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;
}
相关问题