How to create and update a file in a Github repository with PHP and Github API? -


i trying build php functions create , update files within github repository, using php through github api.

the php files being run standard shared hosting account. therefore using frameworks depend on installing composer or other libraries not option me. example, https://github.com/knplabs/php-github-api not option.

so far

i have managed access , list files using php, using info given here:

github api list repositories , repo's content

i used combination of 2nd , 6th answer (2nd starts here nice way curl. flesheater , 6th answer when "display repo , contents" ivan zuzak).

both list code , steps needed list files , contents (thank @flesheater , @ivan zuzak)

i searched or solutions this. far have not found example, working or otherwise, using php.

git api documentation

git data api info (https://developer.github.com/v3/git/) says this:

as example, if wanted commit change file in repository, would:

get current commit object retrieve tree points retrieve content of blob object tree has particular file path change content somehow , post new blob object new content, getting blob sha post new tree object file path pointer replaced new blob sha getting tree sha create new commit object current commit sha parent , new tree sha, getting commit sha update reference of branch point new commit sha 

however there no code examples of how done, , not sure if 'commit' need (if understand correctly, not creating file, content).

git repository api info (https://developer.github.com/v3/repos/contents/#create-a-file):

the part 'create file' shows method doing this, without code examples. usure how or if can used prgrammatically.

github api update file

similar threads

in opinion none offering enough clarity or information provide solution, due either questions or answers. therefore don't think duplicate.

how create commit , push repo github api v3?

this thread has 2 answers. 1 link github api not give code examples. other pearl solution (it seem), using separate pearlframework.so trying convert pearl shown not practical.

how create github repository , upload files php?

the answer repeats other thread, blob should created. start of seems needed create / update file through github api. thread has python example, example needs separate pearl framework.

github api - write repo

this thread linked to. it's answered user posted not clear question, without details or clarity.

looks research paid off , can answer main part of own question (which not expecting able do) - creating new file. looks same approach useable update exisiting file (if not, i'll back).

this link helped: http://www.lornajane.net/posts/2009/putting-data-fields-with-php-curl

i realised path referred in github api docs (https://developer.github.com/v3/repos/contents/#get-contents) - section create file - used url curl as:

https://api.github.com/repos/ user / repo_name /contents/ filename 

and tweaks needed allow json sent. 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 $data, used json example github api page:

{   "message": "my commit message",   "committer": {     "name": "mike a",     "email": "someemail@gmail.com"   },   "content": "bxkgbmv3igzpbgugy29udgvudhm=" } 

after implementing within php file (on shared hosting), no other frameworks used, saw new file in github respository.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -