通过PHP脚本将文件上载到Bitbucket

通过PHP脚本将文件上载到Bitbucket,php,file-upload,curl,bitbucket,Php,File Upload,Curl,Bitbucket,我尝试通过使用curl库的PHP脚本将数据库上传到存储库的bitbucket下载部分。通常我会转到我的phpmyadmin并将数据库导出到一个文件夹,然后转到我的存储库下载部分下的bitbucket帐户并手动上载。我需要一个自动化这些任务的脚本 我试着像这样使用curl库: // bitbucket username and password define('USERNAME', 'my_username'); define('PASSWORD', 'my_password'); $url =

我尝试通过使用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(filename path)的本地文件被上传到我的repos下载区。

今天也有同样的问题。来自API CURL请求的“不支持的授权类型”响应

我花了很长时间咨询谷歌。最后,我发现我的URL中同时有两个斜杠(不小心)。看,我连接到的API有一个URL,然后是一个API端点。像这样:
-或-

但我不小心把它们组合成了这样:
curl\u setopt($curl,CURLOPT\u URL,$URL./“$endpoint)
根据我的数据:
$url=”https://www.example.com/api/v2/“

$endpoint=“foo”

我的结论是:
”https://www.example.com/api/v2//foo“

通过删除
/“
并将其设置为:


额外的可能性:在这个过程中,我还学到了一些有趣的事情,API人员更喜欢在发布给他们的json数据周围加上双引号。我还没有证实这有什么不同,但是我没有用单引号括起双引号(或者用单引号括起双引号),而是将内部双引号转义。它正在工作。

您有解决方案吗?没有,但现在我使用管道