title: Proxy Authentication Required with cURL
body: <p>I would like to use a cURL function, but I'm behind a proxy, so I get an <code>HTTP/1.1 407 Proxy Authentication Required</code> error...</p>
  
  <p>This is the php code I use:  </p>
  
  <pre><code>$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);
  </code></pre>
  
  <p>And I don't know what I do wrong... How can I get around this error?</p>