Github API update a file in PHP

浪子不回头ぞ 提交于 2019-12-11 22:04:30

问题


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

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);
}

回答1:


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.



来源:https://stackoverflow.com/questions/19888832/github-api-update-a-file-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!