Pau*_*aul 5 php dropbox dropbox-api
我正在使用Dropbox REST API,我可以成功检索文件的共享URL.
https://www.dropbox.com/developers/reference/api#shares
但是,共享链接将用户带到dropbox.com上的预览页面,而我正在寻找用户可以直接下载文件的直接链接.例如.右键单击,另存为...
Pau*_*aul 13
事实证明,返回的默认共享网址是一个短网址,短网址将始终指向Dropbox预览页面.
因此,您需要通过将short_url参数设置为false来使REST API返回完整的URL.获得完整网址后,在网址末尾添加?dl = 1.
例如:https://dl.dropbox.com/s/xxxxxxxxxxxxxxxxxx/MyFile.pdf? dl = 1
更多信息:
https://www.dropbox.com/help/201/en
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>';
Run Code Online (Sandbox Code Playgroud)