I am trying to build PHP functions to create and update files within a Github repository, using PHP through the 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.
So far
I have managed to access and list files using PHP, using the info given here:
Github API List all repositories and repo's content
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).
Both list clearly the code and steps needed to list files and get contents (thank you @flesheater and @Ivan Zuzak)
I searched for help or solutions for this. So far I have not found any example, working or otherwise, using PHP.
Git API documentation
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
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.
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?
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.
How to create github Repository and upload files from PHP?
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.
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).
This link helped: http://www.lornajane.net/posts/2009/putting-data-fields-with-php-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
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;
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="
}
After implementing this within a PHP file (on shared hosting), no other frameworks used, I saw the new file in the Github respository.