Last active
September 25, 2017 14:20
-
-
Save LipinArts/014c6978eb61a9e2b4ac0eb4634ce376 to your computer and use it in GitHub Desktop.
Upload file via API into Dropbox and create public link for this file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Загружаем файлы в dropbox по API | |
$file_path_str = '/images/photo.jpg' //путь к файлу | |
$url_path_str = 'https://api-content.dropbox.com/1/files_put/auto/'.$file_path_str;//задаем куда загружать | |
$header = array( | |
"Authorization:Bearer YOUR_DROPBOX_TOKEN_HERE",//авторизация | |
"Content-Length:".filesize($file_path_str) | |
); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url_path_str); | |
curl_setopt($ch, CURLOPT_PUT, 1); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); | |
$fh_res = fopen($file_path_str, 'r'); | |
$file_data_str = fread($fh_res, filesize($file_path_str)); | |
rewind($fh_res); | |
curl_setopt($ch, CURLOPT_INFILE, $fh_res); | |
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path_str)); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$curl_response_res = curl_exec ($ch); | |
$results = json_decode($curl_response_res,true);//получаем ответ от сервера с массивом данных | |
curl_close($ch); | |
fclose($fh_res); | |
// Делаем публичную ссылку на загруженный файл | |
$parameters = array('path'=>$results['path']); | |
$headers = array('Authorization: Bearer YOUR_DROPBOX_TOKEN_HERE', | |
'Content-Type: application/json'); | |
$curlOptions = array( | |
CURLOPT_HTTPHEADER => $headers, | |
CURLOPT_POST => true, | |
CURLOPT_POSTFIELDS => json_encode($parameters), | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_VERBOSE => true | |
); | |
$ch = curl_init('https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings'); | |
curl_setopt_array($ch, $curlOptions); | |
$response = curl_exec($ch); | |
$results = json_decode($response,true);//получаем ответ от сервера с массивом данных | |
curl_close($ch); | |
$file_public_link = $results['url'];//публичная ссылка на загруженный файл |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment