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)));
Related
My requirement is to upload a file to my google drive using Php cURL and then rename the upload file and move the file to a specific folder.
For this, I have done oAuth and successfully uploaded a file from my website to google drive using the below code.
$image = "../../../".$name;
$apiURL = 'https://www.googleapis.com/upload/drive/v3/files?uploadType=media';
$mime_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
$folder_id = "1WBkQQ6y0TPt2gmFR3PKCzSip_aAuuNEa";
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, $apiURL);
curl_setopt($ch1, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_POSTFIELDS, file_get_contents($image));
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_HTTPHEADER, array('Content-Type: '.$mime_type, 'Authorization: Bearer ' . $access_token) );
// execute cURL request
$response=curl_exec($ch1);
if($response === false){
$output = 'ERROR: '.curl_error($ch1);
} else{
$output = $response;
}
// close first request handler
curl_close($ch1);
$this_response_arr = json_decode($response, true);
The file is uploaded as Untitled and I used the below code to rename it to a proper filename as per my requirement.
if(isset($this_response_arr['id'])){
$this_file_id = $this_response_arr['id'];
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, 'https://www.googleapis.com/drive/v3/files/'.$this_file_id);
curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, 'PATCH');
$post_fields = array();
$this_file_name = explode('.', $name);
$post_fields['name'] = $this_file_name[0];
curl_setopt($ch2, CURLOPT_POSTFIELDS, json_encode($post_fields));
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token) );
$response2 = curl_exec($ch2);
if($response2 === false){
$output2 = 'ERROR: '.curl_error($ch2);
} else{
$output2 = $response2;
}
curl_close($ch2);
$this_response2 = json_decode($response2, true);
}
Now I want to move this uploaded file in the Google drive root folder to a specific folder. I tried adding the “Parents” , “addParents”, “removeParents” parameters in post body along with "Name" parameter and and also as a separate cURL Patch request but none of them is working.
if($this_response2['id']){
$this_f_id = $this_response2['id'];
$ch3 = curl_init();
curl_setopt($ch3, CURLOPT_URL, 'https://www.googleapis.com/drive/v3/files/'.$this_f_id);
curl_setopt($ch3, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch3, CURLOPT_POST, 1);
$post_fields1 = array();
$post_fields1['addParents'] = $folder_id;
$post_fields1['removeParents'] = "root";
curl_setopt($ch3, CURLOPT_POSTFIELDS, json_encode($post_fields1));
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch3, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token) );
$response3 = curl_exec($ch3);
if($response3 === false){
$output3 = 'ERROR: '.curl_error($ch3);
} else{
$output3 = $response3;
}
curl_close($ch3);
}
Any help would be appreciated.
Documentation for uploading,renaming and moving the file in gdrive is not properly documented for Php cURL and there are not much examples available too.
There is no need to send an additional cURL request for moving the file to a specific folder. This can be done in the second cURL request itself.
The mistake in your code is, you are sending the addParents and removeParents in Request Body instead of sending this as query parameters.
You can modify the second cURL as below to update the name of uploaded file and for moving the file inside a specific folder.
if(isset($this_response_arr['id'])){
$this_file_id = $this_response_arr['id'];
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, 'https://www.googleapis.com/drive/v3/files/'.$this_file_id.'?addParents='.$folder_id.'&removeParents=root');
curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, 'PATCH');
$post_fields = array();
$this_file_name = explode('.', $name);
$post_fields['name'] = $this_file_name[0];
curl_setopt($ch2, CURLOPT_POSTFIELDS, json_encode($post_fields));
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token) );
$response2 = curl_exec($ch2);
if($response2 === false){
$output2 = 'ERROR: '.curl_error($ch2);
} else{
$output2 = $response2;
}
curl_close($ch2);
$this_response2 = json_decode($response2, true);
}
Let me know if this works.
I'm attempting to download file from dropbox to my server, nothing is returned but i know the file exists
$headers = array('Authorization: Bearer '.$access_token,
'Content-Type: application/json',
'Dropbox-API-Arg: {"path": "'.$filepath.'"}');
$file = fopen($filename, "w+");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://content.dropboxapi.com/2/files/download");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FILE, $file);
$result = curl_exec($ch);
curl_close ($ch);
fclose($file);
Where did i go wrong? How do i solve?
try this way and check the output of the result. and save it to the local server.
$url = 'https://content.dropboxapi.com/2/files/download';
$formData = [
"path"=> $filepath
];
$ch = curl_init($url);
$authorization = "Authorization: Bearer ".$access_token;
$request_data = "Dropbox-API-Arg: ".json_encode($formData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorization, $request_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
dd($result)
$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");
First of all I'm aware of curl_multi_init but it doesn't exactly do what I need, to my knowledge.
Im downloading files behind a login, so
a) Need to login
b) Download image(s)
Here is what I currently have
$login_url = 'https://us.test.com/Member/Login';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$login_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'login='.$this->username.'&password='.$this->userpass.'&signin=1&login_referer=none&remember_me=1');
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $_SERVER['DOCUMENT_ROOT'].'/test/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, $_SERVER['DOCUMENT_ROOT'].'/test/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, true);
curl_exec($ch);
//Grab and Write the image
$image = file_get_contents($image_url);
$f = fopen($_SERVER['DOCUMENT_ROOT'].'/test/uploads/'.$id.'.jpg', 'w');
fwrite($f, $image);
fclose($f);
curl_close($ch);
Anything helps, much appreciated.
I figured it out, just added session ID into cURL. But a quick and dirty way is
$download_str = 'wget -O '. $save . $id . '.jpg --cookies=off --header "Cookie: PHPSESSID=' . $session_id . '" ' . $image_url;
shell_exec($download_str);