我正在使用 tymondesigns / jwt-auth 包,在尝试使用curl对用户进行身份验证时,我遇到了一个非常奇怪的问题。 JWTAuth :: attempt($ credentials)始终返回false,即使凭据是正确的原因。我在firefox上使用HttpRequester插件测试它,它可以使用它,它返回令牌,为什么它不适用于卷曲?
这是验证码
try {
//i checked that the credentials are correctly received at this point, even when using curl
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) { }
你之前见过类似的东西吗?
这是我在客户端使用的卷曲
//set headers
$headers=[];
if($method == 'post'){
$headers[]= 'Content-Type: application/x-www-form-urlencoded';
}
else{
$headers[]='Content-Type: text/plain; charset=UTF-8';
}
if(isset($apiToken) && $apiToken != ''){
$headers[]= 'Authorization: Bearer '.$apiToken;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if($method == 'post'){
curl_setopt($ch, CURLOPT_POST, true);
if(isset($parameters)){
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters));
}
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
$result['content'] = curl_exec($ch);
$ parameters 数组只包含电子邮件和密码,据我所知 JWTAuth :: attempt()只需要那些工作(事实上)它可以在Firefox HttpRequester中运行)也许它与laravel的后卫系统有关吗?
更新
我已经使用GET方法而不是POST测试但是我得到了相同的结果,它没有授权
我刚刚使用命令行curl测试它并且它正在工作,这就是我所做的:
curl -X GET 'http://myapi.cc/api/auth?email=XXXXXX&password=XXXXX'
显然, PHP Curl 会导致此问题