首页 文章

无法使用代理后面的PHP cURL进行外部HTTP请求

提问于
浏览
-1

我正在通过vagrant配置的虚拟机VM中运行本地LAMP Web服务器,该虚拟机位于公司代理后面 .

从Web服务器我试图使用 php-curl 发出 external HTTP请求,但它们只是 timing out ;但是通过php-curl请求本地地址工作正常 .

片段:

$ch = curl_init();
    $fp = fopen('/tmp/curl-debug.txt', 'w');
    $options = array(
    CURLOPT_URL            => 'http://stackoverflow.com',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER         => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_AUTOREFERER    => true,
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_TIMEOUT        => 15,
    CURLOPT_MAXREDIRS      => 10,
    CURLOPT_VERBOSE        => true,
        CURLOPT_STDERR         => $fp
    );

    curl_setopt_array( $ch, $options );

    $response = curl_exec($ch);
    curl_close($ch);

Debug log:

* Hostname was NOT found in DNS cache
*   Trying 23.201.173.230...
* Connection timed out after 10001 milliseconds
* Closing connection 0

有用的信息:

  • 我的操作系统:Windows 7

  • VM OS:Ubuntu 14.04.4

  • PHP v5.5.9-1

  • Apache 2.4.7

  • VM的private_network ip为192.168.33.10

  • 它_1116142_ m在企业代理后面,vm配置为与AFAIK很好地配合 .

如果我尝试从虚拟机的命令行运行curl,一切都按预期工作

例如: curl http://stackoverflow.com/ 返回html .

nslookup 来自VM内部:

Server:         10.0.2.3
Address:        10.0.2.3#53

Non-authoritative answer:
Name:   stackoverflow.com
Address: 104.16.35.249
Name:   stackoverflow.com
Address: 104.16.34.249
Name:   stackoverflow.com
Address: 104.16.37.249
Name:   stackoverflow.com
Address: 104.16.33.249
Name:   stackoverflow.com
Address: 104.16.36.249

我已经读过 /etc/resolv.conf 可以在此发挥作用,并确保用户组 www-data 具有读取权限 . 我的resolv.conf:

nameserver 10.0.2.3
search proxy.internal

我已经尝试将名称服务器更改为 8.8.8.8 没有运气,并尝试将搜索更改为 proxy.internal 解析为的IP .

我也尝试通过php-curl达到域名解析的IP以绕过dns但它仍然超时 .

我没有想法,谷歌的结果!

2 回答

  • 0

    我的第一个想法是通过在你的apache php.ini上使用 grep 来确保正确安装php-curl扩展并正常工作 . 我意识到你的问题表明你有php-curl工作在本地地址,但没有提供进一步的细节,所以我倾向于仔细检查 .

    我的下一个想法是公司代理干扰 - 尝试在不同的互联网连接(你的房子,连接到你的手机,无论如何)启动盒子,看看是否能解决你的问题 . 如果它不是至少你可以排除代理是直接的问题 .

  • 1

    问题是代理服务器 .

    我错误地认为,因为我的虚拟机配置为与代理很好地协作,所有Apache请求都将通过VM .

    Solution - tell php-curl about the proxy:

    curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
    curl_setopt($ch, CURLOPT_PROXYPORT, $PORT);
    curl_setopt($ch, CURLOPT_PROXY, $PROXY_ADDR);
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'username_here:password_here');
    

相关问题