Search code examples
phpsimple-html-dom

simple html dom parser $html is empty


When trying to parse html using simple html parser, I get no response. Here is the code:

$html = new simple_html_dom();  
$html->file_get_html('http://thepiratebay.se/search/1080p/0/7/207');

$html returns nothing. However, when I'm doing the same thing using this url, http://thepiratebay.se/browse/207/0/7, I get a normal response.

I don't really understand why since the url works perfectly.

A var_dump on $html returns a bool (false).

I have php 5.3.1 and allow_url_fopen is on in php.ini


Solution

  • Use cURL and set a user-agent. Apparently thepriatebay.se does not to respond to requests without a user-agent.

    This grabs the user-agent of your browser and sends it to the target.

    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    

    To request a web page through cURL, use the following:

    // Start a cURL resource
    $ch = curl_init();
    // Set options for the cURL
    curl_setopt($ch, CURLOPT_URL, 'http://thepiratebay.se/search/1080p/0/7/207'); // target
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // provide a user-agent
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow any redirects
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the result
    // Execute the cURL fetch
    $result = curl_exec($ch);
    // Close the resource
    curl_close($ch);
    // Output the results
    echo $result;