Dropbox:产生直接下载链接[首选PHP] [英] Dropbox: Produce a direct download link [PHP preferred]

查看:260
本文介绍了Dropbox:产生直接下载链接[首选PHP]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the Dropbox REST API and I can successfully retrieve a share url for a file.

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

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

However, the share link takes the user to a preview page on dropbox.com, whereas I'm looking for a direct link that a user could directly download a file. eg. Right click, Save as...

推荐答案

事实证明,返回的默认共享URL是短url ,并且短url将始终指向Dropbox预览页面.

It turns out that the default share url that is returned is a short url and the short url will always point to the Dropbox preview page.

因此,您需要通过将short_url参数设置为false来获取REST API以返回完整的url.拥有完整的网址后,请在网址末尾添加?dl = 1.

Therefore, you need to get the REST API to return the full url by setting the short_url parameter to false. Once you have the full url, then add ?dl=1 at the end of url.

例如: https://dl.dropbox.com/s /xxxxxxxxxxxxxxxxxx/MyFile.pdf?dl=1

更多信息:

https://www.dropbox.com/help/201/zh-CN

提示用户保存从Dropbox下载的内容

PHP示例:

此示例从以下示例借用/启发了以下代码示例: http://www.phpriot.com/articles/download-with- curl-and-php

This example has borrowed/inspired-by code samples from these: 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:产生直接下载链接[首选PHP]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆