脚本宝典收集整理的这篇文章主要介绍了php – GuzzleHttp:如何从POST响应中保存cookie并在下一次POST中使用它?,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
$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:@H_301_4@
$newCookies = $response->getHeader('set-cookie');
现在,我需要在接下来的调用中使用这个cookie,我知道Guzzle可以为我保存cookie并使用“cookiejar”或“SessionCookieJar”在下次调用中自动发送(或不发送),我试过使用但是我没有在’jar’中看到cookie,这就是我所做的:@H_301_4@
$cookieJar = new SessionCookieJar('SESSION_STORAGE',true); $client = new Client([ 'cookies' => $cookieJar ]); $response = $client->request ....
但是,当我从POST中获取cookie时,我只能通过以下方式看到它:@H_301_4@
$newCookies = $response->getHeader('set-cookie');
它不在cookieJar中,因此它不会在下次调用时发送它.
我在这里错过了什么?@H_301_4@
谢谢!@H_301_4@
$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');
要么@H_301_4@
$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');
以上是脚本宝典为你收集整理的php – GuzzleHttp:如何从POST响应中保存cookie并在下一次POST中使用它?全部内容,希望文章能够帮你解决php – GuzzleHttp:如何从POST响应中保存cookie并在下一次POST中使用它?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。