我正在使用curl使用PHP构建请求,对于基本身份验证,必须使用抢先身份验证。 我可以强制curl使用抢先身份验证吗?
或者还有其他方法可以用PHP构建我的请求吗?
@Bas van Stein
我不确定我是否理解正确。我试过这样的话:
$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);
但它不起作用。
由于
katzu