原来返回的默认分享url是一个短url,短url会一直指向Dropbox预览页面。
因此,您需要通过将 short_url 参数设置为 false 来让 REST API 返回完整的 url。获得完整的 url 后,在 url 末尾添加 ?dl=1。
例如:https://dl.dropbox.com/s/xxxxxxxxxxxxxxxxxx/MyFile.pdf?dl=1
更多信息:
https://www.dropbox.com/help/201/en
Prompt user to save on download from Dropbox
PHP 示例:
本示例借鉴/启发了以下代码示例:
http://www.phpriot.com/articles/download-with-curl-and-php
http://www.humaan.com.au/php-and-the-dropbox-api/
/* These variables need to be defined */
$app_key = 'xxxxxxxx';
$app_secret = 'xxxxxxxxxxxxxxxxxxxx';
$user_oauth_access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$user_oauth_access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$ch = curl_init();
$headers = array( 'Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT"' );
$params = array('short_url' => 'false', 'oauth_consumer_key' => $app_key, 'oauth_token' => $user_oauth_access_token, 'oauth_signature' => $app_secret.'&'.$user_oauth_access_token_secret);
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $params);
curl_setopt( $ch, CURLOPT_URL, 'https://api.dropbox.com/1/shares/'.$dir );
/*
* To handle Dropbox's requirement for https requests, follow this:
* http://artur.ejsmont.org/blog/content/how-to-properly-secure-remote-api-calls-from-php-application
*/
curl_setopt( $ch, CURLOPT_CAINFO,getcwd() . "\dropboxphp\cacert.pem");
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
$api_response = curl_exec($ch);
if(curl_exec($ch) === false) {
echo 'Curl error: ' . curl_error($ch);
}
$json_response = json_decode($api_response, true);
/* Finally end with the download link */
$download_url = $json_response['url'].'?dl=1';
echo '<a href="'.$download_url.'">Download me</a>';