将 cookie 从浏览器传递到 Guzzle 6 客户端
Passing cookies from browser to Guzzle 6 client
我有一个 PHP 网络应用程序向另一个 PHP API 发出请求。我使用 Guzzle 发出 http 请求,将 $_COOKIES
数组传递给 $options['cookies']
。我这样做是因为 API 使用与前端应用程序相同的 Laravel 会话。我最近升级到 Guzzle 6,我不能再将 $_COOKIES
传递给 $options['cookies']
(我收到关于需要分配 CookieJar
的错误)。我的问题是,如何将浏览器中存在的任何 cookie 移交给我的 Guzzle 6 客户端实例,以便它们包含在对我的 API?
的请求中
试试这样的东西:
/**
* First parameter is for cookie "strictness"
*/
$cookieJar = new \GuzzleHttp\Cookie\CookieJar(true);
/**
* Read in our cookies. In this case, they are coming from a
* PSR7 compliant ServerRequestInterface such as Slim3
*/
$cookies = $request->getCookieParams();
/**
* Now loop through the cookies adding them to the jar
*/
foreach ($cookies as $cookie) {
$newCookie =\GuzzleHttp\Cookie\SetCookie::fromString($cookie);
/**
* You can also do things such as $newCookie->setSecure(false);
*/
$cookieJar->setCookie($newCookie);
}
/**
* Create a PSR7 guzzle request
*/
$guzzleRequest = new \GuzzleHttp\Psr7\Request(
$request->getMethod(), $url, $headers, $body
);
/**
* Now actually prepare Guzzle - here's where we hand over the
* delicious cookies!
*/
$client = new \GuzzleHttp\Client(['cookies'=>$cookieJar]);
/**
* Now get the response
*/
$guzzleResponse = $client->send($guzzleRequest, ['timeout' => 5]);
下面是如何让它们再次出现:
$newCookies = $guzzleResponse->getHeader('set-cookie');
我想你现在可以用 CookieJar::fromArray
来简化这个:
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Client;
// grab the cookies from the existing user's session and create a CookieJar instance
$cookies = CookieJar::fromArray([
'key' => $_COOKIE['value']
], 'your-domain.com');
// create your new Guzzle client that includes said cookies
$client = new Client(['cookies' => $jar]);
我有一个 PHP 网络应用程序向另一个 PHP API 发出请求。我使用 Guzzle 发出 http 请求,将 $_COOKIES
数组传递给 $options['cookies']
。我这样做是因为 API 使用与前端应用程序相同的 Laravel 会话。我最近升级到 Guzzle 6,我不能再将 $_COOKIES
传递给 $options['cookies']
(我收到关于需要分配 CookieJar
的错误)。我的问题是,如何将浏览器中存在的任何 cookie 移交给我的 Guzzle 6 客户端实例,以便它们包含在对我的 API?
试试这样的东西:
/**
* First parameter is for cookie "strictness"
*/
$cookieJar = new \GuzzleHttp\Cookie\CookieJar(true);
/**
* Read in our cookies. In this case, they are coming from a
* PSR7 compliant ServerRequestInterface such as Slim3
*/
$cookies = $request->getCookieParams();
/**
* Now loop through the cookies adding them to the jar
*/
foreach ($cookies as $cookie) {
$newCookie =\GuzzleHttp\Cookie\SetCookie::fromString($cookie);
/**
* You can also do things such as $newCookie->setSecure(false);
*/
$cookieJar->setCookie($newCookie);
}
/**
* Create a PSR7 guzzle request
*/
$guzzleRequest = new \GuzzleHttp\Psr7\Request(
$request->getMethod(), $url, $headers, $body
);
/**
* Now actually prepare Guzzle - here's where we hand over the
* delicious cookies!
*/
$client = new \GuzzleHttp\Client(['cookies'=>$cookieJar]);
/**
* Now get the response
*/
$guzzleResponse = $client->send($guzzleRequest, ['timeout' => 5]);
下面是如何让它们再次出现:
$newCookies = $guzzleResponse->getHeader('set-cookie');
我想你现在可以用 CookieJar::fromArray
来简化这个:
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Client;
// grab the cookies from the existing user's session and create a CookieJar instance
$cookies = CookieJar::fromArray([
'key' => $_COOKIE['value']
], 'your-domain.com');
// create your new Guzzle client that includes said cookies
$client = new Client(['cookies' => $jar]);