public function testIfCookiesAreSticky() { $initialCookies = array( new SetCookie('foo', 'far', null, '/', 'www.domain.com' ), new SetCookie('bar', 'biz', null, '/', 'www.domain.com') ); $requestString = "GET http://www.domain.com/index.php HTTP/1.1\r\nHost: domain.com\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:16.0) Gecko/20100101 Firefox/16.0\r\nAccept: */*\r\nAccept-Language: en-US,en;q=0.5\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive\r\n"; $request = Request::fromString($requestString); $client = new Client('http://www.domain.com/'); $client->setRequest($request); $client->addCookie($initialCookies); $cookies = new Cookies($client->getRequest()->getHeaders()); $rawHeaders = "HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nContent-Encoding: gzip\r\nContent-Type: application/javascript\r\nDate: Sun, 18 Nov 2012 16:16:08 GMT\r\nServer: nginx/1.1.19\r\nSet-Cookie: baz=bah; domain=www.domain.com; path=/\r\nSet-Cookie: joe=test; domain=www.domain.com; path=/\r\nVary: Accept-Encoding\r\nX-Powered-By: PHP/5.3.10-1ubuntu3.4\r\nConnection: keep-alive\r\n"; $response = Response::fromString($rawHeaders); $client->setResponse($response); $cookies->addCookiesFromResponse($client->getResponse(), $client->getUri()); $client->addCookie( $cookies->getMatchingCookies($client->getUri()) ); $this->assertEquals(4, count($client->getCookies())); }
private function parseReview($fieldName, Query $domDocument) { $query = 'div.prod_rvw time'; // Review selector foreach ($domDocument->execute($query) as $result) { // Parse review date $this->content[$fieldName][] = date('d-m-y', strtotime($result->textContent)); } $result = $domDocument->execute('span.js-prod-rvw-view-more'); // Is more reviews button set if ($result->count() > 0) { $result = $result->offsetGet(0); $pageType = $result->getAttribute('data-page-type'); $total = $result->getAttribute('data-total'); if ($total > count($this->content[$fieldName])) { //Can get more reviews $cookie = Cookies::fromResponse($this->response, $this->url); $this->loader->resetParameters(); $href = '/submit_review.php'; if (!parse_url($href, PHP_URL_HOST)) { $urlHost = parse_url($this->url, PHP_URL_HOST); $urlScheme = parse_url($this->url, PHP_URL_SCHEME) . '://'; $href = $urlScheme . $urlHost . $href; } $this->loader->setParameterPost(array('action' => $this->getReviewAction($pageType), 'type' => $result->getAttribute('data-type'), 'id' => $result->getAttribute('data-id'), 'offset' => count($this->content[$fieldName]))); $cookies = new Cookies(); foreach ($cookie->getAllCookies() as $cookieRaw) { //Zend Cookies Error hack /** @var SetCookie $cookieRaw */ if ($cookieRaw->getDomain() != null) { $cookies->addCookie($cookieRaw); } } $this->loader->addCookie($cookies->getMatchingCookies($this->loader->getUri())); $this->loader->setUri($href); $request = $this->loader->getRequest(); $request->getHeaders()->addHeaderLine('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); $request->getHeaders()->addHeaderLine('X-Requested-With', ' XMLHttpRequest'); $request->setContent($request->getPost()->toString()); $this->response = $this->loader->setMethod('POST')->send(); $body = $this->response->getBody(); $domDocumentNew = new Query($body); if ($domDocumentNew->execute($query)->count() == 0) { foreach ($domDocumentNew->execute($query) as $result) { // Parse review date $this->content[$fieldName][] = date('d-m-y', strtotime($result->textContent)); } return false; } else { $this->parseReview($fieldName, $domDocument); } } } return true; }
public function testFromResponseInCookie() { $response = new Response(); $headers = new Headers(); $header = new SetCookie("foo", "bar"); $header->setDomain("www.zend.com"); $header->setPath("/"); $headers->addHeader($header); $response->setHeaders($headers); $response = Cookies::fromResponse($response, "http://www.zend.com"); $this->assertSame($header, $response->getCookie('http://www.zend.com', 'foo')); }
/** * * @param string $url * @return insatnce of Zend\Http\Client */ protected function getNewResponse($url = NULL) { $client = new HttpClient(); /** * Incase of 2nd call , use product url */ if ($url != NULL) { $client->setUri($url); } /** * check if it is 2nd call then use the header cookies so that we can use the same session to avoid */ /** * sending more traffic to the site for the performance point of view */ if (isset($_SESSION['cookiejar']) && $_SESSION['cookiejar'] instanceof \Zend\Http\Cookies) { $cookieJar = $_SESSION['cookiejar']; } else { // set Curl Adapter $client->setAdapter('Zend\\Http\\Client\\Adapter\\Curl'); // $response = $this->getResponse (); // keep the conntection alive for multiple calls $client->setOptions(array('keepalive' => true)); // set content-type // $httpResponse->getHeaders ()->addHeaderLine ( 'content-type', 'text/html; charset=utf-8' ); $client->setUri(SELF::GROCCERYURL); $result = $client->send(); $cookieJar = \Zend\Http\Cookies::fromResponse($result, SELF::GROCCERYURL); $_SESSION['cookiejar'] = $cookieJar; $client->resetParameters(); return $result; } $connectionCookies = $cookieJar->getMatchingCookies($client->getUri()); // set the cookies for the 2nd call $client->setCookies($this->getConntetionCookies($connectionCookies)); $response = $client->send(); return $response; }