我正在使用Guzzle登录我的API站点,并且在我使用正确的凭据登录的那一刻,我得到一个带有RefreshToken的cookie,以便在下次调用时发送它,这是我的简单(并且运行良好)代码:
$client = new Client(array( 'cookies' => true )); $response = $client->request('POST','http://myapi.com/login',[ 'timeout' => 30,'form_params' => [ 'email' => $request->get('email'),'password' => $request->get('password'),] ]);
我用cookie回复了正确的回复,我可以通过以下方式看到cookie:
$newCookies = $response->getHeader('set-cookie');
现在,我需要在接下来的调用中使用这个cookie,我知道Guzzle可以为我保存cookie并使用“CookieJar”或“SessionCookieJar”在下次调用中自动发送(或不发送),我试过使用但是我没有在’jar’中看到cookie,这就是我所做的:
$cookieJar = new SessionCookieJar('SESSION_STORAGE',true); $client = new Client([ 'cookies' => $cookieJar ]); $response = $client->request ....
但是,当我从POST中获取cookie时,我只能通过以下方式看到它:
$newCookies = $response->getHeader('set-cookie');
它不在cookieJar中,因此它不会在下次调用时发送它.
我在这里错过了什么?
谢谢!
根据文档
here,[‘cookies’=> true]表示对所有请求使用共享cookie jar,而[‘cookies’=> $jar]表示使用特定的cookie jar($jar)与客户的请求/响应一起使用.所以你需要使用:
原文链接:https://www.f2er.com/php/136912.html$client = new Client(array( 'cookies' => true )); $response = $client->request('POST',[ 'timeout' => 30,'form_params' => [ 'email' => $request->get('email'),] ]); // and using the same client $response = $client->request('GET','http://myapi.com/next-url'); // or elsewhere ... $client = new Client(array( 'cookies' => true )); $response = $client->request('GET','http://myapi.com/next-url');
要么
$jar = new CookieJar; $client = new Client(array( 'cookies' => $jar )); $response = $client->request('POST','http://myapi.com/next-url'); // or elsewhere ... $client = new Client(array( 'cookies' => $jar // the same $jar as above )); $response = $client->request('GET','http://myapi.com/another-url');