我正在尝试实现对PHP API的身份验证,但是每当我尝试获取
$request->getHeader('Authorization');
它总是返回空。
我尝试了多种解决方案,例如在项目内部编辑.htaccess文件:
RewriteEngine On
CGIPassAuth On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
但是没有成功。
那是中间件类:
<?php
namespace App\Middleware;
use App\Models\User;
class Authentication{
public function __invoke($request, $response, $next){
$auth = $request->getHeader('Authorization');
$_apikey = $auth[0];
$apikey = substr($_apikey, strpos($_apikey,'')+1);
$user = new User();
if(!$user->auth($apikey)){
return $response->withStatus(401);
}
$response = $next($request,$response);
return $response;
}
}
服务器的唯一输出是$ auth是一个空数组。
[2018年11月7日星期三09:39:09] PHP注意:未定义的偏移量:第13行的/var/www/html/slim_app/app/Middleware/Authentication.php中为0
[2018年11月7日星期三09:39:09] PHP警告:strpos():第14行的/var/www/html/slim_app/app/Middleware/Authentication.php中的空针头