Dropbox API File Listing using PHP and cURL extension


Example

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api.dropboxapi.com/2/files/list_folder");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CAINFO, "cacert.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"path\":\"/<FOLDER PATH>\"}");
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = "Authorization: Bearer <ACCESS_TOKEN>";
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

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


$json = json_decode($result, true);
foreach ($json['entries'] as $data) {
    echo 'File Name: ' . $data['name'];
}

?>

Note that the response may contain has_more=true, in which case your app should call back to /files/list_folder/continue to continue getting more entries.

<ACCESS_TOKEN> should be replaced with the OAuth 2 access token.