【问题标题】:How to create and update a file in a Github repository with PHP and Github API?如何使用 PHP 和 Github API 在 Github 存储库中创建和更新文件?
【发布时间】:2023-03-10 08:35:01
【问题描述】:

我正在尝试通过 Github API 使用 PHP 构建 PHP 函数以在 Github 存储库中创建和更新文件。

PHP 文件是从标准共享主机帐户运行的。因此,使用依赖于安装 Composer 或其他库的框架对我来说不是一个选择。例如,这个https://github.com/KnpLabs/php-github-api 不是一个选项。

到目前为止

我已经设法使用 PHP 访问和列出文件,使用此处提供的信息:

Github API List all repositories and repo's content

我使用了第 2 和第 6 答案的组合(第 2 以 这里是一个很好的方式,只是使用 curl。 由肉食者和第 6 答案与 当你说“显示回购及其内容” Ivan Zuzak)。

两者都清楚地列出了列出文件和获取内容所需的代码和步骤(感谢@flesheater 和@Ivan Zuzak)

我为此搜索了帮助或解决方案。到目前为止,我还没有找到任何使用 PHP 的示例,无论是否正常工作。

Git API 文档

Git 数据 API 信息 (https://developer.github.com/v3/git/) 表示:

举个例子,如果你想对你的 存储库,你会:

get the current commit object
retrieve the tree it points to
retrieve the content of the blob object that tree has for that particular file path
change the content somehow and post a new blob object with that new content, getting a blob SHA back
post a new tree object with that file path pointer replaced with your new blob SHA getting a tree SHA back
create a new commit object with the current commit SHA as the parent and the new tree SHA, getting a commit SHA back
update the reference of your branch to point to the new commit SHA

但是没有代码示例说明这是如何完成的,我不确定“提交”是否是我需要的(如果我理解正确,这不是创建文件,只是创建内容)。

Git 存储库 API 信息 (https://developer.github.com/v3/repos/contents/#create-a-file):

“创建文件”部分显示了执行此操作的方法,但没有代码示例。我知道如何或是否可以在语法上使用它。

相似主题

在我看来,由于问题或答案,没有人提供足够的清晰度或信息来提供解决方案。因此我不认为这是重复的。

How to create a commit and push into repo with GitHub API v3?

这个帖子有两个答案。一个只是 Github API 的链接,没有给出代码示例。另一个是 Pearl 解决方案(看起来),但使用的是单独的 Pearlframework。因此,即使尝试转换显示的 Pearl 也是不切实际的。

How to create github Repository and upload files from PHP?

答案只重复另一个线程,即应该创建一个 blob。但这只是通过 Github API 实际创建/更新文件所需的一切的开始。该线程有一个 Python 示例,但该示例需要一个单独的 Pearl 框架。

GitHub API - write to repo

这是链接到的线程。用户回答了一个不太清楚的问题,但没有任何细节或清晰度。

【问题讨论】:

    标签: php github github-api


    【解决方案1】:

    看起来研究得到了回报,我可以回答我自己问题的主要部分(我没想到能够做到) - 创建一个新文件。看起来可以使用相同的方法来更新现有文件(如果没有,我会回来的)。

    此链接有帮助:http://www.lornajane.net/posts/2009/putting-data-fields-with-php-curl

    我意识到 Github API 文档 (https://developer.github.com/v3/repos/contents/#get-contents) 中提到的路径 - 创建文件部分 - 可以用作 curl 的 url:

    https://api.github.com/repos/ USER / REPO_NAME /contents/ FILENAME
    

    并且需要进行一些调整以允许发送 JSON。所以我现在有:

    $curl_url = $url
    $curl_token_auth = 'Authorization: token ' . $token;
    $ch = curl_init($curl_url);
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'User-Agent: $username', $curl_token_auth ));
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
    
    $response = curl_exec($ch);  
    curl_close($ch);
    $response = json_decode($response);
    
    return $response;
    

    对于 $data,我使用了 Github API 页面中的 JSON 示例:

    {
      "message": "my commit message",
      "committer": {
        "name": "Mike A",
        "email": "someemail@gmail.com"
      },
      "content": "bXkgbmV3IGZpbGUgY29udGVudHM="
    }
    

    在 PHP 文件(在共享主机上)中实现此功能后,没有使用其他框架,我在 Github 存储库中看到了新文件。

    【讨论】:

      【解决方案2】:

      也许它会对某人有所帮助(除了更新或删除文件之外,还具有 SHA 功能):

      <?php
      $file_git = "wall.jpg";
      $data_git = array(
      'sha'=>file_get_contents("sha.txt"),
      'message'=>'image',
      'content'=> base64_encode($file_git),
      'committer'=> array(
      'name'=>'Jacob',
      'email' => '45331093+greenandgreen@users.noreply.github.com'
      )
      );
      $data_string_git = json_encode($data_git);
      $ch_git = curl_init('https://api.github.com/repos/YOUR_REPO/contents/wall.jpg');
      curl_setopt($ch_git, CURLOPT_CUSTOMREQUEST, "PUT");
      curl_setopt($ch_git, CURLOPT_POSTFIELDS, $data_string_git);
      curl_setopt($ch_git, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch_git, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json',
      'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 YaBrowser/19.9.3.314 Yowser/2.5 Safari/537.36',
      'Authorization: token PLACE_YOUR_PERSONAL_TOKEN_HERE'
      ));
      $result_git = curl_exec($ch_git);
      echo $result_git;
      $p_git = json_decode($result_git);
      file_put_contents("sha.txt",$p_git->content->sha);
      ?>
      

      在 PHP 7 上测试。 如果代码有效,那么我不会拒绝点赞,感谢您的小工作:)

      【讨论】:

      【解决方案3】:
      <?php
        function pushFile($username,$token,$repo,$branch,$path,$b64data){
          $message = "Automated update";
          $ch = curl_init("https://api.github.com/repos/$repo/branches/$branch");
          curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent:Php/Automated'));
          curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $token);
          curl_setopt($ch, CURLOPT_TIMEOUT, 30);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
          $data = curl_exec($ch);
          curl_close($ch);
          $data=json_decode($data,1);
      
          $ch2 = curl_init($data['commit']['commit']['tree']['url']);
          curl_setopt($ch2, CURLOPT_HTTPHEADER, array('User-Agent:Php/Ayan Dhara'));
          curl_setopt($ch2, CURLOPT_USERPWD, $username . ":" . $token);
          curl_setopt($ch2, CURLOPT_TIMEOUT, 30);
          curl_setopt($ch2, CURLOPT_RETURNTRANSFER, TRUE);
          $data2 = curl_exec($ch2);
          curl_close($ch2);
          $data2=json_decode($data2,1);
      
          $sha='';
          foreach($data2["tree"] as $file)
            if($file["path"]==$path)
              $sha=$file["sha"];
          
          $inputdata =[];
          $inputdata["path"]=$path;
          $inputdata["branch"]=$branch;
          $inputdata["message"]=$message;
          $inputdata["content"]=$b64data;
          $inputdata["sha"]=$sha;
      
          echo json_encode($inputdata);
      
          $updateUrl="https://api.github.com/repos/$repo/contents/$path";
          echo $updateUrl;
          $ch3 = curl_init($updateUrl);
          curl_setopt($ch3, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', 'User-Agent:Php/Ayan Dhara'));
          curl_setopt($ch3, CURLOPT_USERPWD, $username . ":" . $token);
          curl_setopt($ch3, CURLOPT_TIMEOUT, 30);
          curl_setopt($ch3, CURLOPT_CUSTOMREQUEST, "PUT");
          curl_setopt($ch3, CURLOPT_RETURNTRANSFER, TRUE);
          curl_setopt($ch3, CURLOPT_POSTFIELDS, json_encode($inputdata));
          $data3 = curl_exec($ch3);
          curl_close($ch3);
      
          echo $data3;
        }
        //pushFile("your_username","your_personal_token","username/repository","repository_branch","path_of_targetfile_in_repository","base64_encoded_data");
      ?>
      

      【讨论】:

      • 请务必添加一些解释!
      • 这里还有什么要描述的?
      • 问题中解释的一切我发现它对我有用,所以发布它
      猜你喜欢
      • 2012-04-01
      • 2015-04-07
      • 2017-12-08
      • 2023-04-02
      • 2014-06-02
      • 2022-07-21
      • 2021-05-13
      • 2016-04-17
      • 1970-01-01
      相关资源
      最近更新 更多