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);
?>
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);
hi there I want download gmail profile image like this:
$src = 'https://lh3.googleusercontent.com/-MsxFngq63hA/AAAAAAAAAAI/AAAAAAAAAFc/kKIrjln2a_s/photo.jpg?sz=200';
with this curl:
$ch = curl_init ($src);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$rawdata=curl_exec($ch);
curl_close ($ch);
$fp=fopen("./imageProcess.jpg","w+");
fwrite($fp, $rawdata);
fclose($fp);
but result image is blank with 0 size.
what is the wrong?
Not exactly your code, But this one is shorter and not curl complicated
<?php
$content = file_get_contents($url);
file_put_contents("a.jpg", $current);
?>
Hope this helps, unless you also want to edit the raw headers
I am trying to use PHP CURL to save this image to my computer, but isnt working, any help would be appreciated.
Image to Save:
https://s.yimg.com/aw/api/res/1.2/MRhsVBj10l4TrtNg5j7eBA--/YXBwaWQ9eXR3YXVjdGlvbnNlcnZpY2U7aD0xNTA7cT04NTtzcj0xLjI7c3M9MS4yO3c9MjAw/http://nevec-img.zenfs.com/prod/tw_ec05-7/258e866a-b39a-471d-8e6c-62801cbccd9d.jpg
My Curl function:
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_FOLLOWLOCATION, 0);
$rawdata=curl_exec($ch);
curl_close ($ch);
if(file_exists($saveto)){
($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $rawdata);
fclose($fp);
}
I am pretty sure the permission for the place to save to is 777 because I can save image from other website without any problem.
Thanks again
It's a HTTPS request, please add curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
If you want to know what's going on, add $errmsg = curl_error($ch); before curl_close, and then echo $errmsg;.
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!
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);
?>