GuzzleHttp:how 我可以从 POST 响应中保存 cookie 并在下一个 POST 中使用它吗?
GuzzleHttp:how can I save cookies from a POST response and use it in the next POST?
我正在使用 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
) 用于客户端的请求/响应。所以你需要使用:
$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'),
]
]);
// 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/login', [
'timeout' => 30,
'form_params' => [
'email' => $request->get('email'),
'password' => $request->get('password'),
]
]);
// and using the same client
$response = $client->request('GET', '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');
//post form params
$response = $client->post(route('login'), [
'form_params' => [
'login' => 'login',
'password' => 'password',
],
'headers' => [
'Accept' => 'text/html,application/xhtml+xm…ml;q=0.9,image/webp,*/*;q=0.8',
'Content-Type' => 'application/x-www-form-urlencoded',
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0'
]
]);
//get cookie from header in response
$cookie = $response->getHeaderLine('Set-Cookie');
//get page with cookie
$client->get('/products', [
'headers' => [
'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
'accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'accept-encoding' => 'gzip, deflate, br',
'accept-language' => 'ru',
'cookie' => $cookie, //this cookie data
],
'debug' => false
]);
我正在使用 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
) 用于客户端的请求/响应。所以你需要使用:
$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'),
]
]);
// 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/login', [
'timeout' => 30,
'form_params' => [
'email' => $request->get('email'),
'password' => $request->get('password'),
]
]);
// and using the same client
$response = $client->request('GET', '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');
//post form params
$response = $client->post(route('login'), [
'form_params' => [
'login' => 'login',
'password' => 'password',
],
'headers' => [
'Accept' => 'text/html,application/xhtml+xm…ml;q=0.9,image/webp,*/*;q=0.8',
'Content-Type' => 'application/x-www-form-urlencoded',
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0'
]
]);
//get cookie from header in response
$cookie = $response->getHeaderLine('Set-Cookie');
//get page with cookie
$client->get('/products', [
'headers' => [
'user-agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36',
'accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'accept-encoding' => 'gzip, deflate, br',
'accept-language' => 'ru',
'cookie' => $cookie, //this cookie data
],
'debug' => false
]);