php curl获取代理

I am using a PHP script in Windows to make a curl request to successfully make a SOAP request, and I'm trying to track down exactly how this successful request is made to replicate it in C#.NET.

How can I use PHP to detect which proxy server curl is going to use?

I was hoping there might be a curl_getopt in php curl, so I could do something like:

curl_getopt(CURLOPT_PROXY);

but alas it doesn't exist.

Is there any way for me to find out which proxy server php curl will be connecting through?

1. You tell curl what proxy to use, and not 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. Another solution >

And look at this answer > How to get an option previously set with curl_setopt()?

3. Override curl_setopt() function

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

p.s. you should probably need http://php.net/manual/en/function.rename-function.php

4. Use runkit to override functions

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

One way to do it is, you could make a curl request to www.whatismyip.com or a similar site and check the html response to find out the proxy you are using (if you are).

If it's using the same as your Windows configuration, you should be able to find it like this (untested, as I don't have a Windows server):

<?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 should now contain something like the following:

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

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

)