I recently had the need to extract the value of a Set-Cookie response header in PHP. Google lead me
pecl_http which isn’t available with PHP out of box, and is a pain to install.
Other Google results suggest defining your own function.
Here’s a quick overview of how to use it…
Usage in pretty simple. The easiest way to use Guzzle is via composer. Once you’ve autoloaded the library, you pass the
Set-Cookie header as an argument to
fromString method as follows…
use GuzzleHttp\Cookie\SetCookie as CookieParser; $cookieParser = new CookieParser; $cookie = $cookierParser->fromString('Set-Cookie: key=value');
Now you can conveniently interact with the
echo $cookie->getName(); // key echo $cookie->getValue(); // value
You can also access many other aspects of the cookie
echo $cookie->getExpires(); echo $cookie->getHttpOnly(); echo $cookie->getSecure();
Set-Cookie class for a full list of available methods…
I hope this post came in useful for some people. If you have any questions or comments, feel free to drop a note below, or, as always, you can reach me on Twitter as well.
Hi, I'm Max!
If you'd like to get in touch with me the best way is on Twitter.