GithubAPI在PHP中更新文件

GithubAPI在PHP中更新文件,php,curl,github,github-api,Php,Curl,Github,Github Api,我正在尝试使用githubapi,并尝试从我的网站更新文件() 我可以设法获得我想要更新的文件的最后一次提交SHA,一切都已设置好。我遵循了github库上的文档 当代码运行时,它打印出除了curl调用的响应之外的所有内容。我尝试了所有可能的修复方法:对PUT方法使用其他方法,检查curl是否启用,更改不同类型的输入数据。但我无法让它工作 也许我觉得我的卷发有问题,这就是为什么我没有得到回应。我正在使用hostable.com 这是我的密码,请慢慢帮我。非常感谢你 function editFi

我正在尝试使用
githubapi
,并尝试从我的网站更新文件()

我可以设法获得我想要更新的文件的最后一次提交
SHA
,一切都已设置好。我遵循了github库上的文档

当代码运行时,它打印出除了curl调用的响应之外的所有内容。我尝试了所有可能的修复方法:对PUT方法使用其他方法,检查curl是否启用,更改不同类型的输入数据。但我无法让它工作

也许我觉得我的卷发有问题,这就是为什么我没有得到回应。我正在使用
hostable.com

这是我的密码,请慢慢帮我。非常感谢你

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

您没有得到任何输出,因为您的URL缺少域部分

$url = "/repos/$username/$repo/contents/$path";
应该是

$url = "https://api.github.com/repos/$username/$repo/contents/$path";
要在将来捕获此类问题,请使用
curl\u error
函数

$response = curl_exec($c);
if(curl_exec($ch) === false)
{
    echo 'Curl error: ' . curl_error($ch);
}
附言:这是一个8个月前还没有回答的问题,但仍然在回答,希望它能帮助别人