我尝试使用curl库通过PHP脚本将数据库上传到存储库的bitbucket下载部分。通常情况下,我会转到我的phpmyadmin并将数据库导出到一个文件夹,然后转到存储库下载部分下的bitbucket帐户并手动上传。我需要一个能自动执行这些任务的脚本。
我试着使用这样的curl库:
// bitbucket username and password
define('USERNAME', 'my_username');
define('PASSWORD', 'my_password');
$url = 'https://bbuseruploads.s3.amazonaws.com/';
//This needs to be the full path to the file you want to send.
$file_name_with_full_path = realpath('apache_pb2.gif');
$post = array('extra_info' => '123456', 'file_contents' => '@' . $file_name_with_full_path); // image file example here
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, USERNAME . ":" . PASSWORD);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$resp = curl_exec($ch);
// validate CURL status
if (curl_errno($ch))
throw new Exception(curl_error($ch), 500);
结果:
<error><code>InvalidArgument</code>
<message>Unsupported Authorization Type</message>
<argumentvalue>Basic hash_code_here</argumentvalue>
<argumentname>Authorization</argumentname>
<requestid>hash_code_here</requestid>
<hostid>hash_code_here</hostid>
</error>
如果你需要进一步澄清,请告诉我。
只是为了防止有人在这里发现这具旧尸体:
我通过这个放克函数解决了这个问题:
/**
* Uploads a file to Bitbucket Download area of the configured repository
* Does the same as thi bash command:
* curl -X POST "https://USER:PW@api.bitbucket.org/2.0/repositories/owner/slug/downloads/" --form files=@"test.txt"
* @param string $filename
* @param string $apiEndpoint
* @param string $apiUser
* @param string $apiPassword
* @return bool|string
* @throws Deployer'Exception'Exception
*/
public static function sendFileToDownload(
string $filename,
string $apiEndpoint,
string $apiUser,
string $apiPassword
) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $apiEndpoint);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLAUTH_BASIC, 1);
curl_setopt($curl, CURLOPT_USERPWD, "$apiUser:$apiPassword");
$data = [
'files' => curl_file_create($filename),
];
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$erg = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($erg === false || $status > 300) {
throw new Deployer'Exception'Exception("Result: ".print_r($erg, true), $status);
}
curl_close($curl);
}
}
对我来说,最重要的一点是这一部分:
$data = [
'files' => curl_file_create($filename),
];
这导致一个本地文件被上传到我的转发下载区,名称为$filename(文件名路径)。
今天也遇到了同样的问题"不支持的授权类型";API CURL请求的响应。
我花了很长时间在谷歌咨询。最后,我发现我的URL中有两个斜杠(不小心)。看,我连接到的API有一个URL,然后是一个API端点。如下所示:
https://www.example.com/api/v2/foo-或-
https://www.example.com/api/v2/bar
但我不小心把它们组合成这样:curl_setopt($curl, CURLOPT_URL, $url ."/". $endpoint);
给定我的数据:$url = "https://www.example.com/api/v2/"
$endpoint = "foo"
我的结局是:"https://www.example.com/api/v2//foo"
通过移除."/".
并使其成为.
来解决问题
额外的可能性:我在这个过程中了解到的另一个有趣的事情是,API用户更喜欢在发布到他们的json数据周围使用双引号。我还没有验证它有什么不同,但我没有用单引号包装双引号(反之亦然),而是用转义内部双引号的方式留下东西。它正在发挥作用。