I am trying to download a gzip file using curl and php and saving it in a specified location. Below is the code that I am using but everytime it's saving an empty file allthough the gzip file that it's downloading is proper
$file_zip = 'abc.gz'
$fp = fopen("$file_zip", "w");
$request = curl_init();
$headers = array('Content-type: application/x-gzip','Connection: Close');
curl_setopt($request, CURLOPT_URL, $url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($request, CURLOPT_TIMEOUT, 0);
curl_setopt($request, CURLOPT_CONNECTTIMEOUT,0);
curl_setopt($request, CURLOPT_HTTPHEADER,$headers);
curl_setopt($request, CURLOPT_FAILONERROR, true);
curl_setopt($request, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($request, CURLOPT_AUTOREFERER, true);
curl_setopt($request, CURLOPT_BINARYTRANSFER,true);
curl_setopt($request, CURLOPT_FILE, $fp);
$zipFile = curl_exec($request);
I found a method that downloads a file to a location with cUrl. Hope it helps:
<?php
function get_file1($file, $local_path, $newfilename)
{
$err_msg = '';
echo "<br>Attempting message download for $file<br>";
$out = fopen($newfilename, 'wb');
if ($out == FALSE){
print "File not opened<br>";
exit;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $file);
curl_exec($ch);
echo "<br>Error is : ".curl_error ( $ch);
curl_close($ch);
//fclose($handle);
}//end function
?>
The source is : http://www.weberdev.com/get_example.php3?ExampleID=4009
Related
The task - there is a direct link to download the file, you need to use php methods to download this file and save it on the server.
There is code, but it does not load the file, but only creates a new one.
<?php
$token = 'token';
// file from yandex disk.
$yd_file = '/FermerPrice/test.jpeg/';
// save this.
$path = download;
//get array with link
$ch = curl_init('https://cloud-api.yandex.net/v1/disk/resources/download?path=' . urlencode($yd_file));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: OAuth ' . $token));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$res = curl_exec($ch);
curl_close($ch);
// $res['href'] - link download
$res = json_decode($res, true);
if (empty($res['error'])) {
$file_name = $path . '/' . basename($yd_file);
$file = #fopen($file_name, 'w');
$ch = curl_init($res['href']);
curl_setopt($ch, CURLOPT_FILE, $file);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: OAuth ' . $token));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($file);
echo "Good.";
}
?>
You can use this:
file_put_contents("localfilename", file_get_contents('https://cloud-api.yandex.net/v1/disk/resources/download?path=' . urlencode($yd_file)));
I want to attach a PDF file to the mail via CURL response.
I have an URL which prompts to download the file woth Ok button (Windows default).
I have tried below code snippets but no luck..
$hCurl = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, $verbose = fopen('php://temp', 'rw+'));
curl_setopt($hCurl, CURLOPT_PUT, true);
curl_setopt($hCurl, CURLOPT_HEADER, true);
curl_setopt($hCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($hCurl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($hCurl, CURLOPT_URL, "http://exampe.com/invoice/41/download");
$fp = fopen ($finfo['tmp_name'], "r");
curl_setopt($hCurl, CURLOPT_INFILE, $fp);
curl_setopt($hCurl, CURLOPT_INFILESIZE, $finfo['size']);
$sResp = curl_exec($hCurl);
rewind($verbose);
$debug = "Verbose info:\n<pre>" . htmlspecialchars(stream_get_contents($verbose)) ."</pre>";
echo $finfo['tmp_name'];
fclose($verbose);
curl_close($hCurl);
AND
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch, CURLOPT_URL, "http://exampe.com/invoice/41/download");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$output = curl_exec($ch);
$file = fopen($output, 'r');
$size = filesize($output);
$fildata = fread($file,$size);
curl_close($ch);
And then tried to add response with
$mail->addAttachment($response);
I wonder if i can ever do it or if i am doing anything wrong here?
$url = https://r3---sn-5jucgv5qc5oq-cagl.googlevideo.com/videoplayback?initcwndbps=2133750&mime=video%2Fmp4&ipbits=0&fexp=900720%2C907263%2C909721%2C930824%2C934954%2C9405651%2C9405972%2C9406849%2C9407103%2C9407756%2C9407880%2C9408102%2C942310%2C943610%2C948124%2C948703%2C951511%2C951703%2C952302%2C952612%2C952901%2C955301%2C957201%2C957507%2C958600%2C959701%2C961404%2C962727&ratebypass=yes&itag=18&requiressl=yes&sver=3&mm=31&id=o-AH3LjJikaNXn9Fxj1nCeZeGQxBRB2Env2Cd-Rwumwn2U&mt=1426837685&mv=m&ms=au&ip=106.51.132.147&dur=336.596&pl=24&upn=NV9m09pWiSg&source=youtube&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Cmime%2Cmm%2Cms%2Cmv%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cupn%2Cexpire&expire=1426859426&key=yt5&signature=42A2FFA9C6EAFF6EBACC605EE5E06FA7B38805DB.9F608D789F6516F43E25A77B841B428975ADC7A4
How to get the size of file reffered by this URL. when I open this url the video file starts streaming. But I need the file size in order to force download the file to browser using the header('content-length') field
You can do this by using cURL :
I can see you are using a https URL So it will surly help you
<?php
$ch = curl_init();
// curl_setopt($ch, CURLOPT_URL, 'http://upload.wikimedia.org/wikipedia/commons/a/af/Bonsai_IMG_6426.jpg');
curl_setopt($ch, CURLOPT_URL, 'https://r3---sn-5jucgv5qc5oq-cagl.googlevideo.com/videoplayback?initcwndbps=2133750&mime=video%2Fmp4&ipbits=0&fexp=900720%2C907263%2C909721%2C930824%2C934954%2C9405651%2C9405972%2C9406849%2C9407103%2C9407756%2C9407880%2C9408102%2C942310%2C943610%2C948124%2C948703%2C951511%2C951703%2C952302%2C952612%2C952901%2C955301%2C957201%2C957507%2C958600%2C959701%2C961404%2C962727&ratebypass=yes&itag=18&requiressl=yes&sver=3&mm=31&id=o-AH3LjJikaNXn9Fxj1nCeZeGQxBRB2Env2Cd-Rwumwn2U&mt=1426837685&mv=m&ms=au&ip=106.51.132.147&dur=336.596&pl=24&upn=NV9m09pWiSg&source=youtube&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Cmime%2Cmm%2Cms%2Cmv%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cupn%2Cexpire&expire=1426859426&key=yt5&signature=42A2FFA9C6EAFF6EBACC605EE5E06FA7B38805DB.9F608D789F6516F43E25A77B841B428975ADC7A4');
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
var_dump($size);
?>
.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://weknowyourdreams.com/images/love/love-01.jpg');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
var_dump($size);
?>
Try curl to get size of any file by curl_getinfo()
$ch = curl_init('http://www.example.com/yourfile'); //put your url
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
$data = curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
echo $size;
Try to use filesize():
<?php
$filename = 'somefile.txt';
echo $filename . ': ' . filesize($filename) . ' bytes';
?>
I want to upload the image to the url using Curl.
But as the Image file is on https url i am not able to read the file using fopen.
Code is as below.
$file = "https://xyz.com/image.jpg";
$url = "http://abc.com/upload.php";
$fp = fopen($file, "r");
$headers = array("Content-Type: xml");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$responseInfo = curl_getinfo($ch);
curl_close($ch);
I think that this should work assuming that you don't need to pass the file data that you're uploading in as a named key/value pair.
$file = "https://xyz.com/image.jpg";
$url = "http://abc.com/upload.php";
$fileData = file_get_contents($file);
$fp = fopen($file, "r");
$headers = array("Content-Type: xml");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fileData);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$responseInfo = curl_getinfo($ch);
curl_close($ch);
Instead of:
curl_setopt($ch, CURLOPT_PUT, true);
opt setting for PUT API request will work fine on using this setting below:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
I want to download files from offsite server. I do have consent to do so if that comes up :) What i have is a cURL script that grabs the title and file name but for whatever reason i cant get it to dl the file to my server.
$downloadvUrl="$videos";
$file = "downloads";
$fileName=$downloadvUrl;
$header = array(
"Accept-Encoding: gzip,deflate",
"Accept-Charset: utf-8;q=0.7,*;q=0.7",
"Connection: close"
);
$file2 = dirname(__file__).'/'.videos.'.m4v';
$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, $downloadvUrl);
$response = curl_exec($curlObj);
$return = false;
if(!curl_errno($curlObj)) {
file_put_contents($file2, $response);
}
I actually got it to work by making it a bit more simple.
<?php
$i = 0;
while(file_exists($target_path).$i) {
$i++;
}
$ch = curl_init("$matches[1]");
$fp = fopen("1.m4v", "w");
if (file_exists($fp)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
How can i get it so the files are upload with different names?
You can add:
$fh = fopen('/tmp/foo', 'w');
curl_setopt($ch, CURLOPT_FILE, $fh);
Change /tmp/foo to the file you wish it to be saved to.