【问题标题】:cURL with both Basic and Bearer authentication headers具有基本和不记名身份验证标头的 cURL
【发布时间】:2019-11-19 10:35:04
【问题描述】:

我正在使用 PHP cURL 来测试一个位于受 Apache HTPASSWD 保护的开发站点上的 API。

$jwt = // a standard JWT token        

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://dev.mysite.com/api');
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, 'myuser:mypass');
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer '.$jwt));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curl);

但是,设置 HTTPHEADER 似乎会覆盖 HTPASSWD 所需的基本身份验证,因为调用返回 401 UNAUTHORIZED。

我也试过(没有成功):

curl_setopt($curl, CURLOPT_URL, 'https://myuser:mypass@dev.mysite.com/api');

有没有一种方法可以使用 cURL 在 API 调用中包含这两个标头值?还是我在这里做错了什么?

【问题讨论】:

    标签: php curl http-headers .htpasswd


    【解决方案1】:

    https://www.rfc-editor.org/rfc/rfc6750 表示可以使用 Bearer 作为

    Form-Encoded Body 参数:授权:Bearer mytoken123

    URI查询参数:a​​ccess_token=mytoken123

    因此,一个选项是将令牌设置为查询参数。

    【讨论】:

    猜你喜欢
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    相关资源
    最近更新 更多