private function CreateDomDocument() { $dom = new Domdocument(); @$dom->loadHTML($this->htmlStack); $xpath = new DomXpath($dom); // Find all the product blocks that contain beer attribute that we want to search for such as rating, title, price, etc. $elements = $xpath->query('//article[contains(@class, "' . $this->class . '")]'); foreach ($elements as $element) { // get the node value from the .title class (the title of the product) $title = $xpath->query('descendant::div[@class="title"]', $element); $title = $title->item(0)->nodeValue; // get the attribute value from the .link-overlay class (the actual link to the product) $link = $xpath->query('descendant::a[@class="link-overlay"]', $element); $link = $link->item(0)->getAttribute('href'); $link = 'https://www.gall.nl' . $link; // get the image src value to rip the image from the site $image = $xpath->query('descendant::div[@class="image"]/node()/node()', $element); $image = $image->item(1)->getAttribute('src'); // Dive in the link that a single product has $results = $this->curlGetDeep($link); // if a regex option has been supplied run it on the results if ($this->regex) { $title = preg_replace($this->regex, '', $title); } // remove the unwanted 6X 35x6(bulk beers) results with this regex if (!preg_match('/\\dX(\\d+)?/', $title)) { $this->elementsArray[] = ['title' => $title, 'link' => $link, 'image' => $image, 'description' => $results['description'], 'abv' => $results['abv']]; } } $this->json = json_encode(['beers' => $this->elementsArray]); }
public function CreateDomDocument() { $dom = new Domdocument(); @$dom->loadHTML($this->htmlStack); $xpath = new DomXpath($dom); $elements = $xpath->query('//a[@class="' . $this->class . '"]'); foreach ($elements as $element) { $element = $element->getAttribute('href'); // if a regex option has been supplied run it on the results if ($this->regex) { $element = preg_replace($this->regex, '', $element); } // remove the 6X 35x6(bulk beers) results with this regex if (!preg_match('/\\dX(\\d+)?/', $element)) { $this->elementsArray[] = ['title' => $element]; } } echo json_encode(['beers' => $this->elementsArray]); }