简体   繁体   English

在PHP curl请求中使用Auth Digest标头变量

[英]Using Auth Digest header variable in PHP curl request

I am working with an API that provides a token for me to use to connect. 我正在使用一个API,该API为我提供了用于连接的令牌。 It gives these instructions. 它给出了这些指示。

This token is then sent in all subsequent calls to the server in header variable Auth Digest. 然后,该令牌在所有后续调用中以标头变量Auth Digest发送到服务器。

I am not sure what this means. 我不确定这意味着什么。 I've tried several approaches and read several stack overflow questions. 我尝试了几种方法并阅读了一些堆栈溢出问题。 Here's what I've tried. 这是我尝试过的。 See code comments included for details. 有关详细信息,请参见代码注释。

$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);

Here are some related stackoverflow questions that I've tried to apply to my problem without success. 这是一些我尝试应用于我的问题但没有成功的相关stackoverflow问题。

Curl request with digest auth in PHP for download Bitbucket private repository 使用PHP中的摘要身份验证进行卷发请求以下载Bitbucket专用存储库

Client part of the Digest Authentication using PHP POST to Web Service 使用PHP POST到Web服务的摘要身份验证的客户端部分

How do I make a request using HTTP basic authentication with PHP curl? 如何使用HTTP curl和HTTP curl进行请求?

I need to know what the raw http header that they're likely expecting or how I can use php curl to generate the header they're likely expecting. 我需要知道他们可能期望的原始http标头,或者如何使用php curl生成他们可能期望的标头。

An digest authorization header typically looks like: 摘要授权标头通常如下所示:

Authorization: Digest _data_here_

So in your case, try: 因此,在您的情况下,请尝试:

$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);

If you use CURLOPT_HTTPHEADER , that just specifies additional headers to send and doesn't require you to add all the headers there. 如果使用CURLOPT_HTTPHEADER ,则仅指定要发送的其他标头,而不需要您在此处添加所有标头。

If the other headers you're sending all have explicit options, use those and just pass the one authorization header to CURLOPT_HTTPHEADER . 如果要发送的其他标头都具有显式选项,请使用这些标头,然后将一个授权标头CURLOPT_HTTPHEADERCURLOPT_HTTPHEADER

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM