我在尝试使用PHP和Dropbox API上传文件时遇到问题。当cURL执行时,我得到一个页面未找到错误。这表明存在URL问题,但我看不出它是错误的。
该文件存在且所有OAuth都在工作。
错误的屏幕截图:
这是使用的代码 - 我在这里遗漏了什么吗?
$filePathName = "/path_to_file/my_image.png";
$url = "https://api-content.dropbox.com/1/files_put/sandbox/my_folder/my_image.png";
$headers = array("Content-Type: ".mime_content_type($filePathName)."\r\nContent-Length: ".filesize($filePathName)."\r\n".
"Authorization: OAuth oauth_version=\"1.0\", oauth_signature_method=\"PLAINTEXT\", oauth_consumer_key=\"".DROPBOX_APP_KEY."\", oauth_token=\"".DROPBOX_OAUTH_ACCESS_TOKEN."\", oauth_signature=\"".DROPBOX_APP_SECRET."&".DROPBOX_OAUTH_ACCESS_SECRET."\""
);
$fh = fopen($filePathName, "rb");
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_INFILE, $fh);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filePathName));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$apiResponse = curl_exec($ch);
fclose($fh);
die("Response:<br />".$apiResponse);
答案 0 :(得分:0)
另见https://www.dropbox.com/developers/core/api
上传文件的网址结构为https://api-content.dropbox.com/1/files_put/<root>/<path>?param=val
。 root是root的根相对于指定路径的根。有效值为沙箱和保管箱。所以你错过了网址中的根。
答案 1 :(得分:0)
我已设法解决它!
问题在于我构建标题的方式。应该是这样的:
$headers = array(
"'Content-Type: ".mime_content_type($filePathName)."'",
"'Content-Length: ".filesize($filePathName)."'",
"Authorization: OAuth oauth_version=\"1.0\", oauth_signature_method=\"PLAINTEXT\", oauth_consumer_key=\"".DROPBOX_APP_KEY."\", oauth_token=\"".DROPBOX_OAUTH_ACCESS_TOKEN."\", oauth_signature=\"".DROPBOX_APP_SECRET."&".DROPBOX_OAUTH_ACCESS_SECRET."\""
);
Content-Type
和Content-Length
不再以\r\n
结尾,现在被'
包围,并且是数组中的单独元素。
我得到了一个线索,因为我使用curl_getinfo
函数而http_code
为400,这意味着一个糟糕的请求。这让我远离卡在Dropbox返回的“找不到页面”错误上,让我进一步调查标题。