2

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?

Robin Winslow
  • 10,908
  • 8
  • 62
  • 91

3 Answers3

3

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

Community
  • 1
  • 1
Glavić
  • 42,781
  • 13
  • 77
  • 107
  • Okay that's what I was afraid of. Unfortunately this doesn't solve my problem, as I haven't set the curl option in my code at all. So if I can't find this option from within PHP, is there any other way I can work out for definite what proxy option curl might be picking up from the OS? – Robin Winslow Sep 27 '12 at 10:10
  • In the end I found out through experimentation that a proxy was being set in the `HTTP_PROXY` environment variable, and that's what it was using. Overriding in PHP didn't work as I guess this is a lower level thing than PHP is aware of. Thanks for your help though, I'm gonna accept this answer for the wealth of options. – Robin Winslow Oct 01 '12 at 09:11
1

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
        )

)
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
  • 1
    Okay this picked up on the proxy config that was set for internet explorer (internet options), but curl doesn't seem to see that proxy. It was actually picking up on the proxy from the `HTTP_PROXY` environment variable. – Robin Winslow Oct 01 '12 at 09:10
0

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).

Prasanth
  • 5,230
  • 2
  • 29
  • 61
  • I don't think that would work, as the likely proxy server I'm talking about is still within my corporate network so the IP address would likely be the same anyway. And in any case I want to know the IP address of the proxy at the point of request not as it appears to the outside internet. – Robin Winslow Sep 27 '12 at 10:08