【问题标题】:php curl get proxyphp curl获取代理
【发布时间】:2012-09-27 09:50:07
【问题描述】:

我在 Windows 中使用 PHP 脚本发出 curl 请求以成功发出 SOAP 请求,并且我正在尝试准确追踪这个成功的请求是如何在 C#.NET 中复制它的。

如何使用 PHP 检测 curl 将使用哪个代理服务器?

我希望php curl 中可能有一个curl_getopt,所以我可以这样做:

curl_getopt(CURLOPT_PROXY);

可惜不存在。

我有什么方法可以找出php curl 将通过哪个代理服务器进行连接?

【问题讨论】:

    标签: php windows curl


    【解决方案1】:

    1。你告诉 curl 使用什么代理,而不是 viaversa:

    function wget($url)
    {
        $options = array(
            CURLOPT_URL             => $url,
            CURLOPT_HEADER          => false,
            CURLOPT_FOLLOWLOCATION  => true,
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_TIMEOUT         => 30,
            CURLOPT_PROXY           => '...proxy.ip...',
            CURLOPT_PROXYPORT       => '...proxy.port...',
            CURLOPT_USERAGENT       => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.8.0.9) Gecko/20061206 Firefox/1.5.0.9',
        );
        $ch = curl_init();
        curl_setopt_array($ch, $options);
        $content = curl_exec($ch);
        $error = curl_error($ch);
        return (!$error && $content) ? $content : null;
    }
    

    2。另一种解决方案>

    看看这个答案 > How to get an option previously set with curl_setopt()?

    3。覆盖 curl_setopt() 函数

    http://php.net/manual/en/function.override-function.php

    附言你可能需要http://php.net/manual/en/function.rename-function.php

    4。使用runkit覆盖函数

    http://php.net/manual/en/book.runkit.php

    【讨论】:

    • 好吧,这就是我害怕的。不幸的是,这并不能解决我的问题,因为我根本没有在我的代码中设置 curl 选项。因此,如果我在 PHP 中找不到此选项,是否有任何其他方法可以确定 curl 可能从操作系统中获取的代理选项?
    • 最后我通过实验发现在HTTP_PROXY环境变量中设置了一个代理,这就是它使用的。 PHP 中的覆盖不起作用,因为我猜这是比 PHP 所知道的更低级别的事情。不过感谢您的帮助,我会接受这个答案,因为有很多选择。
    【解决方案2】:

    如果它使用与您的 Windows 配置相同的配置,您应该可以像这样找到它(未经测试,因为我没有 Windows 服务器):

    <?php
        $proxyServerString = shell_exec('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings" | find /i "proxyserver"');
        $proxyServerString = trim($proxyServerString);
        /*
            $proxyServerString should be 'ProxyServer    REG_SZ    http=127.0.0.1:8888;https=127.0.0.1:8888'
        */
        preg_match("/ProxyServer *REG_SZ *(.*)/i", $proxyServerString, $match);
        /*
            $match[1] will be something like 'http=127.0.0.1:8888;https=127.0.0.1:8888'
        */
        $proxyServersTemp = explode(";", $match[1]);
        $proxyServers = array();
        foreach ($proxyServersTemp as $proxyServerTemp) {
            preg_match("/^(.*?)=(.*?):(.*?)$/", $proxyServerTemp, $proxyMatch);
            $proxyServers[] = array(
                "protocol" => $proxyMatch[1],
                "address" => $proxyMatch[2],
                "port" => $proxyMatch[3]
            );
        }
        print_r($proxyServers);
    ?>
    

    $proxyServers 现在应该包含如下内容:

    Array
    (
        [0] => Array
            (
                [protocol] => http
                [address] => 127.0.0.1
                [port] => 8888
            )
    
        [1] => Array
            (
                [protocol] => https
                [address] => 127.0.0.1
                [port] => 8888
            )
    
    )
    

    【讨论】:

    • 好的,这是在为 Internet Explorer(互联网选项)设置的代理配置上选择的,但 curl 似乎没有看到该代理。它实际上是从 HTTP_PROXY 环境变量中获取代理。
    【解决方案3】:

    一种方法是,您可以向www.whatismyip.com 或类似站点发出 curl 请求,然后检查 html 响应以找出您正在使用的代理(如果您是的话)。

    【讨论】:

    • 我认为这行不通,因为我所说的可能的代理服务器仍在我的公司网络中,因此 IP 地址可能仍然相同。在任何情况下,我都想知道请求时代理的 IP 地址,而不是在外部互联网上显示的那样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    相关资源
    最近更新 更多