php - 使用 Dropbox 的 v2 API 将文件上传到 Dropbox
问题描述
我想使用 PHP 将上传的文件保存在 Dropbox 中,并为此使用 Dropbox 的 v2 API。
我没有得到任何回应。
下面是我的代码。
<?php
$filename = 'qw.txt';
$api_url = 'https://content.dropboxapi.com/2/files/upload'; //dropbox api url
$token = '<REDACTED>'; // oauth token
$headers = array('Authorization: Bearer '. $token,
'Content-Type: application/octet-stream',
'Dropbox-API-Arg: '.
json_encode(
array(
"path"=> '/'. basename($filename),
"mode" => "add",
"autorename" => true,
"mute" => false
)
)
);
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
$path = $filename;
$fp = fopen($path, 'rb');
$filesize = filesize($path);
curl_setopt($ch, CURLOPT_POSTFIELDS, fread($fp, $filesize));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_VERBOSE, 1); // debug
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo($response.'<br/>');
echo($http_code.'<br/>');
curl_close($ch);
我得到以下回应。
解决方案
从 Dropbox 应用程序获取访问令牌后,运行以下代码,您的文件将上传到 Dropbox
<?php
$filename = 'pr.jpg';
$api_url = 'https://content.dropboxapi.com/2/files/upload'; //dropbox api url
$token = 'Generated access token'; // oauth token
$headers = array('Authorization: Bearer '. $token,
'Content-Type: application/octet-stream',
'Dropbox-API-Arg: '.
json_encode(
array(
"path"=> '/'. basename($filename),
"mode" => "add",
"autorename" => true,
"mute" => false
)
)
);
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
$path = $filename;
$fp = fopen($path, 'rb');
$filesize = filesize($path);
curl_setopt($ch, CURLOPT_POSTFIELDS, fread($fp, $filesize));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1); // debug
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo($response.'<br/>');
echo($http_code.'<br/>');
curl_close($ch);
推荐阅读
- javascript - 如何按值按降序对文件大小字符串列表进行排序
- html - 如何让 Flex Child 正确锚定
- angular - Angular html到pdf问题
- redux - redux-toolkit 在 slice reducer 之间共享状态
- javascript - 在 React 功能组件中调用异步方法
- netapp - 如何在我的 netapp 集群上设置故障转移?
- git - 如果我和我的朋友正在处理同一目录中的不同文件,将会发生 git 冲突
- python - Python:如何将字典列表转换为 geopandas 数据框?
- r - 从r中的多个文件夹加载文件并处理
- javascript - 根据 switch case 更新字符串