我正在使用API,它为我提供了用于连接的令牌。它给出了这些说明。
然后,此标记将在标头变量Auth Digest中的所有后续调用中发送。
我不确定这意味着什么。我已经尝试了几种方法并阅读了几个堆栈溢出问题。这是我尝试过的。有关详细信息,请参阅代码注释。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
// I have tried setting both of these
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
// In combination of the above separately, I have tried each of these individually
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $token);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Auth Digest: ' . $token));
curl_setopt($ch, CURLOPT_POST, true);
$post_data = array('Auth Digest' => $token);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
curl_setopt($ch, CURLOPT_USERPWD, $token);
// Then I execute and close, either giving me a failed response and a response that says token is not valid
$response = curl_exec($ch);
$header_sent = curl_getinfo($ch, CURLINFO_HEADER_OUT);
if (!$response) {
echo $action . ' curl request failed';
return false;
}
curl_close($ch);
$response_json = json_decode($response);
var_dump($response_json);
以下是一些相关的stackoverflow问题,我试图在没有成功的情况下应用于我的问题。
Curl request with digest auth in PHP for download Bitbucket private repository
Client part of the Digest Authentication using PHP POST to Web Service
How do I make a request using HTTP basic authentication with PHP curl?
我需要知道他们可能期望的原始http标头是什么,或者我如何使用php curl来生成他们可能期望的标头。
答案 0 :(得分:1)
摘要授权标头通常如下所示:
Authorization: Digest _data_here_
所以在你的情况下,试试:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//... existing options here
$headers = array(
'Authorization: Digest ' . $token,
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
如果您使用CURLOPT_HTTPHEADER
,则只需指定要发送的其他标头,并且不需要您在其中添加所有标头。
如果您发送的其他标题都有明确的选项,请使用这些标题,然后将一个授权标题传递给CURLOPT_HTTPHEADER
。