iframeUrl = $iframeUrl; if ($cache == null) { // if cache could not be initialized $this->_grabReview(); } else { $reviewCacheId = 'CustomerReviews_'. $asin; $reviewData = $cache->load($reviewCacheId); if (empty($reviewData)) { // data is not cached yet $this->_grabReview(); $reviewData = array( 'imgTag' => $this->imgTag, 'imgSrc' => $this->imgSrc, 'totalReviews' => $this->totalReviews, 'averageRating' => $this->averageRating ); // put data in cache $cache->save($reviewData, $reviewCacheId); } else { // load data from cache $this->imgTag = $reviewData['imgTag']; $this->imgSrc = $reviewData['imgSrc']; $this->totalReviews = $reviewData['totalReviews']; $this->averageRating = $reviewData['averageRating']; } } } /** * open the iframe and grab the relevant data */ protected function _grabReview () { $iframeContents = $this->_getIframeContents(); if ($iframeContents != '') { $this->_getReviewsData($iframeContents); } } /** * get img tag and src from iframe html * @param string $contents */ protected function _getReviewsData ($contents) { $patternTag = '~]+>~is'; $patternSrc = '/(src)="([^"]*)"/i'; $patternAlt = '/(alt)="([^"]*)"/i'; $patternCnt = '~]*>(.*?)~is'; if (preg_match($patternTag, $contents, $matchTag) == 1) { $this->imgTag = $matchTag[0]; if (preg_match($patternSrc, $this->imgTag, $matchSrc) == 1) { $this->imgSrc = $matchSrc[2]; } if (preg_match($patternAlt, $this->imgTag, $matchAlt) == 1) { $alt = explode(' ', $matchAlt[2]); $this->averageRating = $alt[0]; } } if (preg_match_all($patternCnt, $contents, $matchCnt)) { $count = explode(' ', $matchCnt[1][1]); $count = str_replace(',', '', $count); $count = str_replace('.', '', $count); $this->totalReviews = intval($count[0]); } } /** * read the iframe contents and grab the relevant code * @return string $contents */ protected function _getIframeContents () { $contents = ''; $client = new AsaZend_Http_Client($this->iframeUrl); $result = $client->request('GET'); foreach(preg_split("/$\R?^/m", $result->getBody()) as $line){ if (trim($line) == '
') { $saveBuffer = true; } if ($saveBuffer == true && trim($line) == '
') { $saveBuffer = false; } if ($saveBuffer == true) { $contents .= $line; } } return $contents; } } ?>