我如何通过授权头使用cURL?(可在/usr/bin/curl中执行)。


当前回答

注意,当你使用: curl -H "授权:token_str" http://www.example.com

token_str和Authorization必须用空格隔开,否则服务器端将无法获得HTTP_AUTHORIZATION环境。

其他回答

注意,当你使用: curl -H "授权:token_str" http://www.example.com

token_str和Authorization必须用空格隔开,否则服务器端将无法获得HTTP_AUTHORIZATION环境。

(对于那些正在寻找php-curl答案的人)

$service_url = 'https://example.com/something/something.json';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password"); //Your credentials goes here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //IMP if the url has https and you don't want to verify source certificate

$curl_response = curl_exec($curl);
$response = json_decode($curl_response);
curl_close($curl);

var_dump($response);

对于那些做基于令牌的身份验证的人…一定要做到:

curl -H "AuthToken: "

相反! !

这招对我很管用:

curl -H "Authorization: Bearer xxxxxxxxxxxxxx" https://www.example.com/

HTTP基本认证:

curl -H "Authorization: Basic <_your_token_>" http://www.example.com

替换_your_token_和URL。