Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 暴食饼干处理_Php_Rest_Session_Cookies_Guzzle - Fatal编程技术网

Php 暴食饼干处理

Php 暴食饼干处理,php,rest,session,cookies,guzzle,Php,Rest,Session,Cookies,Guzzle,我正在构建一个基于Guzzle的客户端应用程序。我对饼干的处理有点厌倦了。我正在尝试使用实现它,但我无法让它工作。我的客户端应用程序是标准的web应用程序,只要我使用同一个guzzle对象,它看起来就可以工作,但在请求之间,它不会发送正确的cookies。我使用FileCookieJar存储cookies。如何在多个guzzle对象之间保存cookie // first request with login works fine $cookiePlugin = new CookiePlugin(

我正在构建一个基于Guzzle的客户端应用程序。我对饼干的处理有点厌倦了。我正在尝试使用实现它,但我无法让它工作。我的客户端应用程序是标准的web应用程序,只要我使用同一个guzzle对象,它看起来就可以工作,但在请求之间,它不会发送正确的cookies。我使用
FileCookieJar
存储cookies。如何在多个guzzle对象之间保存cookie

// first request with login works fine
$cookiePlugin = new CookiePlugin(new FileCookieJar('/tmp/cookie-file'));
$client->addSubscriber($cookiePlugin);

$client->post('/login');

$client->get('/test/123.php?a=b');


// second request where I expect it working, but it's not...
$cookiePlugin = new CookiePlugin(new FileCookieJar('/tmp/cookie-file'));
$client->addSubscriber($cookiePlugin);

$client->get('/another-test/456');

在第二个请求中创建
CookiePlugin
的新实例,在第二个(以及后续)请求中也必须使用第一个实例

$cookiePlugin = new CookiePlugin(new FileCookieJar('/tmp/cookie-file'));

//First Request
$client = new Guzzle\Http\Client();
$client->addSubscriber($cookiePlugin);
$client->post('/login');
$client->get('/test/first');

//Second Request, same client
// No need for $cookiePlugin = new CookiePlugin(...
$client->get('/test/second');

//Third Request, new client, same cookies
$client2 = new Guzzle\Http\Client();
$client2->addSubscriber($cookiePlugin); //uses same instance
$client2->get('/test/third');

如果所有请求都是在同一个用户请求中完成的,则当前的答案将起作用。但是,如果用户先登录,然后浏览站点,然后再查询“域”,那么它就不起作用了

以下是我的解决方案(使用ArrayCookieJar()):

登录

其他请求

$cookiePlugin = new CookiePlugin(new FileCookieJar('/tmp/cookie-file'));

//First Request
$client = new Guzzle\Http\Client();
$client->addSubscriber($cookiePlugin);
$client->post('/login');
$client->get('/test/first');

//Second Request, same client
// No need for $cookiePlugin = new CookiePlugin(...
$client->get('/test/second');

//Third Request, new client, same cookies
$client2 = new Guzzle\Http\Client();
$client2->addSubscriber($cookiePlugin); //uses same instance
$client2->get('/test/third');
$cookiePlugin = new CookiePlugin(new ArrayCookieJar());

//First Request
$client = new Client($domain);
$client->addSubscriber($cookiePlugin);
$request = $client->post('/login');
$response = $request->send();

// Retrieve the cookie to save it somehow
$cookiesArray = $cookiePlugin->getCookieJar()->all($domain);
$cookie = $cookiesArray[0]->toArray();

// Save in session or cache of your app.
// In example laravel:
Cache::put('cookie', $cookie, 30);
// Create a new client object
$client = new Client($domain);
// Get the previously stored cookie
// Here example for laravel
$cookie = Cache::get('cookie');
// Create the new CookiePlugin object
$cookie = new Cookie($cookie);
$cookieJar = new ArrayCookieJar();
$cookieJar->add($cookie);
$cookiePlugin = new CookiePlugin($cookieJar);
$client->addSubscriber($cookiePlugin);

// Then you can do other query with these cookie
$request = $client->get('/getData');
$response = $request->send();