繁体   English   中英

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

[英]Unable to make external HTTP Requests using PHP cURL behind Proxy

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

我正在尝试从 Web 服务器使用php-curl发出外部HTTP 请求,但它们只是超时 但是通过 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);

调试日志:

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

有用的信息:

  • 我的操作系统:Windows 7
  • 虚拟机操作系统:Ubuntu 14.04.4
  • PHP v5.5.9-1
  • 阿帕奇 2.4.7
  • VM 的 private_network ip 为 192.168.33.10
  • 值得一提的是,我支持一个公司代理,该代理将 vm 配置为与 AFAIK 很好地配合使用。

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

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

从 VM 内部进行nslookup

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,但它仍然超时。

我没有想法,谷歌搜索结果!

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

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

问题是代理服务器。

我错误地认为,因为我的虚拟机被配置为可以很好地与代理一起使用,所以所有 Apache 请求都将通过 VM。

解决方案 - 告诉 php-curl 关于代理:

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');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM