Curl error 407 proxy authentication required

The script where I was grabbing the number of indexed pages from Google suddenly started giving me “Curl error 407 proxy authentication required” error. The script worked perfectly fine for over a year, so I’m not sure why it suddenly started giving the error.

After a lot of digging into Google, I found the following solution.

Add the code below in your curl_setopt.

curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);

Sample PHP code below:

$url = "http://www.google.com.au/search?q=site%3ahttps://www.shounakgupte.com";
$proxy = "some proxy IP address";
$proxyauth = "username:password";

$agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)';
$ch = curl_init();

curl_setopt($ch, CURLOPT_TIMEOUT_MS, 20000);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, '1');
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$data = curl_exec($ch);
curl_close($ch);

Did you face any similar issues? How did you solve them? Feel free to share your solutions below:

Say Hello! Don’t be shy.