user11390751
user11390751

Reputation:

how to fix in use of file_get_html

I use this code by file_get_html for echo a tah from html file in php.

but I get error after run my code in local.

error :

Fatal error: Uncaught Error: Call to undefined function file_get_html() in C:\xampp\htdocs\2\index.php:2 Stack trace: #0 {main} thrown in C:\xampp\htdocs\2\index.php on line 2

php code :

<?php
$html = file_get_html('http://example.com/');
        
// Find all images 
foreach($html->find('img') as $element);
    echo $element->src . '<br>';
    
// Find all links 
foreach($html->find('a') as $element);
    echo $element->href . '<br>';
?>

this is errors after change :

Warning: file_get_contents(): stream does not support seeking in C:\xampp\htdocs\2\simple_html_dom.php on line 75

Warning: file_get_contents(): Failed to seek to position -1 in the stream in C:\xampp\htdocs\2\simple_html_dom.php on line 75

Fatal error: Uncaught Error: Call to a member function find() on boolean in C:\xampp\htdocs\2\index.php:7 Stack trace: #0 {main} thrown in C:\xampp\htdocs\2\index.php on line 7

Upvotes: 0

Views: 3282

Answers (1)

bloo
bloo

Reputation: 1560

I think there's a few steps you're missing. should look something like this:

// Include the library
include('simple_html_dom.php');

// Retrieve the DOM from a given URL
$html = file_get_html('http://example.com/');

// Find all "A" tags and print their HREFs
foreach($html->find('a') as $e) 
    echo $e->href . '<br>';

Here's the link for the library, hope it works... https://tenet.dl.sourceforge.net/project/simplehtmldom/simplehtmldom/1.8.1/simplehtmldom_1_8_1.zip

Upvotes: 1

Related Questions