How do I download an external image off a url with cURL?
See here:
http://www.edmondscommerce.co.uk/php/php-save-images-using-curl/
function save_image($img,$fullpath){
$ch = curl_init ($img);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);
if(file_exists($fullpath)){
unlink($fullpath);
}
$fp = fopen($fullpath,'x');
fwrite($fp, $rawdata);
fclose($fp);
}
Other articles/sources:
http://forums.digitalpoint.com/showthread.php?t=371632
http://www.bitrepository.com/download-image.html
http://php.bigresource.com/Track/php-Jjg3DsKY/
from php.net - also reads into a string.
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
?>
Related
I am using this script to upload myfile by curl function and ftp connection.
In local it works fine but in my server file is appeared uploaded but it have zero file size.
what is wrong? Thank You.
$ch = curl_init();
$localfile = (dirname(__FILE__).'/asset/myfile.zip');
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, "ftp://$user_name:$user_pass#$server/".'myfile.zip');
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
curl_close ($ch);
and also how can I upload multiple files in this script like:
$localfile1 = (dirname(__FILE__).'/asset/myfile1.zip');
$localfile2 = (dirname(__FILE__).'/asset/myfile2.zip');
$localfile3 = (dirname(__FILE__).'/asset/myfile3.zip');
This source may helps your problem.
http://www.web-development-blog.com/archives/tutorial-ftp-upload-via-curl/
To upload the file in curl you can use the curl_file_create.Try the below one:
$localfile = (dirname(__FILE__).'/asset/myfile.zip');
$curl_file = curl_file_create($localfile,'zip');
$params = ['file' => $curl_file];
$ch = curl_init();
$localfile = (dirname(__FILE__).'/asset/myfile.zip');
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, "ftp://$user_name:$user_pass#$server/".'myfile.zip');
curl_setopt($ch, CURLOPT_UPLOAD, 1);
//curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
curl_close ($ch);
I have uploaded my images using Amazon S3 Services at this location "https://s3.amazonaws.com/qbprod/"
After uploading i get response like
https://s3.amazonaws.com/qbprod/70dcdd564f5d4b15b32b975be15e4a1200
I try to get image by below ways but not getting success.
(1)................
$xman = explode("/",$ImageSTR);
//$saveto = end($xman).'.jpg';
$saveto = end($xman);
$fl = file_get_contents($ImageSTR);
file_put_contents('abcd.jpg',$fl);
(2)................
grab_image($ImageSTR,$saveto);
function grab_image($url,$saveto){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw=curl_exec($ch);
curl_close ($ch);
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);
}
Thank You In Advance
U can see this is https and you need using CURLOPT_SSL_VERIFYPEER like this
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
And this is final function is
function grab_image($url,$saveto){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$raw=curl_exec($ch);
curl_close ($ch);
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);
}
You can read here for more CURL
http://php.net/manual/en/function.curl-setopt.php
Hope this help!
i am trying to save a remote image to a file using php curl but the file never get saved! could any one help me how to troublshoot this problem ? the image file never get created and but echo $returned_content has the data!
<?
$returned_content = get_data('http://somesite.com/43534545345dfsdfdsfdsfds.jpg');
echo $returned_content;
$fp = fopen('43534545345dfsdfdsfdsfds.jpg', 'w');
fwrite($fp, $returned_content);
fclose($fp);
/* gets the data from a URL */
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);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
<?php
$ch = curl_init('http://www.example/2012/09/flower.jpg');
$fp = fopen('/localProject/imagesFolder/newname.jpg', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
Try this
<?php
function getImagen($url, $rename, $ch)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec ($ch);
curl_close ($ch);
$fp = fopen("$rename.jpg",'w');
fwrite($fp, $rawdata);
fclose($fp);
}
$ch = curl_init();
$image ="http://sampleurl.com/imagen.jpg";
getImagen ($image, "imagen", $ch);
?>
Works 100% :)
I have an url with an image: http://www.example.com/image.jpg
and I have an action in the controller that uploads a file to the server by user input. I want to use this same action to upload a file by url. I started to get the data from image:
$url = "http://www.example.com/image.jpg";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);
Now how do I send the $rawdata to the controller to be handled like a normal user upload?
Thanks
I figured it out myself.
$url = "http://www.example.com/image.jpg";
$filename = basename($url, rand_string(7));
$path = realpath("../webroot/uploads/")."/".$filename;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);
if(file_exists($path)){
unlink($path);
}
$file = fopen($path, 'x');
fwrite($file, $rawdata);
fclose($file);
$post_data['Filedata'] = '#'.$path;
$url = "http://localhost/controller/action";
$ch = curl_init();
// Set URL on which you want to post the Form and/or data
curl_setopt($ch, CURLOPT_URL, $url);
// Data+Files to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// Pass TRUE or 1 if you want to wait for and catch the response against the request made
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// For Debug mode; shows up any error encountered during the operation
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// Execute the request
$response = curl_exec($ch);
echo $response;
In the controller:
function action(){
//get properties of the file
//$_FILES['Filedata']['name'];
//$_FILES['Filedata']['size'];
//$_FILES['Filedata']['tmp_name'];
//savefile();
}
If anyone knows how to post the file without saving it will be great.
Trying to save image to my server using cURL. The image appears to download. It shows the correct bytes but when i link image does not work. I then DL to see and nope its a blank image.
here is my code... whats the issue with it?
$ch = curl_init("'. $image .'");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec ($ch);
curl_close ($ch);
$fp = fopen("$rename.jpg",'w');
fwrite($fp, $rawdata);
fclose($fp);
I test your script it work fine for me, just remove the useless double quote and dot for $image.
<?
$image ="http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=5";
$rename="123";
$ch = curl_init($image);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec ($ch);
curl_close ($ch);
$fp = fopen("$rename.jpg",'w');
fwrite($fp, $rawdata);
fclose($fp);
?>