Php Laravel作为代理和guzzle的cookie处理
交易是这样的,一个AngularJS应用程序向我的API(Laravel)发出登录后请求。然后,Laravel使用Guzzle向另一个API发出请求。此API返回一个cookie,Laravel将其发送给AngularJS 现在在AngularJS发出的后续请求中,这个cookie被发送,Laravel在后续的Guzzle请求中注入它 我的登录方式:Php Laravel作为代理和guzzle的cookie处理,php,laravel,cookies,laravel-5,guzzle,Php,Laravel,Cookies,Laravel 5,Guzzle,交易是这样的,一个AngularJS应用程序向我的API(Laravel)发出登录后请求。然后,Laravel使用Guzzle向另一个API发出请求。此API返回一个cookie,Laravel将其发送给AngularJS 现在在AngularJS发出的后续请求中,这个cookie被发送,Laravel在后续的Guzzle请求中注入它 我的登录方式: public function login(AuthRequest $request) { $credentials =
public function login(AuthRequest $request)
{
$credentials = $request->only('email', 'password');
$response = $this->httpClient->post('_session', [
'form_params' => [
'name' => $credentials['email'],
'password' => $credentials['password']
]
]);
return $this->respond($response->getHeader('Set-Cookie'));
}
如何“同步”Laravel饼干和Guzzle饼干
我正在使用Laravel 5和最新的Guzzle(6.0.1)。您可以尝试按照中的指定手动添加CookieJar。因此,您的客户的cookies将用于请求
$jar = new \GuzzleHttp\Cookie\CookieJar();
$client->request('GET', '/get', ['cookies' => $jar]);
我能够使用
一个简单的想法(我可能误解了这个问题)-在Laravel会话中保存Guzzle cookie并根据需要检索它。这是我尝试过的,但是在阅读关于Guzzle上的cookie的文档时,我没有找到为每个请求设置cookie的方法。这是Guzzle docs上关于饼干的唯一信息,是真的,这是我最终做的,完全忘记了回答我自己的问题
public function login($credentials){
$jar = new \GuzzleHttp\Cookie\CookieJar;
$response = CouchDB::execute('post','_session', [
'form_params' => [
'name' => 'email_'.$credentials['email'],
'password' => $credentials['password']
],
'cookies' => $jar
]);
$user = CouchDB::parseStream($response);
//Here I'm using the $jar to get access to the cookie created by the Guzzle request
$customClaims = ['name' => $credentials['email'], 'token' => $jar->toArray()[0]['Value']];
CouchDB::setToken($customClaims['token']);
$payload = \JWTFactory::make($customClaims);
$token = \JWTAuth::encode($payload);
$user->token = $token->get();
return $user;
}