【问题标题】:Dropbox: Produce a direct download link [PHP preferred]Dropbox:生成直接下载链接 [PHP 首选]
【发布时间】:2012-10-29 03:34:15
【问题描述】:

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

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

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

【问题讨论】:

标签: php dropbox dropbox-api


【解决方案1】:

原来返回的默认分享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>';

【讨论】:

    【解决方案2】:

    【讨论】:

    • 截至 2017 年 8 月,这似乎不再有效。我在浏览器中看到“我们将尝试强制您下载我们的应用并创建一个帐户”屏幕
    【解决方案3】:

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

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

    获取文件流内容的临时链接。这个链接将 四个小时后过期,之后您将获得 410 Gone。 链接的内容类型由文件的自动确定 哑剧类型。

    【讨论】:

      猜你喜欢
      • 2018-05-17
      • 1970-01-01
      • 2012-08-05
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      • 2017-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多