I have a problem with the Simple PHP DOM Parser. I basically have to scrape a catalogue site for the images and their titles.
The site is have to scrape is http://pinesite.com.
I have come up with the following code to do it (this will be called via AJAX):
<?php
include ('simple_html_dom.php');
$function = $_GET['function'];
switch($function) {
case 'subcat':
$maincat = $_GET['cat'];
$url = "http://www.pinesite.com/meubelen/index.php?".$maincat."&lang=de";
$html = file_get_html($url);
$data = $html->find('.box_166_content .act_path li a');
$output ="";
foreach ($data as $subcat) {
$title = $subcat->plaintext;
$href = $subcat->href;
$link['title'] = $title;
$link['href'] =substr($href,10);
$output[] = $link;
}
echo json_encode($output);
$html->clear();
unset($html);
unset($url);
break;
case 'images':
$subcat = $_GET['subcat'];
$url = "http://www.pinesite.com/meubelen/index.php?".$subcat;
$html = file_get_html($url);
$iframe = $html->find('#the_iframe',0);
$url2 = $iframe->src;
$html->clear();
unset($html);
$html2 = file_get_html("http://www.pinesite.com/meubelen/".$url2);
$titles = $html2->find('p');
$images = $html2->find('img');
$output='';
$i=0;
foreach ($images as $image) {
$item['title'] = $titles[$i]->plaintext;
$item['thumb'] = $image->src;
$item['image'] = str_replace('thumb_','',$image->src);
$output[] = $item;
$i++;
}
echo json_encode($output);
break;
}
?>
So that's the "functions" file, the part that doesn't work is the last case.
I don't know what wrong here, so I tested it (the last case) in a separate file (I put the URL that it gets from the iFrame (that part does work):
<?php
include_once "simple_html_dom.php";
$fullurl = "http://www.pinesite.com/meubelen/prog/browse.php?taal=nl&groep=18&subgroep=26";
$html = file_get_html($fullurl);
$titles = $html->find('p');
$images = $html->find('img');
$output='';
$i=0;
foreach ($images as $image) {
$item['title'] = $titles[$i]->plaintext;
$item['thumb'] = $image->src;
$item['image'] = str_replace('thumb_','',$image->src);
$output[] =$item;
$i++;
}
echo json_encode($output);
?>
Like I said the first part should return the same as the second (if you add ?function=images&subcat=dichte-kast) but it doesn't. I'm guessing it is because I use the parser multiple times.
Does anybody have a suggestion for me?