我目前正在开发一个 Laravel 项目来向 API 发出请求。对端点的每个请求都需要标头中的令牌。
我创建了一个用于登录的函数,当登录成功时,我想在对端点的每个请求的标头中放置一个令牌。
我可以使用 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
}
Run Code Online (Sandbox Code Playgroud)