Skip to content

Instantly share code, notes, and snippets.

@tistre
Created April 7, 2014 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tistre/10016739 to your computer and use it in GitHub Desktop.
Save tistre/10016739 to your computer and use it in GitHub Desktop.
Parse cookies from raw HTTP response using PHP’s pecl_http
<?php
$msg = <<<EOT
HTTP/1.1 200 OK
Date: Mon, 07 Apr 2014 08:29:07 GMT
Server: Apache/2.2.15 (CentOS)
X-Powered-By: PHP/5.3.20
Set-Cookie: dcx_app_trunk=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/dcx_trunk/
Set-Cookie: dcx_app_trunk=33mnkpiau2s80uhrtb7a105l23; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Length: 2251
Connection: close
Content-Type: text/html; charset=UTF-8
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
</html>
EOT;
// http://devel-m6w6.rhcloud.com/mdref/http/Message/__construct
$response = new http\Client\Response($msg);
// http://devel-m6w6.rhcloud.com/mdref/http/Client/Response/getCookies
foreach ($response->getCookies() as $cookie)
{
// http://devel-m6w6.rhcloud.com/mdref/http/Cookie/getCookies
foreach ($cookie->getCookies() as $name => $value)
{
printf("Got a cookie named '%s' with value '%s'\n\tdomain=%s\n\t path=%s\n",
$name, $value, $cookie->getDomain(), $cookie->getPath());
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment