Download File endpoint returns binary data , but outputting with PHP still results in corrupted file

New post

Comments

3 comments

  • LoCortes

    Hello ,

     

    I understand that you are able to download a file but it appears with no extension. Is that the situation? Have you tried to set the extension manually and see if it opens or the document is really corrupted?

     

    I would remove, maybe, the line 

    header('Content-Type: application/octet-stream');

     

    I am no expert on PHP but I would say that you are then forcing the element to be printed to be treated as a generic element and not the particular one you expect.

     

    Regards

    0
    Comment actions Permalink
  • bearinaustin

    Thanks for the reply. Depending on the file type, I have tried a couple of different Content-Type headers like "image/png" etc.

     

    I ended up settling with a solution that actually works okay and uses the Box expiring link embed element. Attaching what I did for example purposes.

    $url = "https://api.box.com/2.0/files/".$file_id."?fields=expiring_embed_link&showDownload=true&access_token=".$token;
    
    $header = '';
    $header = array();
    //////////////////////////////////////////
    // Set Required CURL options
    //////////////////////////////////////////
    $options = array(
    CURLOPT_RETURNTRANSFER => true, // return web page
    CURLOPT_HEADER => false, // don't return headers
    CURLOPT_FOLLOWLOCATION => true, // follow redirects
    CURLOPT_ENCODING => "", // handle compressed
    CURLOPT_USERAGENT => "test", // who am i
    CURLOPT_AUTOREFERER => true, // set referer on redirect
    CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
    CURLOPT_TIMEOUT => 120, // timeout on response
    CURLOPT_MAXREDIRS => 10);
    //////////////////////////////////////////
    // Specify API endpoint
    //////////////////////////////////////////
    $ch = curl_init ($endpoint);
    //////////////////////////////////////////
    // Write options
    //////////////////////////////////////////
    curl_setopt_array ( $ch, $options );
    //////////////////////////////////////////
    // Execute
    //////////////////////////////////////////
    $header['response'] = curl_exec ( $ch );
    $header['errno'] = curl_errno ( $ch );
    $header['errmsg'] = curl_error ( $ch );
    $header['httpcode'] = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
    //////////////////////////////////////////
    // Close connection /unbind
    //////////////////////////////////////////
    curl_close ($ch);
    
    if($header['httpcode'] == 200)
    {
    $file = json_decode($header['response'], true);
    
    header('location: '.$file['expiring_embed_link']['url'].'?showDownload=true');
    }

     

    0
    Comment actions Permalink
  • LoCortes

    Hello ,

     

    when you use the REST API to download the document it returns a URL like this one: "https://dl.boxcloud.com/d/1/uydh28ktI4yTOgpeh7jf5gSVbqxxGVREqEWQgK7ELLubq9Hk7WSUm3M5LJ0fl8CGrtw2IH8PU3YfPU(...)nmo11K3ALKjev7Eg2R86cuq3EjZI1xtC3ZMUOvxpzidOMGXVwh8OlMNb3jfI3D138Nrb3wLgjtlyu9fOIeYVhGhf2nL460paarA../download"

     

    So... if you replace this piece

    '.$file['expiring_embed_link']['url'].'?showDownload=true'

    by the URL 

     

    header('location: https://dl.boxcloud.com/d/1/uydh28ktI4yTOgpeh7jf5gSVbqxxGVREqEWQgK7ELLubq9Hk7WSUm3M5LJ0fl8CGrtw2IH8PU3YfPU(...)nmo11K3ALKjev7Eg2R86cuq3EjZI1xtC3ZMUOvxpzidOMGXVwh8OlMNb3jfI3D138Nrb3wLgjtlyu9fOIeYVhGhf2nL460paarA../download');

     

    does it not work?

     

    Completely guessing here to be sincere 🙂 

     

    Regards

    0
    Comment actions Permalink

Please sign in to leave a comment.