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; }