/** * Extracts cookies from an HTTP Response object, looking for Set-Cookie: * and Set-Cookie2: headers and persists them to the cookie storage. * * @param Response $response */ public function extractCookies(Response $response) { if (!($cookie = $response->getSetCookie())) { return array(); } $cookieData = array(); $parser = ParserRegistry::get('cookie'); foreach ($cookie as $c) { $request = $response->getRequest(); if ($request) { $cdata = $parser->parseCookie($c, $request->getHost(), $request->getPath()); } else { $cdata = $parser->parseCookie($c); } //@codeCoverageIgnoreStart if (!$cdata) { continue; } //@codeCoverageIgnoreEnd $cookies = array(); // Break up cookie v2 into multiple cookies if (count($cdata['cookies']) == 1) { $cdata['cookie'] = array(key($cdata['cookies']), current($cdata['cookies'])); unset($cdata['cookies']); $cookies = array($cdata); } else { foreach ($cdata['cookies'] as $key => $cookie) { $row = $cdata; unset($row['cookies']); $row['cookie'] = array($key, $cookie); $cookies[] = $row; } } if (count($cookies)) { foreach ($cookies as &$c) { $this->jar->save($c); $cookieData[] = $c; } } } return $cookieData; }
/** * Extracts cookies from an HTTP Response object, looking for Set-Cookie: * and Set-Cookie2: headers and persists them to the cookie storage. * * @param Response $response */ public function extractCookies(Response $response) { if (!($cookie = $response->getSetCookie())) { return array(); } $cookieData = array(); foreach ($cookie as $c) { $cdata = self::parseCookie($c, $response->getRequest()); //@codeCoverageIgnoreStart if (!$cdata) { continue; } //@codeCoverageIgnoreEnd $cookies = array(); // Break up cookie v2 into multiple cookies if (count($cdata['cookies']) == 1) { $cdata['cookie'] = explode('=', $cdata['cookies'][0], 2); unset($cdata['cookies']); $cookies = array($cdata); } else { foreach ($cdata['cookies'] as $cookie) { $row = $cdata; unset($row['cookies']); $row['cookie'] = explode('=', $cookie, 2); $cookies[] = $row; } } if (count($cookies)) { foreach ($cookies as &$c) { $this->jar->save($c); $cookieData[] = $c; } } } return $cookieData; }
/** * Add values to the cookiejar */ public static function addCookies(CookieJarInterface $jar) { $jar->save(array('cookie' => array('foo', 'bar'), 'domain' => 'example.com', 'path' => '/', 'max_age' => '86400', 'port' => array(80, 8080), 'version' => '1', 'secure' => true))->save(array('cookie' => array('baz', 'foobar'), 'domain' => 'example.com', 'path' => '/', 'max_age' => '86400', 'port' => array(80, 8080), 'version' => '1', 'secure' => true))->save(array('cookie' => array('test', '123'), 'domain' => 'www.foobar.com', 'path' => '/path/', 'discard' => true))->save(array('domain' => '.y.example.com', 'path' => '/acme/', 'cookie' => array('muppet', 'cookie_monster'), 'comment' => 'Comment goes here...', 'expires' => Guzzle::getHttpDate('+1 day')))->save(array('domain' => '.example.com', 'path' => '/test/acme/', 'cookie' => array('googoo', 'gaga'), 'max_age' => 1500, 'version' => 2)); }