Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple PHP DOM Parser doesn't work in switch case (PHP)
    primarykey
    data
    text
    <p>I have a problem with the Simple PHP DOM Parser. I basically have to scrape a catalogue site for the images and their titles. </p> <p>The site is have to scrape is <a href="http://pinesite.com" rel="nofollow">http://pinesite.com</a>.</p> <p>I have come up with the following code to do it (this will be called via AJAX):</p> <pre><code>&lt;?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."&amp;lang=de"; $html = file_get_html($url); $data = $html-&gt;find('.box_166_content .act_path li a'); $output =""; foreach ($data as $subcat) { $title = $subcat-&gt;plaintext; $href = $subcat-&gt;href; $link['title'] = $title; $link['href'] =substr($href,10); $output[] = $link; } echo json_encode($output); $html-&gt;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-&gt;find('#the_iframe',0); $url2 = $iframe-&gt;src; $html-&gt;clear(); unset($html); $html2 = file_get_html("http://www.pinesite.com/meubelen/".$url2); $titles = $html2-&gt;find('p'); $images = $html2-&gt;find('img'); $output=''; $i=0; foreach ($images as $image) { $item['title'] = $titles[$i]-&gt;plaintext; $item['thumb'] = $image-&gt;src; $item['image'] = str_replace('thumb_','',$image-&gt;src); $output[] = $item; $i++; } echo json_encode($output); break; } ?&gt; </code></pre> <p>So that's the "functions" file, the part that doesn't work is the last case.</p> <p>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):</p> <pre><code>&lt;?php include_once "simple_html_dom.php"; $fullurl = "http://www.pinesite.com/meubelen/prog/browse.php?taal=nl&amp;groep=18&amp;subgroep=26"; $html = file_get_html($fullurl); $titles = $html-&gt;find('p'); $images = $html-&gt;find('img'); $output=''; $i=0; foreach ($images as $image) { $item['title'] = $titles[$i]-&gt;plaintext; $item['thumb'] = $image-&gt;src; $item['image'] = str_replace('thumb_','',$image-&gt;src); $output[] =$item; $i++; } echo json_encode($output); ?&gt; </code></pre> <p>Like I said the first part should return the same as the second (if you add ?function=images&amp;subcat=dichte-kast) but it doesn't. I'm guessing it is because I use the parser multiple times.</p> <p>Does anybody have a suggestion for me?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload