dstxpei5823 2017-02-12 23:59
浏览 102
已采纳

使用PHP通过cURL提交gitlab失败

I am trying to do a commit to a gitlab repository via the gitlab API and PHP. Although the payload looks fine to me, the cURL request fails and returns

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

My code looks like the following:

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

As far as I can see, my payload is formatted like in the example, but maybe I am missing something.

  • 写回答

1条回答 默认 最新

  • dsjk3214 2017-02-14 21:31
    关注

    I found the solution to this.

    actions does not simply expect a single array but an array of arrays which then contain the single actions that shall be executed to the repository.

    I furthermore found out, that the id parameter is optional, as I have already specified the id in the CURLOPT_URL parameter.

    So for updating a single file in the specified repository, this code works:

    $info = array(
       'branch_name' => 'master',
       'commit_message' => 'updating content...',
       'actions' => array(
          array(
             'action' => 'update', 
             'file_path' => 'filename',
             'content' => 'test 1234'
          )
       )
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备