尝试合并时Bitbucket API错误请求

时间:2015-03-04 06:09:33

标签: php git curl bitbucket bitbucket-api

这是我的代码:

$url = "https://bitbucket.org/api/2.0/repositories/***/***/pullrequests/35/merge";

$curl1 = curl_init();   

curl_setopt($curl1, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ); 
curl_setopt($curl1, CURLOPT_USERPWD, "***:***");
curl_setopt($curl1, CURLOPT_HEADER, true); 
curl_setopt($curl1, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl1, CURLOPT_URL, $url);
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl1, CURLOPT_POST, true);

echo curl_exec($curl1);

这是回应:

HTTP/1.1 400 BAD REQUEST Server: nginx/1.5.10 Date: Wed, 04 Mar 2015 06:03:15 GMT Content-Type: text/plain Content-Length: 11 Connection: keep-alive X-Served-By: app19 X-Render-Time: 0.0410010814667 Content-Language: de X-Static-Version: 572a80470390 Vary: Authorization, Accept-Language, Cookie X-Version: 1d224fb664b6 ETag: "825644f747baab2c00e420dbbc39e4b3" X-Request-Count: 27 X-Frame-Options: SAMEORIGIN Bad Request

为什么这不起作用? (出于安全原因,我用***替换了一些信息)

1 个答案:

答案 0 :(得分:1)

您缺少应包含在请求正文中的特定端点的强制参数。

根据API,这些必填参数为ownerrepo_slugpull_request_id

$request_body = array(
  'owner'           => 'account-name',
  'repo_slug'       => 'repo-name',
  'pull_request_id' => 35
);

由于您已将application/json指定为内容类型,因此您需要json_encode上方的数组:

curl_setopt($curl1, CURLOPT_POSTFIELDS, json_encode($request_body));

作为旁注,您可以使用bitbucket-api库,它可以帮助您在more easy way中与Bitbucket API进行互动。

使用该库接受拉取请求,如下所示:

$pull = new Bitbucket\API\Repositories\PullRequests();

// set your login credentials here
$pull->getClient()->addListener(
  new \Bitbucket\API\Http\Listener\BasicAuthListener('username', 'password')
);
$pull->accept($account_name, $repo_slug, 35);

您可以在docs中阅读更多内容。

免责声明:我是bitbucket-api库的作者。