1

我正在尝试通过 gitlab API 和 PHP 提交到 gitlab 存储库。尽管负载对我来说看起来不错,但 cURL 请求失败并返回

[error] => branch_name is missing, commit_message is missing, actions is missing

我的代码如下所示:

$ch_git = curl_init(); 

curl_setopt($ch_git, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch_git, CURLOPT_HTTPHEADER, array('PRIVATE-TOKEN: xxx')); 
curl_setopt($ch_git, CURLOPT_URL, 'https://gitlab.com/api/v3/projects/xxx/repository/commits'); 
curl_setopt($ch_git, CURLOPT_POST, true);

$info = array(
    'id' => 'xxx',
    'branch_name' => 'master',
    'commit_message' => 'updating content...',
    'actions' => array(
        'action' => 'update', 
        'file_path' => 'filename',
        'content' => 'test 1234'
    )
);

$payload = json_encode($info, JSON_PRETTY_PRINT);

echo '<pre>';
echo $payload;
echo '</pre>';

curl_setopt($ch_git, CURLOPT_POSTFIELDS, $payload);

$res = curl_exec($ch_git);

echo '<pre>';
print_r(json_decode($res));
echo '</pre>';

据我所见,我的有效载荷的格式与示例中的一样,但也许我遗漏了一些东西。

4

1 回答 1

0

我找到了解决方案。

actions不只是期望一个数组,而是一个数组数组,其中包含应执行到存储库的单个操作。

我还发现,该id参数是可选的,因为我已经在CURLOPT_URL参数中指定了 id。

因此,对于更新指定存储库中的单个文件,此代码有效:

$info = array(
   'branch_name' => 'master',
   'commit_message' => 'updating content...',
   'actions' => array(
      array(
         'action' => 'update', 
         'file_path' => 'filename',
         'content' => 'test 1234'
      )
   )
);
于 2017-02-14T21:31:48.477 回答