gpt4 book ai didi

laravel - 登录操作后在 Guzzle HTTP 客户端中设置 Authorization Bearer header - Laravel

转载 作者:行者123 更新时间:2023-12-04 08:54:48 24 4
gpt4 key购买 nike

我目前正在开发一个向 API 发出请求的 laravel 项目。对端点的每个请求都需要 header 中的 token 。

我创建了一个登录函数,当登录成功时,我想在每个对端点的请求的 header 中放置一个 token 。

我可以使用 Guzzle 做到这一点吗?

这是我的登录功能

public function login(Request $request)
{
$client = new Client();
$url = "http://localhost:8002/login";

$request = $client->post($url, [
'headers'=> ['Content-Type' => 'application/json'],
'body' => json_encode([
'email' => $request->email,
'password' => $request->password,
])

]);

$response = json_decode($request->getBody());
$token = $response->result->token; //I have got the token


}

最佳答案

根据您的情况,您应该使用任何数据存储在成功登录后保留您的授权 token 。 session 存储将是一个好的开始。

另外,根据您的 API 使用的授权类型(Bearer token、Basic auth...),向 Guzzle 请求添加 header 将如下所示:

$request = $client->post($url, [
'headers'=> ['Authorization' => 'your auth header with auth token'],
'body' => json_encode([
'foo' => 'bar',
])

]);

关于laravel - 登录操作后在 Guzzle HTTP 客户端中设置 Authorization Bearer header - Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63890456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com