使用 PHP 通过 cURL 提交 gitlab 失败

gitlab commit via cURL with PHP fails

我正在尝试通过 gitlab API 和 PHP 提交到 gitlab 存储库。虽然有效负载对我来说看起来不错,但 cURL 请求失败并且 returns

[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>';

据我所知,我的有效载荷的格式与 the example 类似,但也许我遗漏了一些东西。

我找到了解决方法。

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'
      )
   )
);