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

标签 php github github-api

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

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

到目前为止

我已经成功使用 PHP 访问和列出文件,使用此处给出的信息:

Github API List all repositories and repo's content

我使用了第二个和第六个答案的组合(第二个答案以Here is a good way just with the curly.开头,由mesheater和第六个答案当你说“显示一个存储库时及其内容”,作者:Ivan Zuzak)。

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

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

Git API 文档

Git Data API 信息 ( https://developer.github.com/v3/git/ ) 是这样说的:

As an example, if you wanted to commit a change to a file in your repository, you would:

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 ):

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

Github API Update 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

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

最佳答案

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

此链接有帮助: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 存储库中看到了新文件。

关于php - 如何使用 PHP 和 Github API 在 Github 存储库中创建和更新文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36835116/

相关文章:

php - 如何解析YouTube网址以获取视频或播放列表ID?

php - Laravel 5 表单不工作(不插入数据)

github - 如何转义 '#' 使其不会在 GitHub 评论中引用问题?

php - 顺序和选择子句中的 Full Group BY 错误

php - MySQL 查询根据两个不同表中的值选择一个表。

image - SVG 图像标签在 github README 上不起作用

github - 如何在 github markdown 的子模块中添加到文件的相对链接?

amazon-cognito - 将 Github 身份提供商添加到 AWS Cognito

Node.js Github API 连接 ECONNREFUSED 获取用户存储库时出错

python - Http 发布成功,但在 python 中失败