Dropbox:生成直接下载链接[PHP首选]

Dropbox:生成直接下载链接[PHP首选],php,dropbox,dropbox-api,Php,Dropbox,Dropbox Api,我正在使用Dropbox REST API,可以成功地检索文件的共享url 然而,共享链接将用户带到dropbox.com上的预览页面,而我正在寻找一个用户可以直接下载文件的直接链接。右键单击,另存为…返回的默认共享url是一个短url,短url将始终指向Dropbox预览页面 因此,您需要通过将short_url参数设置为false,让restapi返回完整的url。获得完整url后,在url末尾添加?dl=1 例如: 更多信息: PHP示例: 本示例借鉴了以下代码示例: /*需要定

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


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

返回的默认共享url是一个短url,短url将始终指向Dropbox预览页面

因此,您需要通过将short_url参数设置为false,让restapi返回完整的url。获得完整url后,在url末尾添加?dl=1

例如:

更多信息

PHP示例

本示例借鉴了以下代码示例:

/*需要定义这些变量*/
$app_key='xxxxxxxx';
$app_secret='xxxxxxxxxxxxxxxxxxxxx';
$user_oauth_access_token='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$user_oauth_access_token_secret='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$ch=curl_init();
$headers=数组('Authorization:OAuth OAuth_version=“1.0”,OAuth_signature_method=“PLAINTEXT”);
$params=array('short\u url'=>'false','oauth\u consumer\u key'=>$app\u key',oauth\u token'=>$user\u oauth\u access\u token','oauth\u signature'=>$app\u secret.&.$user\u oauth\u access\u token\u 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);
/*
*要处理Dropbox对https请求的要求,请执行以下操作:
* 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\u response=curl\u exec($ch);
if(curl_exec($ch)==false){
回显“旋度误差:”。旋度误差($ch);
}
$json\u response=json\u decode($api\u response,true);
/*最后以下载链接结束*/
$download_url=$json_response['url']。?dl=1';
回声';

只需在链接末尾添加
?dl=1

发件人:

致:

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

获取指向文件流内容的临时链接。此链接将 四小时后过期,之后你将失去410。 链接的内容类型由文件的 mime类型


见我的回复:截至2017年8月,这似乎不再有效。我的浏览器中出现了“我们将尝试强制您下载我们的应用程序,并创建一个帐户”屏幕
/* 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>';