API使用PHP更新文件

API使用PHP更新文件

本文介绍了Github API使用PHP更新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用 github API ,并尝试从我的网站()

我可以设法获取我想要更新的文件的最后一个提交 SHA ,并且设置了所有内容。我跟随了github库上的文档。

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



可能是我的事情我做了cURL的错误,这就是为什么我没有回应。我正在使用 hostable.com



以下是我的代码,请花些时间帮助我。
$ b $ pre $ 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。\\\
;
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);

$ header = array(
'X-HTTP-Method-Override:PUT',
'Content-type:application / json',
'Content-长度:'。strlen($ content)
);
curl_setopt($ c,CURLOPT_HTTPHEADER,$ headers);

echo $ content;


$ fp = fopen('php:// temp / maxmemory:256000','w');
if(!$ fp){
die('无法打开临时内存数据');
}
fwrite($ fp,$ content);
fseek($ fp,0);

echo $ fp;

curl_setopt($ c,CURLOPT_BINARYTRANSFER,true);
curl_setopt($ c,CURLOPT_INFILE,$ fp); //文件指针
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\response:。$ response;
curl_close($ c);
}


解决方案

因为您的网址缺少域名部分。

  $ url =/ repos / $ username / $ repo / contents / $路径; 

应该是

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

为了能够在将来捕获这些问题,请使用 curl_error function

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

$ / code>

PS:它是一个8个月大的未解答的问题,但仍然回答希望它有帮助别人。

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);
}
解决方案

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.

这篇关于Github API使用PHP更新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 15:23