繁体   English   中英

Dropbox:制作直接下载链接[PHP首选]

[英]Dropbox: Produce a direct download link [PHP preferred]

我正在使用Dropbox REST API,我可以成功检索文件的共享URL。

https://www.dropbox.com/developers/reference/api#shares

但是,共享链接将用户带到dropbox.com上的预览页面,而我正在寻找用户可以直接下载文件的直接链接。 例如。 右键单击,另存为...

事实证明,返回的默认共享网址是一个短网址 ,短网址将始终指向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

提示用户从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>';

寻找类似解决方案以获得直接链接以下载跳过Dropbox下载窗口的文件的人可以使用API​​版本2中添加的“获取临时链接”端点。

https://www.dropbox.com/developers/documentation/http/documentation#files-get_temporary_link

获取流式传输文件内容的临时链接。 此链接将在四小时后过期,之后您将获得410消失。 内容 - 链接的类型由文件的mime类型自动确定。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM