How to Use File_Get_Contents or File_Get_Html

How to use file_get_contents or file_get_html?

You are not able to specify in file_get_contents() just to retrieve the tables.

You would have to get the return value of file_get_contents() using:

$result = file_get_contents("urlHere");

And then analyse the $result variable and extract what information you need to output.

file_get_contents(): stream does not support seeking / When was PHP behavior about this changed?

See file_get_contents(): stream does not support seeking PHP

You are working with a remote file. Seeking is only supported for local files.

You probably need to copy the file to your local file system before using file_get_html. It should work fine on localhost.

Get Links with simple html dom

wrap your file_get_contents in str_get_html function

// method 1
$html = new simple_html_dom();
$html->load( file_get_contents ($url, false, $context) );
// or method 2
$html = str_get_html( file_get_contents ($url, false, $context) );

you're creating a new dom and assigning it to the variable $html, than reading the url returning the string and setting it to $html, thus overwriting your simple_html_dom instance, so when your invoking the find method you have a string instead of an object.

Good error handling with file_get_contents

Here's an idea:

function fget_contents() {
$args = func_get_args();
// the @ can be removed if you lower error_reporting level
$contents = @call_user_func_array('file_get_contents', $args);

if ($contents === false) {
throw new Exception('Failed to open ' . $file);
} else {
return $contents;
}
}

Basically a wrapper to file_get_contents. It will throw an exception on failure.
To avoid having to override file_get_contents itself, you can

// change this
$dom->load(call_user_func_array('file_get_contents', $args), true);
// to
$dom->load(call_user_func_array('fget_contents', $args), true);

Now you can:

try {
$html3 = file_get_html(trim("$link"));
} catch (Exception $e) {
// handle error here
}

Error suppression (either by using @ or by lowering the error_reporting level is a valid solution. This can throw exceptions and you can use that to handle your errors. There are many reasons why file_get_contents might generate warnings, and PHP's manual itself recommends lowering error_reporting: See manual

file_get_html error, not working

Try this:

$html = str_get_html(file_get_contents($url));


Related Topics



Leave a reply



Submit