我正在使用 curl 使用 PHP 构建一个请求,对于基本身份验证,必须使用抢先式身份验证。我可以强制 curl 使用抢先身份验证吗?
或者有没有其他方法可以用 PHP 构建我的请求?
@巴斯范斯坦
我不确定我是否理解正确。我试过这样:
$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);
但它不起作用。
谢谢
卡祖