【问题标题】:Github API update a file in PHPGithub API 在 PHP 中更新文件
【发布时间】:2013-11-22 05:17:31
【问题描述】:

我正在尝试习惯 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);
}

【问题讨论】:

    标签: php curl github github-api


    【解决方案1】:

    您没有得到任何输出,因为您的 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 个月大的未回答问题,但仍然回答希望对其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      • 2021-09-10
      相关资源
      最近更新 更多