Hello,
I want upload a file whit API PHP. But i want get the public link share to my users download the file from their home.
Is posible?
Dropbox does offer an API that enables programmatic listing, uploading, downloading, and sharing of files, so this is possible. You can find everything you need to get started with the Dropbox API, including documentation, tutorials, and SDKs here. You'll need to register an app here.
App authorization is processed using OAuth. You can find more information about using OAuth in our OAuth guide.
We don't currently have an official PHP SDK for Dropbox API v2, so if you're using PHP, you can call the API v2 endpoints directly.
To upload a file, you can use the /files/upload endpoint. To get a shared link, you can use the /sharing/create_shared_link_with_settings endpoint.
Or you can use the official PHP SDK for Dropbox API v1:
https://www.dropbox.com/developers-v1/core/sdks/phphttps://www.dropbox.com/developers-v1/core/start/phphttps://dropbox.github.io/dropbox-sdk-php/api-docs/v1.1.x/
In that SDK, you can upload files using the getFile method, and share them using the createShareableLink method.
This is the code I have for PHP v2 for uploading a file, using curl functions, but I'm at a loss as to how to describe what the POST data should look like.
<?php$ch = curl_init();curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_POST, true);$token = "yourtoken";$host = "https://api.dropboxapi.com/";//set headers$headers = array('Authorization: Bearer '.$token, 'Content-Type: application/octet-stream', 'Dropbox-API-Arg: {"path":"/test.txt","mode":"add"}',);
$f = fopen("test.txt", "rb");
curl_setopt($ch, CURLOPT_URL, $host);curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);// What should the POST data look like? Using the file handle? That doesn't work.curl_setopt($ch, CURLOPT_POSTFIELDS, $f);$response = curl_exec($ch);fclose($f);echo "<br />File Upload Response: <br />";var_export($response);echo "<br />File UPload HTTP CODE: <br />";var_export(curl_getinfo($ch, CURLINFO_HTTP_CODE));curl_close($ch);
Your URL is wrong... it should be https://content.dropboxapi.com/2/files/upload.
There may be other ways, but one way to post the content of a local file would be to read it into memory first. E.g., this worked for me:
curl_setopt($ch, CURLOPT_POSTFIELDS, fread($f, filesize("test.txt")));
Yes, it is possible. Please follow the code below:
$token = 'YOUR-TOKEN'; // oauth token $path = 'YOUR-FILE-HERE'; // Ex. text.txt or demo.mp4 /** * Upload file to dropbox */ $fp = fopen($path, 'rb'); $size = filesize($path); $ext = end(explode('.', $path)); $d_path = uniqid().'.'.$ext; $cheaders = array('Authorization: Bearer '.$token, 'Content-Type: application/octet-stream', 'Dropbox-API-Arg: {"path":"/'.$d_path.'", "mode":"add"}'); $ch = curl_init('https://content.dropboxapi.com/2/files/upload'); curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders); curl_setopt($ch, CURLOPT_PUT, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_INFILE, $fp); curl_setopt($ch, CURLOPT_INFILESIZE, $size); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); // echo $response; curl_close($ch); fclose($fp); /** * Get shared link */ $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_URL => 'https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings', CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => '{ "path": "/' . $d_path . '", "settings": { "access": "viewer", "allow_download": true, "audience": "public", "requested_visibility": "public" } }', CURLOPT_HTTPHEADER => [ 'Content: application/json', 'Authorization: Bearer ' . $token, 'Content-Type: application/json' ] ]); $response = curl_exec($curl); $data = json_decode($response, true); echo $url = $data['url']; curl_close($curl);