I keep getting "Bad API Key" response while the same API key is working fine without a problem in Postman
Here is my code
$apiKey ='MY API KEY';
$url = "MY URL";
$client = curl_init($url);
curl_setopt($client,CURLOPT_RETURNTRANSFER,true);
//curl_setopt($client, CURLOPT_USERPWD, "$username:$password");
curl_setopt($client, CURLOPT_HTTPHEADER, array('Authorization: ' . $apiKey));
//curl_setopt($client, CURLOPT_HTTPHEADER, array("X-API-Key: $apiKey"));
$response = curl_exec($client);
$result = json_decode($response);
print_r($result);
CodePudding user response:
The authorization
header normally comes in different "flavors", or authorization schemes. Since you are sending a token, I expect you want to send a Bearer
request (which is handled automatically by Postman, if you are using the integrated authorization feature). So you have to slightly adapt your curl_setopt
:
curl_setopt($client, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $apiKey));
CodePudding user response:
Check the double quotes with your url. The problem might be with your url.