Search code examples
phpdropbox

Dropbox CORS cannot download file


I'm following this guide https://dropbox.github.io/dropbox-api-v2-explorer/#files_download_zip

I've problem when using api dropbox and want to get download files. first is about cors problem.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://content.dropboxapi.com/2/files/download_zip');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = 'Authorization: Bearer myTokenAcess';
$headers[] = 'Dropbox-Api-Arg: {\"path\":\"/myfolder/mysubfolder/mysubfolderagain\"}';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

and the results is

Error in call to API function "files/download": Bad HTTP "Content-Type" header: "application/x-www-form-urlencoded". Expecting one of "text/plain", "text/plain; charset=utf-8", "application/octet-stream", "application/octet-stream; charset=utf-8".

then I'm trying to add this line on header

$headers[] = 'Content-Type: application/octet-stream';

the results be

Error in call to API function "files/download": HTTP header "Dropbox-API-Arg": could not decode input as JSON

and then I'm trying to change drobox-api-arg line to

$headers[] = 'Dropbox-Api-Arg: json_decode({\"path\":\"/myfolder/mysubfolder/mysubfolderagain\"},TRUE)';

and the results still same like above

Error in call to API function "files/download": HTTP header "Dropbox-API-Arg": could not decode input as JSON

can someone help me to fix this?

Update : @sideshowbarker results using this

$headers[] = 'Dropbox-Api-Arg: {"path":"/myfolder/mysubfolder/mysubfolderagain"}'

enter image description here


Solution

  • I've found the answer based on @sideshowbarker say. I just need to conver bytes into zip so i just add this line at end line and now this working fine.

    foreach($result as $byte)
    {
        $byteString .= $byte;
    }
    header("Content-type: application/zip"); 
    header("Content-Disposition: inline; filename=tesFolderQ.zip");
    echo $byteString;