php curl密匙已获取到 如何验证,PHP:在CURL GET Call中使用API​​密钥

我看过使用api密钥验证curl中的post调用的帖子.我有一个GET调用,需要apikey进行授权,即请求必须有一个授权标头来保存apiKey.我已经获得了api密钥并尝试将其用于GET调用:

$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);

?>

我在json响应中收到错误:

{"error":true,"message":"Api key is misssing"}

我尝试了一些其他的方式,如传递一个标题数组,但我不断收到错误.

如何正确获取curl_response?我应该如何传递使用api密钥的Authorization标头?

我正在进行的get调用的api是(使用Slim Library创建的):

index.php

/**

* Listing all tasks of particual user

* method GET

* url /tasks

*/

$app->get('/tasks', 'authenticate', function() {

global $user_id;

$response = array();

$db = new DbHandler();

// fetching all user tasks

$result = $db->getAllUserTasks($user_id);

$response["error"] = false;

$response["tasks"] = array();

// looping through result and preparing tasks array

while ($task = $result->fetch_assoc()) {

$tmp = array();

$tmp["id"] = $task["id"];

$tmp["task"] = $task["task"];

$tmp["status"] = $task["status"];

$tmp["createdAt"] = $task["created_at"];

array_push($response["tasks"], $tmp);

}

echoRespnse(200, $response);

});

验证功能是:

in the same index.php file

/**

* Adding Middle Layer to authenticate every request

* Checking if the request has valid api key in the 'Authorization' header

*/

function authenticate(\Slim\Route $route) {

// Getting request headers

$headers = apache_request_headers();

$response = array();

$app = \Slim\Slim::getInstance();

// Verifying Authorization Header

if (isset($headers['Authorization'])) {

$db = new DbHandler();

// get the api key

$api_key = $headers['Authorization'];

// validating api key

if (!$db->isValidApiKey($api_key)) {

// api key is not present in users table

$response["error"] = true;

$response["message"] = "Access Denied. Invalid Api key";

echoRespnse(401, $response);

$app->stop();

} else {

global $user_id;

// get user primary key id

$user = $db->getUserId($api_key);

if ($user != NULL)

$user_id = $user["id"];

}

} else {

// api key is missing in header

$response["error"] = true;

$response["message"] = "Api key is misssing";

echoRespnse(400, $response);

$app->stop();

}

}

解决方法:

好吧所以它应该非常简单…你能尝试添加:

curl_setopt($curl, CURLOPT_HTTPHEADER, array(

'Authorization: ' . $apiKey

));

你的卷曲?之后,在authenticate()函数中执行print_r($headers)以查看是否收到它.

标签:json,php,rest,curl,api

来源: https://codeday.me/bug/20190715/1466853.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值