1

I'm building a request with PHP using curl, for the basic authentication it is necessary to use the preemptive authentication. Can I force curl to use the preemptive authentication?

Or is there any other way to build my request with PHP?

@Bas van Stein

I'm not sure if I understand this correctly. I tried it like this:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// auth header
$headers = array(
    sprintf('Authorization: Basic %s',base64_encode(sprintf("%s:%s",$username,$password))),
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);

// preemptive authentication (first request)
$result = curl_exec($ch);

// extend header for payload
$boundary = $this->generateRandomString();
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge_values($headers,array(
    'Content-Encoding: gzip',
    'Content-Type: multipart/mixed;boundary=' . $boundary,
)));

curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
$multiPart = sprintf("--%s\r\nContent-Disposition: content\r\nContent-Type: application/xx.xx\r\nContent-Version: 1.5\r\n\r\n%s\r\n--%s--",$boundary,$data,$boundary);

curl_setopt($ch, CURLOPT_POSTFIELDS, gzencode($multiPart));


// request with payload (second request)
$result = curl_exec($ch);
curl_close($ch);

but it doesn't worked.

Thanks

katzu

thoss
  • 81
  • 8
  • 1
    Did you see: http://stackoverflow.com/questions/14360534/get-curl-to-preemptively-send-authorization-header-for-digest-authentication-in? Basically you can send the authentication request first and then reuse the curl handler. – Niki van Stein May 13 '16 at 10:17

0 Answers0