当在运行时动态给出的承载密钥时,它显示错误意外的HTTP代码401 。当它以静态方式给出时,CURL正在成功运行。请帮助解决此问题。
$s=$new_api_fields['key']; // key loaded from DB
$authorization = "Authorization: Bearer ".$s; //Not working (Unexpected HTTP code 401)
(OR)
$authorization = "Authorization: Bearer XXXXXXXXX"; //Working fine
$local_file = 'Report/Report.csv';
$fp = fopen ($local_file, 'w+');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/Csv', $authorization));
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_exec($ch);
答案 0 :(得分:0)
您可以执行$authorization = "Authorization: Bearer $s";
卷曲标题集可能有问题。
您可以像以下
一样设置curl Headercurl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/Csv",
"Authorization: Bearer $s"
));
我刚试过它。它应该工作