/** * This will extract individual product information. * @return array */ public function getResponse() { //set initial data response $data = array(); //get curl response /** @var \Library\Common\Curl $curl */ $curl = $this->getCurl(); $curl->get($this->getUrl()); $response = $curl->getResponse(); //parse response html dom $parser = new HtmlDomParser(); $parser->loadstring($response); //get html filesize $data['size'] = strlen($response); //get product description information $strings = array(); foreach ($parser->find('#information .productText p') as $product) { $strings[] = $product->plaintext; } if (!empty($strings[0])) { $data['description'] = $strings[0]; } //return html size and description in array return $data; }
/** * This will get the basic product information from the product list html snippet response * @param $html * @return array */ public function getProductInfo($html) { //parse html dom $parser = new HtmlDomParser(); $parser->loadstring($html); //init default productInfo array $productInfo = array(); //set product incrementer $i = 0; //iterate over .product classes foreach ($parser->find('.product') as $product) { //iterate over .productInfo a elements foreach ($product->find('.productInfo a') as $a) { //get product title $productInfo['title'] = trim($a->plaintext); //get product href $productInfo['href'] = trim($a->href); //use first anchor tag result only break; } //iterate over .pricePerUnit class foreach ($parser->find('.pricePerUnit') as $pricePerUnit) { //get product unit price. $unitPrice = $pricePerUnit->plaintext; $unitPrice = str_replace('/unit', '', $unitPrice); $unitPrice = str_replace('£', '', $unitPrice); $productInfo['unit_price'] = trim($unitPrice); } $i++; } //return product title, href, and unit price return $productInfo; }