1

I want to use the contents of a website and include them in mine with "Simple Html DOM", unfortunately the code which normally works for another websites, fails in this specific case:

<?php

// Note you must download the php files from the link above 
// and link to them on this line below. 
include_once('simple_html_dom.php');


$html = file_get_html('http://www.comelmar.it/index.aspx?idmnu=23&midx=3&mbidx=2&idmnub=8&Lang=EN');
$elem = $html->find('table[class=Descrizione]', 0);
echo $elem;

?> 

Warning: file_get_contents(http://www.comelmar.it/index.aspx?idmnu=23&midx=3&mbidx=2&idmnub=8&Lang=EN) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in public_html/elin-custom-code/simple_html_dom.php on line 75

Fatal error: Call to a member function find() on a non-object in public_html/elin-custom-code/sklad-1.php on line 9

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Bocho Todorov
  • 429
  • 1
  • 9
  • 22
  • It looks like the page requires a user agent or something. It is throwing a 500 which means there is an error. Your `Fatal error` is because `$html` is false. – chris85 Feb 28 '16 at 16:42

1 Answers1

2

As a link for the reference:-

file_get_contents - failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

Most hosting provides now block the furl_open parameter which allows you to use file_get_contents() to load data from an external url.

You can use CURL or a PHP client library like Guzzle.

To be make sure i just use CURL to check what is the exact problem with the help of below code:-

<?php
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://www.comelmar.it/index.aspx?idmnu=23&midx=3&mbidx=2&idmnub=8&Lang=EN');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name');
$query = curl_exec($curl_handle);
curl_close($curl_handle);
echo "<pre/>";print_r($query);
?>

Output:- http://prntscr.com/a91nl5

So it seems that for security reason these method calls(CURL and file_get_contents) are blocked on that site.

You can try this link answers also, may be you will get some useful output:-

PHP file_get_contents() returns "failed to open stream: HTTP request failed!"

Good Luck.

Community
  • 1
  • 1
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98