File_Get_Contents(): Stream Does Not Support Seeking/When Was PHP Behavior About This Changed

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.

How can I handle the warning of file_get_contents() function in PHP?

Step 1: check the return code: if($content === FALSE) { // handle error here... }

Step 2: suppress the warning by putting an error control operator (i.e. @) in front of the call to file_get_contents():
$content = @file_get_contents($site);

Wordpress simple_html_dom.php admin page

I was able to solve this by looking at file_get_contents(): stream does not support seeking / When was PHP behavior about this changed?

On line 75 of simple_html_dom.php:

$contents = file_get_contents($url, $use_include_path, $context, $offset);
I removed the reference to $offset:

$contents = file_get_contents($url, $use_include_path, $context);
No my page works fine. Not taking liability for anything else it breaks! :)

Handle errors of file_get_contents()

Put an @ in front of file_get_contents and then check if $contents === false;

$contents = @file_get_contents($url, $use_include_path, $context, $offset);
if ($contents === false) {
//Something went wrong.
}

EDIT: The @ suppresses the error from being dumped out.

Uncaught Error: Call to undefined function get_file_html()

You can use str_get_html() with the response from the curl request you have, so the code would look something like...

include "simple_html_dom.php";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://in.tradingview.com/symbols/NSE-TCS/");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);

$html = str_get_html($response);
foreach($html->find('a') as $element) {
echo $element->href . " -> " . $element->plaintext .'<br>';
}


Related Topics



Leave a reply



Submit