For smart Primates & ROBOTS (oh and ALIENS ).

Blogroll

Friday, October 17, 2014

Download image without cURL and with cURL in PHP

 Download image without cURL and with cURL in PHP




<?php
// without cURL
$downloadImageUrl="http://fc04.deviantart.net/fs26/f/2008/072/b/1/Frozen_Galaxy_by_Vpr87.jpg";
////// Using without CURL
$img = file_get_contents($downloadImageUrl);
$file1 = dirname(__file__).'/aaa.jpg';
file_put_contents($file1, $img);



////// // with cURL
$fileName=$downloadImageUrl;
$header = array(
            "Accept-Encoding: gzip,deflate",
            "Accept-Charset: utf-8;q=0.7,*;q=0.7",
            "Connection: close"
        );
$useragent = 'Amit Kumar Gaur amitt800@gmail.com  http://amitkgaur.blogspot.com';
$file2 = dirname(__file__).'/bbb.jpg';
$curlObj=curl_init();
curl_setopt($curlObj, CURLOPT_HTTPHEADER, $header);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlObj, CURLOPT_HEADER, false);
curl_setopt($curlObj, CURLOPT_USERAGENT, $useragent);
curl_setopt($curlObj, CURLOPT_CONNECTTIMEOUT, 999);
curl_setopt($curlObj, CURLOPT_TIMEOUT, 9999);
curl_setopt($curlObj, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curlObj, CURLOPT_URL, $downloadImageUrl);
$response = curl_exec($curlObj);
$return = false;
if(!curl_errno($curlObj)) {
    file_put_contents($file2, $response);
}
?>
Share:

0 comments:

Post a Comment

Multiple attribute passing in querySelectorAll

Multiple attribute passing in querySelectorAll     Here I am demonstrating code to how to pass multiple attributes in querySelectorAll. <...

Ads Inside Post

Powered by Blogger.

Arsip