Search code examples
phphtmldomdomparser

How to pass store selection in Simple HTML DOM parser


I'm trying to fetch a product name and price on this website Toplivo.bg

I am using the Simple HTML DOM parser to get it. Here is my code

include_once('simple_html_dom.php');

$link="https://toplivo.bg/en/products/Construction-materials/Dry-construction-mixtures/Screeds-and-flooring";

$html = file_get_html($link);
//Price
foreach ($html->find('div[class="content"]') as $text){
  echo $text -> plaintext.'<br>';
}
?> 

The problem is that first, I need to select the warehouse on the website to get the price for "Baumit Cement screed Baumit Solido E160, 25 kg".

Can I select it by default through PHP code? For example, I want to select the "Plovdiv region -> Plovdiv Store"

Thanks for helping!


Solution

  • This can be achieved using cURL. Complete code below:

    <?php
    include_once('simple_html_dom.php');
    
    $link = "https://toplivo.bg/en/products/Construction-materials/Dry-construction-mixtures/Screeds-and-flooring";
    
    // let's use curl to create a get request first to select a store while keeping the session using a cookie file
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://toplivo.bg/izborNaSklad/39');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-45fg.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie-45fg.txt');
    $output = curl_exec($ch);
    
    curl_setopt($ch, CURLOPT_URL, $link); // now let's fetch the raw content of the store products page
    $output = curl_exec($ch);
    
    $html = str_get_html($output); // since we have the raw input, we can use the str_get_html method instead of file_get_html
    
    //Price
    foreach ($html->find('div[class="content"]') as $text){
      echo $text->plaintext . '<br>';
    }
    
    ?>