PHP cURL downloaded file is empty - php

So I want to download a mp3 file from a dynamic link, when I run the script it creates test.mp3 but it's empty.
Code :
$source = 'http://www.youtubeinmp3.com/download/get/?
i=yt6MfhzTljRBZ6AjqSsnb2ih1ZifuMoY&e=69';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_SSLVERSION,0);
$data = curl_exec ($ch);
$error = curl_error($ch);
curl_close ($ch);
$destination = "test.mp3";
$file = fopen($destination, "w+");
fwrite($file, $data);
fclose($file);
There is nothing in the error.log
EDIT
If I add :
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
It downloads the source of the page (HTML) but not the mp3 file.

Because You are not downloading anything.
Check output of your $data = curl_exec ($ch);. It must be empty

Related

How to upload file by PHP cURL using ftp connection?

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);

Saving and image to server from a URL using cURL results in a blank image

$camera_ip = "10.10.10.10";
$image_url = "http://$camera_ip/cgi-bin/image.jpg?camera=right&size=1024x768&quality=60";
$ch = curl_init($image_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$rawdata = curl_exec($ch);
curl_close($ch);
$fp = fopen('./logo.jpg', 'wb');
fwrite($fp, $rawdata);
fclose($fp);
A file is being saved at 7KB but the size is 0x0 ... I can't figure out why the file isn't being saved correctly. My guess is that the file isn't being loaded fully before being saved, but is there a way of ensuring that?
Try using "w" instead of "wb" $fp = fopen('./logo.jpg', 'w');
Or try: file_put_contents('./logo.jpg',$rawdata); (not sure it is binary safe, should be)
And
Check your curl result to see what came back.
$info = var_export(curl_getinfo($ch),true);
file_put_contents('log.txt',$info);
OR
header('content-type: text/plain');
var_export(curl_getinfo($ch));
UPDATE
The curl Request failed (returned false).
Now you need to find out WHY.
For now change your:
curl_setopt($ch, CURLOPT_HEADER, 0);
To:
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
Add these options for trouble shooting:
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$data = curl_exec($ch);
if (curl_errno($ch)){
echo ' Retrieve Base Page Error: ' . curl_error($ch);
}
else {
$info = rawurldecode(var_export(curl_getinfo($ch),true));
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$responseHeader= substr($data,0,$skip);
$data= substr($data,$skip);
echo "HEADER: $responseHeader\n";
echo "\n\nINFO: $info\n\nDATA: $data";
}
You will likely get the "Retrieve Base Page Error:" but it will also tell WHY it failed. If you cannot echo, replace echo with $info .=

PHP CURL Download File

im trying to download a file from a url,
when I use the browser the download dialog works but when I use this code the new file on my server stay empty.
$ch = curl_init();
$source = "https://myapps.gia.edu/ReportCheckPortal/downloadReport.do?reportNo=$row['certNo']&weight=$row['carat']";
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);
$destination = "./files/certs/$row['certNo'].pdf";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);
example of url: https://myapps.gia.edu/ReportCheckPortal/downloadReport.do?reportNo=1152872617&weight=1.35
I solved this problem using this:
curl_setopt($ch, CURLOPT_SSLVERSION,3);
This is the final code:
$source = "https://myapps.gia.edu/ReportCheckPortal/downloadReport.do?reportNo=1152872617&weight=1.35";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSLVERSION,3);
$data = curl_exec ($ch);
$error = curl_error($ch);
curl_close ($ch);
$destination = "./files/test.pdf";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);
Your url using https connection. Use the following curl option as well.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
Did you check if result actually contains data?
with fputs on larger files you need to specify byte length
Try using file_put_contents($destination, $data);
See if you can use the following code to help you.
//Create a cURL handle.
$ch = curl_init($fileUrl);
//Pass our file handle to cURL.
curl_setopt($ch, CURLOPT_FILE, $fp);

How to send a file with curl to the controller

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.

Downloading an Image From an External URL With cURL

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);
?>

Categories