Loading a certain area of HTML from an external domain webpage into a div

Hey! Im new here :slight_smile:

I am trying to scrape a hotel booking site to import their reviews div so I can display their reviews on my website, I just seem to be having a few problems.

This is the code I have so far:

[php]<?php
$url = ‘http://www.laterooms.com/en/hotel-reviews/238902_the-westfield-bb-sandown.aspx’;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$htmlContent = curl_exec($curl);
curl_close($curl);

$doc = new DOMDocument();
$doc->load($htmlContent);
foreach ($pElements as $pEl) {
if ($pEl->getAttribute(‘div’) == ‘reviews’) {
$pContent = $pEl->nodeValue;
}
}
?>[/php]

Thanks for any input in advance 8)

here’s the solution… I worked it out… eventually

[php]function file_get_contents_curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}
function DOMinnerHTML($element){
$innerHTML = “”;
$children = $element->childNodes;
foreach ($children as $child)
{
$tmp_dom = new DOMDocument();
$tmp_dom->appendChild($tmp_dom->importNode($child, true));
$innerHTML.=trim($tmp_dom->saveHTML());
}
return $innerHTML;
}
$url = ‘http://www.laterooms.com/en/hotel-reviews/238902_the-westfield-bb-sandown.aspx’;
$html = file_get_contents_curl($url);

//parsing begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$div_elements = $doc->getElementsByTagName(‘div’);

if ($div_elements->length <> 0){
foreach ($div_elements as $div_element) {
if ($div_element->getAttribute(‘class’) == ‘review newReview’){
$reviews[] = DOMinnerHTML($div_element);

}

}
}

print_r($reviews);[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service