2017-01-30 13 views
0

我如何獲得基本授權標題並通過使用PHP對API進行身份驗證。我創建了一個編碼並存儲在數據庫中的唯一密鑰。我正在使用Slim Framework獲取其他API。我如何獲得基本授權標題,並通過使用php驗證它的api使用php

我可以通過在Slim Framework中使用$request->headers();來獲取標頭。它返回指定的所有標題,但是如何使用我的數據庫標記檢查該鍵。

有沒有適當的方法來做到這一點?

+1

1碼可以說超過一萬字..添加[MCVE] – Xorifelse

+0

你試過$請求 - > getHeader [$ nameOfHeaderKey]? – Rob

回答

0

我在超薄框架3這樣做是這樣的:

我加入了中間件的所有請求,並檢查Authorization頭對某些字符串,如果授權頭未設置或犯規我的字符串我只是匹配返回400個HTTP代碼。

$app->add(function($request,$response,$next){ 
$authorization_header = $request->getHeader("Authorization"); 

if(empty($authorization_header) || ($authorization_header[0]!="test")){ //you can check the header for a certain string or you can check if what is in the Authorization header exists in your database 

    return $response->withStatus(400); 
} 

$response = $next($request,$response); 

return $response; 

}); 
+0

你應該使用苗條的響應,而不是標題(),但''返回$ response-> withStatus(400);'' – jmattheis

+0

是的,你是對的......謝謝..我要編輯答案 –

+0

它的工作。謝謝 –

相關問題