我看过使用api密钥验证curl中的post调用的帖子.我有一个GET调用,需要apikey进行授权,即请求必须有一个授权标头来保存apiKey.我已经获得了api密钥并尝试将其用于GET调用:
<?php
$service_url = 'http://localhost/finals/task_manager/v1/tasks/Authorization:'.$apiKey;
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded1 = json_decode($curl_response,true);
if (isset($decoded1->response->status) && $decoded1->response->status == 'ERROR') {
die('error occured: ' . $decoded1->response->errormessage);
}
echo 'response ok!';
var_export($decoded1->response);
?>
Run Code Online (Sandbox Code Playgroud)
我在json响应中收到错误:
{"error":true,"message":"Api key is misssing"}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了一些其他方式,如传递标头数组,但我不断收到错误.如何正确获取curl_response?我应该如何传递使用api密钥的Authorization标头?
我正在进行的get调用的api是(使用Slim Library创建的):
index.php
/**
* Listing all tasks of particual user
* method GET
* url /tasks
*/ …Run Code Online (Sandbox Code Playgroud)