simple_html_dom(php parser) not working on a particular website when I connect it to a server

203 Views Asked by At

I build a PHP scrapper that scraps a website with the URL (https://www.forebet.com/en/values); My code for finding the title is:

require 'simple_html_dom.php';
$html=file_get_html("https://www.forebet.com/en/values");
echo $html->find('title', 0)->plaintext;

It DOES work when I am running it on my computer, but it doesn't run on a server. It shows the error message"This page isn’t workingwager1x2.com is currently unable to handle this request. HTTP ERROR 500".

By enabling the 'PHP error code display function' which is ini_set('display_errors', 1); it shows a fatal error before a warning:

  1. Warning: "file_get_contents(https://www.forebet.com/en/values): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in /home/winbvgmt/wager1x2.com/forbet_scrapper/simple_html_dom.php on line 84" Line 79-84 of simple_html_dom is:

    $contents = file_get_contents( $url, $use_include_path, $context, $offset, $maxLen);

  2. Fatal Error: "Uncaught Error: Call to a member function find() on bool in /home/winbvgmt/wager1x2.com/forbet_scrapper/php.php:16 Stack trace: #0 {main} thrown in /home/winbvgmt/wager1x2.com/forbet_scrapper/php.php on line 16". Line 16 is:echo $html->find('title', 0)->plaintext;

I tried checking it on google.com and it works perfectly for google.

1

There are 1 best solutions below

2
Tural Rzaxanov On

Maybe it for security reason. There are two reason types. One is them is remote server security . Another is local server(hosting) security is blocking when you send request to remote server. Instead of this you may use curl library. For example:



  // create curl resource
    $ch = curl_init();

    // set url
    curl_setopt($ch, CURLOPT_URL, "http://example.com");

    //return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

    // $output contains the output string
    $output = curl_exec($ch);

    // close curl resource to free up system resources
    curl_close($ch);