我第一次使用laravel来消费外部api。我需要使用外部api进行身份验证,而不是使用Laravel身份验证。我已经使用失眠症rest客户端测试了端点,得到了200(OK),这是很棒的。为了在Laravel上完成它,我做了以下工作:
我还进口了:
use GuzzleHttp\Client;我在文件:app/Http/Controllers/Auth/AuthenticatedSessionController中修改了存储方法,并将存储方法更改为
public function store(LoginRequest $request)
{
$client = new Client(['base_uri'=>'http://www.abcde.com']);
$response = $client->post('/api/token',['auth'=>[$request->input('email',$request->input('password'))]]);
dd(($response)->getStatusCode());
}但我知道错误是:
未定义偏移量:1
我现在卡住了,不知道该怎么做。
编辑的api数据结构:
{
"email": "greentwigg@gmail.com",
"password": "Password0001",
}发布于 2021-01-07 20:28:18
我要修改一下你的代码。如果需要包含标头,则需要将此格式放置在适当的位置。
public function store(LoginRequest $request)
{
$client = new Client();
$response = $client->request('POST','http://www.abcde.com/api/token'),[
'json' => [
'email' => $request->input('email'),
'password' => $request->input('password'),
],
'headers' => [
'Content-Type' => 'application/json',
]
]);
dd(($response)->getStatusCode());
}https://stackoverflow.com/questions/65614794
复制相似问题