We’re Still Here to Help (Even Over the Holidays!) - find out more here.
Forum Discussion
bryancart32
9 years agoExplorer | Level 3
Help downloading a non-image file using the API
I am trying to use the v2 API to to get a temporary link to a file. JPG files work fine, but I have some .dcm files. These are medical image DICOM format files. I need to be able to create a tempo...
bryancart32
9 years agoExplorer | Level 3
I used PHP and fopen. This is my function:
function do_post_request($url, $data, $optional_headers = null) {
$params = array('http' => array('method' => 'POST','content' => $data));
if ($optional_headers !== null) { $params['http']['header'] = $optional_headers; }
echo "<pre>";print_r($params);echo "</pre>";
$ctx = stream_context_create($params);
try {
$fp = @fopen($url, 'rb', false, $ctx);
} catch(Exception $ex) {
echo '1 Error: ' .$ex->getMessage();
}
//if (!$fp) { throw new Exception("Problem with $url, $php_errormsg"); }
if (!$fp) { echo "2 Error: ".$php_errormsg; }
$response = @stream_get_contents($fp);
echo "<pre>";print_r($response);echo "</pre>";
if ($response === false) { $response = "Invalid URL: ".$url;/*echo "Problem reading data from $url.";*/ }
return $response;
}
THIS ONE WORKED DOWNLOADING A JPG FILE:
Array
(
[http] => Array
(
[method] => POST
[content] => {"path":"/DICOM/13c76bf2914c6d87786d555f0994c531/1.jpg"}
[header] => Content-Type: application/json
Authorization: Bearer [TOKEN-HERE]
)
)
RETURNED:
{
"metadata":{
"name":"1.jpg",
"path_lower":"/dicom/13c76bf2914c6d87786d555f0994c531/1.jpg",
"path_display":"/DICOM/13c76bf2914c6d87786d555f0994c531/1.jpg",
"id":"id:PnjeM0FAI_AAAAAAAAAJWQ",
"client_modified":"2017-07-07T14:26:41Z",
"server_modified":"2017-07-07T14:26:41Z",
"rev":"13004a8335bf",
"size":982513,
"content_hash":"3a3179223b1b08759a3b5d222d1a7d226b16557b3f98b75d88874bce27d74bb2"
},
"link":"https://dl.dropboxusercontent.com/apitl/1/AACiQGW0Md8EYbPrqqNkdPsl ..."
}
THIS ONE FAILED DOWNLOADING A DCM FILE:
Array
(
[http] => Array
(
[method] => POST
[content] => {"path":"/DICOM/13c76bf2914c6d87786d555f0994c531/1.dcm"}
[header] => Content-Type: application/json
Authorization: Bearer [TOKEN-HERE]
)
)
I do not know how to get any more details of why the response failed.
Any other ideas?
Greg-DB
Dropbox Community Moderator
9 years agoThanks! That's helpful. So, the "Invalid URL" error isn't coming from the API itself, but rather just from this line of code:
if ($response === false) { $response = "Invalid URL: ".$url;/*echo "Problem reading data from $url.";*/ }
That will occur if `$response` is false, and `$response` will be false based on `@stream_get_contents($fp)`. The `$fp` comes from `fopen`, which will return false if an error occurs.
So, if something went wrong with the API call itself, `fopen` would return false, which wouldn't be a valid `resource` as `stream_get_contents` expects, so `stream_get_contents` would then return false, and trigger your `$response === false` line.
According to the fopen documentation: "If the open fails, an error of level E_WARNING is generated.". I can't offer much general PHP advice, but it looks like there is some information in this StackOverflow post about getting the error information, if that's where the issue is occuring.
It looks like you already have some other `echo` statements in the case where things failed though. Are you getting any output from those?
Alternatively, you can use curl in PHP instead, like shown in this example.
- bryancart329 years agoExplorer | Level 3
I finally was able to find that the error I am getting back from DropBox is this:
fopen(https://api.dropboxapi.com/2/files/get_temporary_link): failed to open stream: HTTP request failed! HTTP/1.1 409 Conflict
I did some searching and I was not able to see why this is happening. Any ideas?
Thanks!
Bryan
- Greg-DB9 years ago
Dropbox Community Moderator
Thanks! That "HTTP/1.1 409 Conflict" is part of the API response, but it's only the status line. Can you print out the rest? The response body should contain a specific error.- bryancart329 years agoExplorer | Level 3
409 - Conflict
{"error_summary": "path/not_found/..", "error": {".tag": "path", "path": {".tag": "not_found"}}}When I look in the folder: /DICOM/13c76bf2914c6d87786d555f0994c531
I have 4 files: 1.jpg, 1.dcm, 2.jpg, 2.dcm
The exact same code works for 1.jpg and 2.jpg, but fails for 1.dcm and 2.dcm.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!