18

I would like to use a cURL function, but I'm behind a proxy, so I get an HTTP/1.1 407 Proxy Authentication Required error...

This is the php code I use:

$proxy_user = 'Michiel';
$proxy_pass = 'mypassword';
$proxy_url = 'myproxyurl:port';
$proxy = true;

$service_url = "https://www.myapiurltocall.com";
$service_user = 'user:password:FO';
$service_pass = 'password';

$ch = curl_init($service_url);

// Set proxy if necessary
if ($proxy) {
    curl_setopt($ch, CURLOPT_PROXY, $proxy_url);
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy_user.':'.$proxy_pass);
    curl_setopt($ch, CURLOPT_PROXYPORT, 8080);
    curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_NTLM);
}

// Set service authentication
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, "{$service_user}:{$service_pass}");

// HTTP headers
$headers['Authorization'] = 'Basic ' . base64_encode("$proxy_user:$proxy_pass");

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);    
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_TIMEOUT, 15);

//WARNING: this would prevent curl from detecting a 'man in the middle' attack
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);

$data = curl_exec($ch);

And I don't know what I do wrong... How can I get around this error?

Michiel
  • 7,855
  • 16
  • 61
  • 113
  • 1
    assuming the `$proxy_user:$proxy_pass` are the correct values, if you are behind an office firewall the authentication credentials to be used would be that of an administrator and not your own...I've encountered something very similar in my work environment and that was the case for me as my normal credentials didn't have the necessary privileges. – optimusprime619 Mar 22 '12 at 12:32
  • Yep, behind an office firewall. The $proxy_user and $proxy pass are 100% correct (I can use my webmail and communicator with these credentials, so...) The administrator of my PC or of the network you mean? – Michiel Mar 22 '12 at 12:35
  • Ok, great! Please add it as an answer, so I can accept it :) – Michiel Mar 23 '12 at 10:36

4 Answers4

28

This is what I'm mostly using :

function curlFile($url,$proxy_ip,$proxy_port,$loginpassw)
{
    //$loginpassw = 'username:password';
    //$proxy_ip = '192.168.1.1';
    //$proxy_port = '12345';
    //$url = 'http://www.domain.com';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
    curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
    curl_setopt($ch, CURLOPT_PROXY, $proxy_ip);
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, $loginpassw);
    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}
Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
4

This is what I use and it works perfectly:

$proxy = "1.1.1.1:12121";
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
$url = "http://www.google.pt/search?q=anonymous";
$credentials = "user:pass"

$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,15);
curl_setopt($ch, CURLOPT_HTTP_VERSION,'CURL_HTTP_VERSION_1_1' );
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYUSERPWD,$credentials);
curl_setopt($ch, CURLOPT_USERAGENT,$useragent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
$result=curl_exec ($ch);
curl_close ($ch);
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
3

Try to implement this in your function

   function send_string($data) {
//       pr($data);exit;

     $equi_input="Your values";
//  echo $equi_input; exit;
        $agent = ""//Agent Setting for Netscape  
        $url = ""; // URL to POST FORM. (Action of Form)
        //xml format
         $efx_request=strtoupper($equi_input);
      //echo  $efx_request;exit;
        $post_fields = "site_id=""&service_name=""&efx_request=$efx_request";
        $credentials = ;
        $headers = array("HTTP/1.1",
            "Content-Type: application/x-www-form-urlencoded",
            "Authorization: Basic " . $credentials
        );
        $fh = fopen('/tmp/test.txt', 'w') or die("can't open file"); //File to write the header information of the curl request.
        $ch = curl_init();    // Initialize a CURL session.
        curl_setopt($ch, CURLOPT_USERAGENT, $agent);
        curl_setopt($ch, CURLOPT_URL, $url);  // Pass URL as parameter.
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_STDERR, $fh);
        curl_setopt($ch, CURLOPT_POST, 1); // use this option to Post a form  
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); // Pass form Fields.  
        curl_setopt($ch, CURLOPT_VERBOSE, '1');
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, '1');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);  // grab URL and pass it to the variable.
        curl_close($ch);  // close curl resource, and free system resources.  
        fclose($fh);


   //echo '<pre>';print_r($array);echo '</pre>';exit;
        if (!empty($result)) {
            return $result;
        } else {
            echo "Curl Error" . curl_error($ch);
        }
    }

check request send 200ok ...

And check your secure certificate if you are using....

Thanks.

davehale23
  • 4,374
  • 2
  • 27
  • 40
Javascript Coder
  • 5,691
  • 8
  • 52
  • 98
  • Thanks for the answer, but I'm not sure how to implement this in my current code. Could you help me out here? – Michiel Mar 22 '12 at 12:31
  • i am also passing your variables value like credentials , Url ,Authorization .. i am passing authorization in $credentials variable .. i am passing username , password in postfield..,, read code , i think it will help you. – Javascript Coder Mar 22 '12 at 12:35
  • main thing first check your authentication credentials , may be that are creating problem for you... check .. – Javascript Coder Mar 22 '12 at 12:37
  • Please note a bug in the above code: `CURLOPT_SSL_VERIFYHOST` should be an integer parameter, and should be set to 2 (the default). See http://www.php.net/manual/en/function.curl-setopt.php for more details. If you set the value to `true`, you're actually disabling host checking, and therefore enabling MITM attacks! – simpleigh Nov 10 '12 at 13:57
1

Assuming the $proxy_user:$proxy_pass are the correct values, if you are behind an office firewall the authentication credentials to be used would be that of a network administrator and not your own...I've encountered something very similar in my work environment and that was the case for me as my normal credentials didn't have the necessary privileges. The best way to ensure that this is the case however would be to ensure the code works in an open network with your proxy settings commented. Hope this helps!

optimusprime619
  • 754
  • 2
  • 17
  • 38