Jump to content

Find all images in a website


Raex

Recommended Posts

Hi, I have a php code that is used to find all the images in a website using simple_html_dom. When I tried to run it, this error occured:

 

Fatal error: Call to a member function find() on a non-object in C:\xampp\htdocs\myPHP\index.php on line 20

 

This is my code:

<!DOCTYPE html>
<html>
<body>

<?php
include_once("simple_html_dom.php");
//use curl to get html content
function getHTML($url,$timeout)
{
       $ch = curl_init($url); // initialize curl with given url
       curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set  useragent
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any
       curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute
       curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error
       return @curl_exec($ch);
}
$html=getHTML("http://www.website.com",10);
// Find all images on webpage
foreach($html->find("img") as $element)
echo $element->src . '<br>';
 
?>

</body>
</html>

I have the simple_html_dom.php file in my directory. How can I correct this? thanks

Edited by Raex
Link to comment
Share on other sites

You aren't actually using simple_html_dom. You grab the page using curl and put it in $html, but $html is not a simple_html_dom object.  It's just a string.

 

Either use the simple_html_dom function, file_get_html(url) instead of using your custom curl function, or, pass $html that you retrieve using getHTML() to the simple_html_dom function str_get_html($html). Then you can use the find method.

$html = file_get_html('http://www.website.com');
foreach($html->find("img") as $element) ...

or

$html=getHTML("http://www.website.com",10);
$html = str_get_html($html);
foreach($html->find("img") as $element) ...

http://simplehtmldom.sourceforge.net/manual.htm

Edited by CroNiX
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.