2010
05.27

I’ve got a task yesterday, to submit, let’s say 200 sitemap.xml entries to different search engines, and I don’t want to do it with hand, of course.

The first option, that came into my mind was obviously php with curl.
The problem was, that at our company, we have a corporate proxy server, that handles outgoing connections. So this is not a transparent proxy, we need windows domain based (NTLM) authentication.

So, I’ll share with you the solution I found, I’ve while I was googling around.

Basically the curl thing, without the proxy would look like something like this:

1
2
3
4
5
6
7
8
    if (function_exists("curl_init"))
    {  
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        $out = curl_exec($ch);

        curl_close($ch);
    }

Pretty simple, actually.
Now, I add, the proxy handling stuff, some return echoing, output buffering. The last thing if only needed, if you plan to you is in a looong foreach, with many items, like I did.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    if (function_exists("curl_init"))
    {  
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
           
        curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
        curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
        curl_setopt($ch, CURLOPT_PROXY, "$proxyServer:$proxyPort");
        curl_setopt($ch, CURLOPT_PROXYUSERPWD, "$proxyUsername:$proxyPassword");

        $out = curl_exec($ch);
           
        print "error:" . curl_error($ch) . "<br />";
        print "output:" . $out . "<br /><br />";
           
        curl_close($ch);
           
        ob_flush();
        flush();
    }

This’ll ALMOST work, except, you need to set, the proxy options BEFORE this code.
Which could be:

1
2
3
4
$proxyServer="proxy.example.com";
$proxyPort="8080";
$proxyUsername='MYCOMPANY\djzone';
$proxyPassword="mypass";

If you don’t know your Windows Domain, which is the uppercased part in the $proxyUsername, just hit Ctrl + Alt + Del (at least it works this way on my XP SP2) and it’ll say something like:

“Zsolt Lakatos if logged in on as MYCOMPANY\djzone.”

That’s it :)

Update
I’ve seen recently another way to use $proxyUsername which is concatenating Windows Active Directory Domain and username for example: “MYCOMPANY+djzone”.
This might be a solution as well.

5 comments so far

Add Your Comment
  1. Thanks! This post precisely laid out exactly what I was looking for, saved me quite a bit of time :)

  2. Thank you! Will this method work for other proxy servers, other than Squid?

  3. You’re a friggin life-saver. You have no idea how long i’ve been googling for this (or maybe you do have an idea, which is why you posted it in the first place).

    I’m guessing the code will work for other proxy servers… It definitely worked for my ISA server.

  4. genius

  5. Very very long searched for a solution. Thank you very much. Best post. Simple and beautiful.