0

我正在尝试习惯github API,并尝试从我的网站(http://developer.github.com/v3/repos/contents/)更新文件

我可以设法获得SHA要更新的文件的最后一次提交,并且一切都已设置。我关注了 github 库上的文档。

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

可能我觉得我对 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);
}
4

1 回答 1

0

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

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

应该

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

为了能够在将来发现此类问题,请使用该curl_error功能

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

PS:这是一个 8 个月大的未回答问题,但仍回答希望对其他人有所帮助。

于 2014-07-11T16:32:53.090 回答