views:

88

answers:

4

I need to create a php script.

The idea is very simple:

When I send a link of a blogpost to this php script, then the webpage is crawled and the first image with the title page are saved on my server.

What PHP function I have to use for this crawler ?

+3  A: 

Use PHP Simple HTML DOM Parser

// Create DOM from URL
$html = file_get_html('http://www.example.com/');

// Find all images
$images = array(); 
foreach($html->find('img') as $element) {
       $images[] = $element->src;
} 

Now $images array have images links of given webpage. Now you can store your desired image in database.

NAVEED
+1  A: 

Well, you'll have to use quite a few functions :)

But I'm going to assume that you're asking specifically about finding the image, and say that you should use a DOM parser like Simple HTML DOM Parser, then curl to grab the src of the first img element.

Brock Batsell
+1  A: 

I would user file_get_contents() and a regular expression to extract the first image tags src attribute.

CURL or a HTML Parser seem overkill in this case, but you are welcome to check it out.

Jason McCreary
+1  A: 

HTML Parser: HTMLSQL

Features: you can get external html file, http or ftp link and parse content.

Eugen