CURL Picture Bot
313
If you would like to download pictures from a link, cURL is right tool for you to operate in PHP.
By having short lines of code, you can easily fetch all pictures without not requiring individually saving one by one. When data is already in your side, it's your decision to how to use it further.
Code Example:
$curl = curl_init("http://www.halukyamaner.com");
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_USERAGENT, $agent); // We are acting like Mozilla Firefox
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // If you define this value as "0", it also gives output of web site for debugging purposes.
$data = curl_exec($curl);
curl_close($curl);
$images = array();
preg_match_all('/(img|src)=("|')[^"'>]+/i', $data, $media);
unset($data);
$data=preg_replace('/(img|src)("|'|="|=')(.*)/i',"$3",$media[0]);
foreach($data as $url)
{
$info = pathinfo($url);
if (isset($info['extension']))
{
if (($info['extension'] == 'jpg') ||
($info['extension'] == 'jpeg') ||
($info['extension'] == 'gif') ||
($info['extension'] == 'png'))
array_push($images, $url);
}
}
// We have downloaded all pictures and transform data through $images Array. Let's see what it's done:
echo "
";
print_r($images);
echo "
";
That's it, easy huh?
If you are looking for consultation, fill the Contact Form below.
Great companies are built on great products.
Haluk YAMANER
Founder @ Future Software UAE
Founder @ Future Linux
Click here for more about me »