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'
          )
       )
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态