Github API update a file in PHP


I'm trying to get use to github API, and trying to update a file from my website (<a href="http://developer.github.com/v3/repos/contents/" rel="nofollow">http://developer.github.com/v3/repos/contents/</a>)

I could manage to get the last commit SHA of the file that I want to update, and everything is setup. I followed the document on github library.

When the code is run, it prints out everything except the response from the curl call. I tried all possible fixes that I could: use other ways for PUT method, check whether curl is enabled or not, change different type of input data. but I couldn't get it work.

Probably I thing I did something wrong with the cURL that is why I got no response back. I'm using hostable.com

Here are my code, please take your time to help me with that. Thank you so much!

function editFileOnRepo($username, $password, $message, $path, $bodyContent, $repo, $sha){ $c = curl_init(); $url = "/repos/$username/$repo/contents/$path"; //PUT /repos/:owner/:repo/contents/:path echo $url."\n"; curl_setopt($c, CURLOPT_VERBOSE, "false"); curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($c, CURLOPT_USERPWD, "$username:$password"); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); curl_setopt($c, CURLOPT_USERAGENT, "testacc01"); curl_setopt($c, CURLOPT_HEADER, true); curl_setopt($c, CURLOPT_FOLLOWLOCATION, true); curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($c, CURLOPT_PUT, true); $data = array(); $data['path'] = $path; $data['message'] = $message; $data['content'] = $bodyContent; $data['sha'] = $sha; $content = json_encode($data, JSON_FORCE_OBJECT); $headers = array( 'X-HTTP-Method-Override: PUT', 'Content-type: application/json', 'Content-Length: ' . strlen($content) ); curl_setopt($c, CURLOPT_HTTPHEADER, $headers); echo $content; $fp = fopen('php://temp/maxmemory:256000', 'w'); if (!$fp) { die('could not open temp memory data'); } fwrite($fp, $content); fseek($fp, 0); echo $fp; curl_setopt($c, CURLOPT_BINARYTRANSFER, true); curl_setopt($c, CURLOPT_INFILE, $fp); // file pointer curl_setopt($c, CURLOPT_INFILESIZE, strlen($content)); error_log($url); curl_setopt($c, CURLOPT_URL, $url); curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0); $response = curl_exec($c); echo "\nresponse:".$response; curl_close($c); }


You are not getting any output because your URL is missing the domain part.

$url = "/repos/$username/$repo/contents/$path";

should be

$url = "https://api.github.com/repos/$username/$repo/contents/$path";

To be able to catch such issues in future, use the curl_error function

$response = curl_exec($c); if(curl_exec($ch) === false) { echo 'Curl error: ' . curl_error($ch); }

PS: Its a 8 month old unanswered question, but still answering hoping it helps someone else.


  • Bind a DataGrid to the selectedrow object of a second datagrid, WPF Caliburn.Micro
  • if statement in SQL to calculate age
  • AVAssetExportSession using AVAssetExportPresetPassthrough breaking output
  • Combining Reports that use subreports already
  • how to use request.form[] in inherited web form
  • webforms : use infragistics webdropdown in updatepanel
  • How to get the second element from the sublist using python
  • Not able to read Object values in Freemarker Template
  • font-size in @media queries not applying
  • Referencing external dependencies in GGTS by convention
  • force insert with spring data jpa
  • Google cloud datastore emulator init data
  • “git push” doing the same as “git fetch” from the remote
  • How to calculate the camera position from Vuforia GL matrix?
  • Default parameter as generic type
  • custom UITableViewCell with image for highlighting
  • MongoError: Incorrect arguments
  • Django rest serializer Breaks when data exists
  • Meteor: Do Something On Email Verification Confirmation
  • NHibernate Validation Localization with S#arp Architecture
  • How can I send an e-mail from a vbs script
  • Accessing IRQ description array within a module and displaying action names
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • Where to put my custom functions in Wordpress?
  • Symfony2: How to get request parameter
  • RestKit - RKRequestDelegate does not exist
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • WPF Applying a trigger on binding failure
  • Getting error when using KSoap library to consume .NET web services
  • Getting Messege Twice Using IMvxMessenger
  • Authorize attributes not working in MVC 4
  • Binding checkboxes to object values in AngularJs
  • UserPrincipal.Current returns apppool on IIS
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?