Download image using php cURL

Posted by midhun pottmmal On Saturday, 22 December 2012 0 comments

         
     cURL is a great tool to help you connect to remote web sites ,you can also use this for download image from web or binary file like pdf,psd,doc,zip with curl. Google is using this  technique to get all web images from different  web pages .


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<?php
// Example 003
// Download Image (Binnary File)
// Copyright http://scriptime.blogspot.in
$url = "http://2.bp.blogspot.com/-lZXXoJ0Ujy0/UNQLKJ8MY-I/AAAAAAAAAH8/qH2FKTJayVo/s1600/php-curl.png"; // URL to Download Image
$ch = curl_init(); // Initialize a CURL session.
curl_setopt($ch, CURLOPT_URL, $url); // Pass URL as parameter.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Return stream contents.
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); // We'll be returning this transfer, and the data is binary
$data = curl_exec($ch); // // Grab the jpg and save the contents in the
//$data variable
curl_close($ch); // close curl resource, and free up system resources.
// Set the header to type image/jpeg, since that's what we're
// displaying
header("Content-type: image/jpeg");
echo $data; // Print stream contents.
?>


0 comments:

Post a Comment

Fashion