Use PHP cURL to download and Excel file - php

I've been trying for a few days to be able to call a url on a remote server that automatically downloads an Excel file when you put the url into a browser. I made a page locally that automatically downloads the file for testing, but I cannot get cURL to do it. The script returns no errors and says it was successful, but the file it writes to becomes corrupt and opens blank.
Here is the code...
$output_file = 'E:\Downloads\Export.xlsx';
$download_url = 'http://localhost/test/urlexport.php';
$ch = curl_init();
$out = fopen($output_file, 'w');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_URL, $download_url);
$output = curl_exec($ch);
fwrite($out, $output);
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo 'Operation completed without any errors';
}
if(curl_errno($ch)){
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
I've spent quite a bit of time researching this but have yet to find an answer that actually works. Any help is appreciated, thanks.

If you're not sure what you're doing - keep it simple.
Here's the simplest possible way to do what you're doing, which is downloading a file and then saving it to disk.
<?php
$output_file = 'E:\Downloads\Export.xlsx';
$download_url = 'http://localhost/test/urlexport.php';
$file = file_get_contents($download_url);
file_put_contents($output_file, $file);
If this doesn't work then your download url isn't doing what you think it's doing. Take a look at what's actually in the $output_file (open it in a text editor) - maybe it's some html.

Related

Trying to get data from a webhook with php json

I'm trying to download a file, which URL I get from a webhook:
<?php
$webhookResponse = json_decode(file_get_contents('php://input'), true);
//The resource that we want to download.
$fileUrl = $webhookResponse["data"]["orderDetails"][0]["detailFiles"][0]["fileUrl"];
//The path & filename to save to.
$saveTo = 'test.pdf';
//Open file handler.
$fp = fopen($saveTo, 'w+');
//If $fp is FALSE, something went wrong.
if($fp === false){
throw new Exception('Could not open: ' . $saveTo);
}
//Create a cURL handle.
$ch = curl_init($fileUrl);
//Pass our file handle to cURL.
curl_setopt($ch, CURLOPT_FILE, $fp);
//Timeout if the file doesn't download after 20 seconds.
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
//Execute the request.
curl_exec($ch);
//If there was an error, throw an Exception
if(curl_errno($ch)){
throw new Exception(curl_error($ch));
}
//Get the HTTP status code.
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//Close the cURL handler.
curl_close($ch);
//Close the file handler.
fclose($fp);
if($statusCode == 200){
echo 'Downloaded!';
} else{
echo "Status Code: " . $statusCode;
}
?>
When I try to run it, it gives me an error:
Trying to access array offset on value of type null
This is how my array looks that I get from webhook:
Interesting thing, if I put this same script on another server, it works fine, it downloads the file, if I put it one the actual one I need to use (ubuntu, nginx) it gives me this error, could this be a little server related?

How to download client-site a video with cURL / PHP?

If the following PHP is runnig, I would like to download the file with `cURL on the client-site. It means, if one of my website visitor run's an action, which starts this PHP file, it should download the file on this PC.
I tried with different locations, but without any success. If I run my code, it does always download it on my WebSever, which is not this, what I want.
<?php
//The resource that we want to download.
$fileUrl = 'https://www.example.com/this-is-a-example-video';
//The path & filename to save to.
$saveTo = 'test.mp4';
//Open file handler.
$fp = fopen($saveTo, 'w+');
//If $fp is FALSE, something went wrong.
if($fp === false){
throw new Exception('Could not open: ' . $saveTo);
}
//Create a cURL handle.
$ch = curl_init($fileUrl);
//Pass our file handle to cURL.
curl_setopt($ch, CURLOPT_FILE, $fp);
//Timeout if the file doesn't download after 20 seconds.
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
//Execute the request.
curl_exec($ch);
//If there was an error, throw an Exception
if(curl_errno($ch)){
throw new Exception(curl_error($ch));
}
//Get the HTTP status code.
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//Close the cURL handler.
curl_close($ch);
if($statusCode == 200){
echo 'Downloaded!';
} else{
echo "Status Code: " . $statusCode;
}
?>
How can I change the cURL downloading process to the client-site?
PHP cannot run client-side.
You could use cURL to download data to the server (without saving it to a file) and then output that data to the client.
Don't do this:
//Open file handler.
$fp = fopen($saveTo, 'w+');
or this:
//Pass our file handle to cURL.
curl_setopt($ch, CURLOPT_FILE, $fp);
Then capture the output:
//Execute the request.
curl_exec($ch);
Should be:
//Execute the request.
$output = curl_exec($ch);
Then you can:
echo $output;
… but make sure you set the Content-Type and consider setting the Content-Length response headers. You might also want Content-Disposition.
Under most circumstances, it would probably be better to simply send the browser to fetch the file directly instead of proxying it through the server.
$fileUrl = 'https://www.example.com/this-is-a-example-video';
header("Location: $fileUrl");

file_put_contents is unable to get whole file

I am using hostmonster as hosting provider.
I have written below code.
$mp4_url = 'http://www.w3schools.com/html/mov_bbb.mp4';
$filename = 'sample.mp4';
file_put_contents( $filename , fopen($mp4_url, 'r' ) );
I am seeing strange behaviour. Sometimes it works but sometimes it copies only few bites like sometimes size of file is 100kb sometimes 600kb sometimes it copies whole file.
Please suggest what should I do to copy any mp4 file from any server to our server.
We have to copy large files, size can be 600MB or 1GB.
Try copy(). This will do your job:
$file = 'http://www.w3schools.com/html/mov_bbb.mp4';
$newfile = $_SERVER['DOCUMENT_ROOT'] . '/sample.mp4';
if ( copy($file, $newfile) ) {
echo "Copy success!";
}else{
echo "Copy failed.";
}
use curl rather than using file_put_contents
$url='http://www.w3schools.com/html/mov_bbb.mp4';
$saveto='path';
$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);

Sending imagepng() object to server using cURL

I'm trying to take an imagepng() object and upload it to a different server using cURL. Here is the code I've got. To be clear, I know the imagepng file works correctly and is being generated because I can save it locally on the server the code is running on. I'm just not sure how to send that info to a new server. All of the variables are set before this code ($fileName, $imageObject, etc.):
$file = imagepng($imageObject, 'newTest'.$counter.'.png');
if($file){
$ch = curl_init();
$fp = $file;
curl_setopt($ch, CURLOPT_URL, 'ftp://'.$ftp_user.':'.$ftp_pass.'#'.$ftp_server.'/'.$fileName.'.png');
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);
if ($error_no == 0) {
$error = 'File uploaded succesfully.';
} else {
$error = 'File upload error.';
}
}
The errors I am getting for every file (this code is in a loop processing multiple files) is. Of course {MY_URL} is replaced with the actual URL of my file:
Warning: curl_setopt(): supplied argument is not a valid File-Handle resource in {MY_URL} on line 43
Warning: filesize() [function.filesize]: stat failed for 1 in {MY_URL} on line 44
So it appears that the file is the wrong format when it's being cURLed. What do I need to set it to in order to send it correctly?
Thanks for your help!
imagepng outputs image into a file,but does not return a file handle (it only returns TRUE in case of success). You need to use something like fopen to get a valid file handle. Try replacing $fp=$file with this:
$fp = fopen('newTest'.$counter.'.png', "rb");
Also, replace filesize($file) with filesize($fp).
In general, $file is just a boolean, not a file handle. Use $fp for every function that expects a file handle. Also, don't forget to close every file at the end of the loop (e.g. add the following line after curl_close):
fclose($fp);

How can I send and receive put query with same script

I need to receive image send by PUT method. So I'm wrinting script for testing it. I want to receive and send it with the same script. How can I implement this? The following variant echoes nothing and string about Congratulations that http method was send ok.
<?php
//if they DID upload a file...
var_dump(file_get_contents("php://input"));
if($_FILES['photo']['tmp_name'])
{
echo $_FILES['photo']['error'];
if($_FILES['photo']['error']==0)
{
//now is the time to modify the future file name and validate the file
$new_file_name = strtolower($_FILES['photo']['tmp_name']); //rename file
$message = 'Congratulations!!!!!!!.';
//move it to where we want it to be
move_uploaded_file($_FILES['photo']['tmp_name'], 'url.../1.jpg');
echo'Congratulations! Your file was accepted.';
$image = fopen('url.../1.jpg', "rb");
var_dump($image);
$ch = curl_init();
/* Set cURL options. */
curl_setopt($ch, CURLOPT_URL, "http://url.../upload.php");
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILE, $image);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($image));
/* Execute the PUT and clean up */
$result = curl_exec($ch);
fclose($image); //recommended to close the fileshandler after action
curl_close($ch);
}
//if there is an error...
else
{
//set that to be the returned message
$message = 'Ooops! Your upload triggered the following error: '.$_FILES['photo']['error'];
}
}
else
{
echo"WORKS";
}
The most easiest way to send back the image to a browser is via using a URL.
Process the image
Save the image somewhere on your sever
Send the URL back to browser.
Use a tag at the browser and show your image.
<?PHP
$imgFile="";
if($_FILES['photo']['tmp_name'])
{
///Your existing code
$imgFile="http://" . $_SERVER['HTTP_HOST'] .'/Your image url here';
//Ex:\\ http://yourserver.com/images/1.jpg -
//you can take this from your move_upload_file
}
?>
<img src="<?PHP echo $imgFile; ?>" />
Useful links How to receive a file via HTTP PUT with PHP
Even for a restful service u can use json or xml to send the image url back. PUT is not a good idea unless u need to send back image data for some reason. May be u should rethink your logic?
The mistakes was:
strlen($image) strlen if wrong, must be filesize and in my url I had www. It's not netion in my post but it was mistake.
More experienced programmer helped me.
r.php to read stream:
$res = file_get_contents("php://input");
$file = fopen('1.txt', "w");
fputs($file, $res);
fclose($file);
var_dump($res);
s.php to get stream and to init r.php
$image = fopen('/var/www/testfiles/1.jpg', "rb");
var_dump($image);
$ch = curl_init();
/* Set cURL options. */
curl_setopt($ch, CURLOPT_URL, "http://url withot www/r.php");
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILE, $image);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize('/var/www/testfiles/1.jpg'));
/* Execute the PUT and clean up */
$result = curl_exec($ch);
fclose($image); //recommended to close the fileshandler after action
curl_close($ch);
die("OK");

Categories