如何使用 PHP 和 Github API 在 Github 存储库中创建和更新文件?

Mik*_*e A 5 php github github-api

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

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

迄今为止

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

Github API 列出所有存储库和存储库的内容

我使用了第 2 个和第 6 个答案的组合(第 2 个以Here is a nice way just with the curl.开头,第 6 个答案为When you say "display a repo and its contents" by 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
Run Code Online (Sandbox Code Playgroud)

However there are no code examples of how this is done, and I am not sure if 'commit' is what i need (if I understand correctly, this is not creating a file, only content).

Git Repository API info (https://developer.github.com/v3/repos/contents/#create-a-file):

The part 'Create a File' shows a method for doing this, but without code examples. I am usure how to or if this can be used prgrammatically.

Github API 更新文件

Similar threads

In my opinion none are offering enough clarity or information to provide a solution, due to either the questions or answers. Therefore I don't think this is a duplicate.

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

这个线程有两个答案。一个只是指向 Github API 的链接,没有给出代码示例。另一个是珍珠解决方案(看起来),但使用单独的珍珠框架。所以即使尝试转换显示的珍珠也不切实际。

如何创建 github Repository 并从 PHP 上传文件?

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

GitHub API - 写入存储库

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

Mik*_*e A 10

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

此链接有帮助: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
Run Code Online (Sandbox Code Playgroud)

还需要进行一些调整以允许发送 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;
Run Code Online (Sandbox Code Playgroud)

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

{
  "message": "my commit message",
  "committer": {
    "name": "Mike A",
    "email": "someemail@gmail.com"
  },
  "content": "bXkgbmV3IGZpbGUgY29udGVudHM="
}
Run Code Online (Sandbox Code Playgroud)

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