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

问题描述:

我试图通过PHP通过Github API构建PHP函数,以在Github存储库中创建和更新文件.

I am trying to build PHP functions to create and update files within a Github repository, using PHP through the Github API.

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

The PHP files are being run from a standard shared hosting account. Therefore using frameworks that depend on installing Composer or other libraries is not an option for me. So for example, this https://github.com/KnpLabs/php-github-api is not an option.

到目前为止

我已经使用PHP并通过此处提供的信息设法访问并列出了文件:

I have managed to access and list files using PHP, using the info given here:

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

我结合使用了第二个答案和第六个答案(第二个以开头,这是一种很好的卷发方式.由flesheater撰写,第六个答案则以当您说显示回购协议及其内容"(伊万·祖扎克(Ivan Zuzak)).

I used a combination of the 2nd and 6th answer (2nd starts with Here is a nice way just with the curl. by flesheater and 6th answer with When you say "display a repo and its contents" by Ivan Zuzak).

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

Both list clearly the code and steps needed to list files and get contents (thank you @flesheater and @Ivan Zuzak)

我为此寻求帮助或解决方案.到目前为止,我还没有找到使用PHP的任何示例,无论工作与否.

I searched for help or solutions for this. So far I have not found any example, working or otherwise, using PHP.

Git API文档

Git数据API信息( https://developer.github.com/v3/git/)这样说:

Git Data API info (https://developer.github.com/v3/git/) says this:

例如,如果您要对您的文件进行更改 存储库,您将:

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

但是,没有代码示例说明执行此操作的方式,并且我不确定'commit'是否是我所需要的(如果我理解正确,这不是在创建文件,而只是在创建内容).

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存储库API信息( https://developer.github .com/v3/repos/contents/#create-a-file ):

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.

类似线程

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

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.

如何创建提交并使用Gi​​tHub API v3推送到存储库中?

此线程有两个答案.其中一个只是Github API的链接,没有给出代码示例.另一个是Pearl解决方案(看起来),但是使用了单独的Pearlframework.因此,即使尝试转换显示的Pearl也不可行.

This thread has two answers. One is just a link to the Github API which does not give code examples. The other is a Pearl solution (it would seem), but is using a separate Pearlframework.So even trying to convert the Pearl shown is not practical.

如何创建github存储库并从中上传文件PHP?

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

The answer only repeats the other thread, that a blob should be created. But this is only the very start of what seems to be needed to actually create / update a file through Github API. This thread has a Python example, but that example needs a separate Pearl framework.

GitHub API-写入存储库

这是链接到的线程.用户提出了一个不太清楚的问题,但没有任何详细信息或明确性.

This is the thread linked to. It's answered by user who posted a not very clear question, but without any details or clarity.

看起来研究取得了成果,我可以回答自己问题的主要部分(我本来没希望这样做)-创建一个新文件.似乎可以使用相同的方法来更新现有文件(如果没有,我会回来的.)

Looks like research paid off and I can answer a main part of my own question (which I was not expecting to be able to do) - creating a new file. It looks like the same approach will be useable to update an exisiting file (if not, I'll be back).

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

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

I realised the path referred to in Github API docs (https://developer.github.com/v3/repos/contents/#get-contents) - section Create a File - could be used as the url for curl as:

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

需要一些调整以允许发送JSON.所以我现在有:

And some tweaks were needed to allow for JSON to be sent. So I now have:

$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示例:

For the $data, I used the JSON example from the Github API page:

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

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

After implementing this within a PHP file (on shared hosting), no other frameworks used, I saw the new file in the Github respository.