【发布时间】:2019-10-29 14:15:12
【问题描述】:
我尝试使用 curl Basic Auth 向我的供应商 API 发出 HTTP 请求,如下所示:
//Server url
$url = "https://api.example.com/store/products?status=all&offset=0&limit=50";
$apiKey = 'xxxxxxx'; // apikey
$headers = array(
'Authorization: Basic '.$apiKey
);
// Send to Server
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Get response
$response = curl_exec($ch);
// Encode : decode returns string error so use encode
echo $result = json_encode($response);
使用此代码我得到错误:
"{\"code\":400,\"result\":\"格式错误的授权 header.\",\"error\":{\"reason\":\"BadRequest\",\"message\":\"格式错误 授权标头。\"}}"
我到处寻找解决方案 here 和 here。所有答案都建议添加我的供应商 API 不需要的用户名和密码,只有 Api Key 和标头 Basic Auth。我做错了什么?
【问题讨论】:
标签: php curl http-headers