Capturing / loading images from multiple pages with php preg_match_all & cURL

So, I'm trying to capture some images from another site, the problem is that each image is on a different page

IE: id / 1, id / 2, id / 3, etc. etc.

so far I have the code below that can capture an image from a single URL given with:

$returned_content = get_data('http://somedomain.com/id/1/'); 

but you need to make the line above to become an array (I think) so that it captures the image from page 1, then go to the next image on page 2, then page 3, etc. automatically

 function get_data($url){ $ch = curl_init(); $timeout = 5; curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); $data = curl_exec($ch); curl_close($ch); return $data; } $returned_content = get_data('http://somedomain.com/id/1/'); if (preg_match_all("~http://somedomain.com/images/(.*?)\.jpg~i", $returned_content, $matches)) { $src = 0; foreach ($matches[1] as $key) { if(++$src > 1) break; $out = $key; } $file = 'http://somedomain.com/images/' . $out . '.jpg'; $dir = 'photos'; $imgurl = get_data($file); file_put_contents($dir . '/' . $out . '.jpg', $imgurl); echo 'done'; } 

As always, all help is appreciated and thanks in advance.

+4
source share
1 answer

It was rather confusing because it sounded like you were only interested in saving one image on a page. But then the code makes it look like you are actually trying to save every image on every page. So this is entirely possible, I completely misunderstood ... But here it goes.

Crossing each page is not so difficult:

 $i = 1; $l = 101; while ($i < $l) { $html = get_data('http://somedomain.com/id/'.$i.'/'); getImages($html); $i += 1; } 

The following assumes that you are trying to save all the images on this particular page:

 function getImages($html) { $matches = array(); $regex = '~http://somedomain.com/images/(.*?)\.jpg~i'; preg_match_all($regex, $html, $matches); foreach ($matches[1] as $img) { saveImg($img); } } function saveImg($name) { $url = 'http://somedomain.com/images/'.$name.'.jpg'; $data = get_data($url); file_put_contents('photos/'.$name.'.jpg', $data); } 
+4
source

All Articles