Slim 框架的身份验证不起作用
Authentication for Slim framework not working
我正在使用 Slim 框架创建 restful api。
我能够创建一个 get api,但是当我向 get 请求添加身份验证时,它会抛出一个错误(Advanced Rest client google chrome 扩展),在这里是错误:
<html><head><title>Slim Application Error</title><style>body{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana,sans-serif;}h1{margin:0;font-size:48px;font-weight:normal;line-height:48px;}strong{display:inline-block;width:65px;}</style></head><body><h1>Slim Application Error</h1><p>The application could not run because of the following error:</p><h2>Details</h2><div><strong>Type:</strong> ErrorException</div><div><strong>Code:</strong> 8</div><div><strong>Message:</strong> Undefined variable: apiKey</div><div><strong>File:</strong> /Library/WebServer/Documents/pascal_api/rest_api/v1/index.php</div><div><strong>Line:</strong> 34</div><h2>Trace</h2><pre>#0 /Library/WebServer/Documents/pascal_api/rest_api/v1/index.php(34): Slim\Slim::handleErrors(8, 'Undefined varia...', '/Library/WebSer...', 34, Array)
#1 [internal function]: authenticate(Object(Slim\Route))
#2 /Library/WebServer/Documents/pascal_api/rest_api/libs/Slim/Route.php(433): call_user_func_array('authenticate', Array)
#3 /Library/WebServer/Documents/pascal_api/rest_api/libs/Slim/Slim.php(1307): Slim\Route->dispatch()
#4 /Library/WebServer/Documents/pascal_api/rest_api/libs/Slim/Middleware/Flash.php(85): Slim\Slim->call()
#5 /Library/WebServer/Documents/pascal_api/rest_api/libs/Slim/Middleware/MethodOverride.php(92): Slim\Middleware\Flash->call()
#6 /Library/WebServer/Documents/pascal_api/rest_api/libs/Slim/Middleware/PrettyExceptions.php(67): Slim\Middleware\MethodOverride->call()
#7 /Library/WebServer/Documents/pascal_api/rest_api/libs/Slim/Slim.php(1254): Slim\Middleware\PrettyExceptions->call()
#8 /Library/WebServer/Documents/pascal_api/rest_api/v1/index.php(100): Slim\Slim->run()
#9 {main}</pre></body></html>
这是我的代码:
<?php
require '.././libs/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
function isValidApiKey($api_key) {
$m = new MongoClient();
$db = $m->pascal;
$collection = $db->apiUsers;
if($collection->findOne(array('apiKey' => $api_key))){
return true;
}
else{
return false;
}
}
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'])) {
// get the api key
$api_key = $headers['Authorization'];
echo $apiKey;
// validating api key
//$db = new dbSupport();
if ($isValidApiKey($api_key) === false) {
// api key is not present
$response["error"] = true;
$response["message"] = "Access Denied. Invalid Api key";
echoRespnse(401, $response);
$app->halt(401);
}
else{
}
} else {
// api key is missing in header
$response["error"] = true;
$response["message"] = "Api key is misssing";
echoRespnse(400, $response);
$app->halt(401);
}
}
$app->get('/offerData','authenticate',function() use ($app) {
$m = new MongoClient();
$db = $m->pascal;
$collection = $db->offerDetails;
$offer_array = array();
$cursor = $collection->find();
$offer_array["offers"] = array();
foreach ($cursor as $document) {
$offerData = array('title' => $document['title'],
'discription' => $document['discription'],
'create_time' => $document['create_time'],
'expire_time' => $document['expire_time'],
'coordinates' => $document['loc']['coordinates'],
'address' => $document['address'],
'tags' => $document['tags'],
'phone_number' => $document['phone_number'],
'email' => $document['email'],
'website' => $document['website'],
'img_url' => $document['img_url']
);
array_push($offer_array["offers"], $offerData);
}
$offer_array["error"] = false;
echoRespnse(200, $offer_array);
});
function echoRespnse($status_code, $response) {
$app = \Slim\Slim::getInstance();
// Http response code
$app->status($status_code);
// setting response content type to json
$app->contentType('application/json');
echo json_encode($response);
}
$app->run();
?>
知道是什么导致了这个错误吗??
谢谢
请在 $headers 上 运行 print_r 命令,看看你在那里得到了什么。我认为密钥未设置或根本没有出现在您的代码中
好的,然后在第 28 行之前执行此操作:
$api_key=null ;
试试这个,然后告诉我。
这是错误:
$api_key = $headers['Authorization'];
echo $apiKey;
但是回显打印了错误的变量。应该是
echo $api_key;
我正在使用 Slim 框架创建 restful api。
我能够创建一个 get api,但是当我向 get 请求添加身份验证时,它会抛出一个错误(Advanced Rest client google chrome 扩展),在这里是错误:
<html><head><title>Slim Application Error</title><style>body{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana,sans-serif;}h1{margin:0;font-size:48px;font-weight:normal;line-height:48px;}strong{display:inline-block;width:65px;}</style></head><body><h1>Slim Application Error</h1><p>The application could not run because of the following error:</p><h2>Details</h2><div><strong>Type:</strong> ErrorException</div><div><strong>Code:</strong> 8</div><div><strong>Message:</strong> Undefined variable: apiKey</div><div><strong>File:</strong> /Library/WebServer/Documents/pascal_api/rest_api/v1/index.php</div><div><strong>Line:</strong> 34</div><h2>Trace</h2><pre>#0 /Library/WebServer/Documents/pascal_api/rest_api/v1/index.php(34): Slim\Slim::handleErrors(8, 'Undefined varia...', '/Library/WebSer...', 34, Array)
#1 [internal function]: authenticate(Object(Slim\Route))
#2 /Library/WebServer/Documents/pascal_api/rest_api/libs/Slim/Route.php(433): call_user_func_array('authenticate', Array)
#3 /Library/WebServer/Documents/pascal_api/rest_api/libs/Slim/Slim.php(1307): Slim\Route->dispatch()
#4 /Library/WebServer/Documents/pascal_api/rest_api/libs/Slim/Middleware/Flash.php(85): Slim\Slim->call()
#5 /Library/WebServer/Documents/pascal_api/rest_api/libs/Slim/Middleware/MethodOverride.php(92): Slim\Middleware\Flash->call()
#6 /Library/WebServer/Documents/pascal_api/rest_api/libs/Slim/Middleware/PrettyExceptions.php(67): Slim\Middleware\MethodOverride->call()
#7 /Library/WebServer/Documents/pascal_api/rest_api/libs/Slim/Slim.php(1254): Slim\Middleware\PrettyExceptions->call()
#8 /Library/WebServer/Documents/pascal_api/rest_api/v1/index.php(100): Slim\Slim->run()
#9 {main}</pre></body></html>
这是我的代码:
<?php
require '.././libs/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
function isValidApiKey($api_key) {
$m = new MongoClient();
$db = $m->pascal;
$collection = $db->apiUsers;
if($collection->findOne(array('apiKey' => $api_key))){
return true;
}
else{
return false;
}
}
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'])) {
// get the api key
$api_key = $headers['Authorization'];
echo $apiKey;
// validating api key
//$db = new dbSupport();
if ($isValidApiKey($api_key) === false) {
// api key is not present
$response["error"] = true;
$response["message"] = "Access Denied. Invalid Api key";
echoRespnse(401, $response);
$app->halt(401);
}
else{
}
} else {
// api key is missing in header
$response["error"] = true;
$response["message"] = "Api key is misssing";
echoRespnse(400, $response);
$app->halt(401);
}
}
$app->get('/offerData','authenticate',function() use ($app) {
$m = new MongoClient();
$db = $m->pascal;
$collection = $db->offerDetails;
$offer_array = array();
$cursor = $collection->find();
$offer_array["offers"] = array();
foreach ($cursor as $document) {
$offerData = array('title' => $document['title'],
'discription' => $document['discription'],
'create_time' => $document['create_time'],
'expire_time' => $document['expire_time'],
'coordinates' => $document['loc']['coordinates'],
'address' => $document['address'],
'tags' => $document['tags'],
'phone_number' => $document['phone_number'],
'email' => $document['email'],
'website' => $document['website'],
'img_url' => $document['img_url']
);
array_push($offer_array["offers"], $offerData);
}
$offer_array["error"] = false;
echoRespnse(200, $offer_array);
});
function echoRespnse($status_code, $response) {
$app = \Slim\Slim::getInstance();
// Http response code
$app->status($status_code);
// setting response content type to json
$app->contentType('application/json');
echo json_encode($response);
}
$app->run();
?>
知道是什么导致了这个错误吗?? 谢谢
请在 $headers 上 运行 print_r 命令,看看你在那里得到了什么。我认为密钥未设置或根本没有出现在您的代码中
好的,然后在第 28 行之前执行此操作:
$api_key=null ;
试试这个,然后告诉我。
这是错误:
$api_key = $headers['Authorization'];
echo $apiKey;
但是回显打印了错误的变量。应该是
echo $api_key;