title: How to create and update a file in a Github repository with PHP and Github API?
body: <p>I am trying to build PHP functions to create and update files within a Github repository, using PHP through the Github API. </p>
  
  <p><em>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 <a href="https://github.com/KnpLabs/php-github-api" rel="noreferrer">https://github.com/KnpLabs/php-github-api</a> is not an option.</em></p>
  
  <p><strong>So far</strong></p>
  
  <p>I have managed to access and list files using PHP, using the info given here:</p>
  
  <p><a href="https://stackoverflow.com/questions/14390090/github-api-list-all-repositories-and-repos-content">Github API List all repositories and repo&#39;s content</a></p>
  
  <p>I used a combination of the 2nd and 6th answer (2nd starts with <em>Here is a nice way just with the curl.</em> by flesheater and 6th answer with <em>When you say "display a repo and its contents"</em> by Ivan Zuzak).</p>
  
  <p>Both list clearly the code and steps needed to list files and get contents (thank you @flesheater and @Ivan Zuzak)</p>
  
  <p>I searched for help or solutions for this. So far I have not found any example, working or otherwise, using PHP.</p>
  
  <p><strong>Git API documentation</strong></p>
  
  <p>Git Data API info (<a href="https://developer.github.com/v3/git/" rel="noreferrer">https://developer.github.com/v3/git/</a>) says this:</p>
  
  <blockquote>
    <p>As an example, if you wanted to commit a change to a file in your
    repository, you would:</p>
  
  <pre><code>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
  </code></pre>
  </blockquote>
  
  <p>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).</p>
  
  <p>Git Repository API info (<a href="https://developer.github.com/v3/repos/contents/#create-a-file" rel="noreferrer">https://developer.github.com/v3/repos/contents/#create-a-file</a>):</p>
  
  <p>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.</p>
  
  <h2><a href="https://i.stack.imgur.com/oi5eZ.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/oi5eZ.jpg" alt="Github API Update a File"></a></h2>
  
  <p><strong>Similar threads</strong></p>
  
  <p>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.</p>
  
  <p><a href="https://stackoverflow.com/questions/11801983/how-to-create-a-commit-and-push-into-repo-with-github-api-v3">How to create a commit and push into repo with GitHub API v3?</a></p>
  
  <p>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.</p>
  
  <p><a href="https://stackoverflow.com/questions/23152702/how-to-create-github-repository-and-upload-files-from-php">How to create github Repository and upload files from PHP?</a></p>
  
  <p>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.</p>
  
  <p><a href="https://stackoverflow.com/questions/6668283/github-api-write-to-repo/7506554">GitHub API - write to repo</a></p>
  
  <p>This is the thread linked to. It's answered by user who posted a not very clear question, but without any details or clarity.</p>