我正在尝试使用PHP通过Github API构建PHP函数来创建和更新Github存储库中的文件。
PHP文件正在从标准的共享主机帐户运行。因此,使用依赖于安装Composer或其他库的框架不是我的选择。例如,这个https://github.com/KnpLabs/php-github-api不是一个选项。
到目前为止
我已经设法使用PHP访问和列出文件,使用此处给出的信息:
Github API List all repositories and repo's content
我使用了第2和第6个答案的组合(第2次开始用这是一个很好的方式,只有卷曲。由flesheater和第6个回答与当你说"显示回购及其内容" 作者:Ivan Zuzak)。
两者都清楚地列出了列出文件和获取内容所需的代码和步骤(谢谢@flesheater和@Ivan Zuzak)
我为此寻找了帮助或解决方案。到目前为止,我还没有使用PHP找到任何工作或其他方面的例子。
Git API文档
Git Data API信息(https://developer.github.com/v3/git/)说明了这一点:
例如,如果您想要对您的文件中的文件进行更改 存储库,你会:
get the current commit object retrieve the tree it points to retrieve the content of the blob object that tree has for that particular file path change the content somehow and post a new blob object with that new content, getting a blob SHA back post a new tree object with that file path pointer replaced with your new blob SHA getting a tree SHA back create a new commit object with the current commit SHA as the parent and the new tree SHA, getting a commit SHA back update the reference of your branch to point to the new commit SHA
但是没有关于如何完成此操作的代码示例,我不确定是否提交了#39;是我需要的(如果我理解正确,这不是创建文件,只是内容)。
Git Repository API信息(https://developer.github.com/v3/repos/contents/#create-a-file):
部分'创建文件'显示了执行此操作的方法,但没有代码示例。我知道如何以及如果可以用prgrammatically。
类似线程
在我看来,由于问题或答案,没有人提供足够的清晰度或信息来提供解决方案。因此,我不认为这是重复的。
How to create a commit and push into repo with GitHub API v3?
这个帖子有两个答案。一个是Github API的链接,它没有提供代码示例。另一种是珍珠解决方案(看起来似乎),但是使用了单独的Pearlframework。所以即使尝试转换所显示的Pearl也不实用。
How to create github Repository and upload files from PHP?
答案只重复另一个线程,即应创建一个blob。但这只是通过Github API实际创建/更新文件似乎需要的开始。这个线程有一个Python示例,但该示例需要一个单独的Pearl框架。
这是链接到的主题。用户回答了一个不太清楚的问题,但没有任何细节或清晰度。
答案 0 :(得分:4)
看起来研究得到了回报,我可以回答我自己问题的主要部分(我不希望这样做) - 创建一个新文件。看起来相同的方法可用于更新现有文件(如果没有,我会回来)。
此链接有助于:http://www.lornajane.net/posts/2009/putting-data-fields-with-php-curl
我意识到Github API文档中引用的路径(https://developer.github.com/v3/repos/contents/#get-contents) - 创建文件部分 - 可以用作curl的url:
https://api.github.com/repos/ USER / REPO_NAME /contents/ FILENAME
需要进行一些调整以允许发送JSON。所以我现在有:
$curl_url = $url
$curl_token_auth = 'Authorization: token ' . $token;
$ch = curl_init($curl_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'User-Agent: $username', $curl_token_auth ));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
return $response;
对于$ data,我使用了Github API页面中的JSON示例:
{
"message": "my commit message",
"committer": {
"name": "Mike A",
"email": "someemail@gmail.com"
},
"content": "bXkgbmV3IGZpbGUgY29udGVudHM="
}
在PHP文件(在共享主机上)中实现之后,没有使用其他框架,我在Github存储库中看到了新文件。
答案 1 :(得分:0)
也许对某人有帮助(除了具有更新或删除文件的功能外,还具有SHA功能)
<?php
$file_git = "wall.jpg";
$data_git = array(
'sha'=>file_get_contents("sha.txt"),
'message'=>'image',
'content'=> base64_encode($file_git),
'committer'=> array(
'name'=>'Jacob',
'email' => '45331093+greenandgreen@users.noreply.github.com'
)
);
$data_string_git = json_encode($data_git);
$ch_git = curl_init('https://api.github.com/repos/YOUR_REPO/contents/wall.jpg');
curl_setopt($ch_git, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch_git, CURLOPT_POSTFIELDS, $data_string_git);
curl_setopt($ch_git, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_git, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 YaBrowser/19.9.3.314 Yowser/2.5 Safari/537.36',
'Authorization: token PLACE_YOUR_PERSONAL_TOKEN_HERE'
));
$result_git = curl_exec($ch_git);
echo $result_git;
$p_git = json_decode($result_git);
file_put_contents("sha.txt",$p_git->content->sha);
?>
在PHP 7上测试。 如果代码有效,那么我不会拒绝喜欢的东西,感谢所做的小小的工作:)
答案 2 :(得分:-1)
<?php
function pushFile($username,$token,$repo,$branch,$path,$b64data){
$message = "Automated update";
$ch = curl_init("https://api.github.com/repos/$repo/branches/$branch");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent:Php/Automated'));
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $token);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
curl_close($ch);
$data=json_decode($data,1);
$ch2 = curl_init($data['commit']['commit']['tree']['url']);
curl_setopt($ch2, CURLOPT_HTTPHEADER, array('User-Agent:Php/Ayan Dhara'));
curl_setopt($ch2, CURLOPT_USERPWD, $username . ":" . $token);
curl_setopt($ch2, CURLOPT_TIMEOUT, 30);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, TRUE);
$data2 = curl_exec($ch2);
curl_close($ch2);
$data2=json_decode($data2,1);
$sha='';
foreach($data2["tree"] as $file)
if($file["path"]==$path)
$sha=$file["sha"];
$inputdata =[];
$inputdata["path"]=$path;
$inputdata["branch"]=$branch;
$inputdata["message"]=$message;
$inputdata["content"]=$b64data;
$inputdata["sha"]=$sha;
echo json_encode($inputdata);
$updateUrl="https://api.github.com/repos/$repo/contents/$path";
echo $updateUrl;
$ch3 = curl_init($updateUrl);
curl_setopt($ch3, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', 'User-Agent:Php/Ayan Dhara'));
curl_setopt($ch3, CURLOPT_USERPWD, $username . ":" . $token);
curl_setopt($ch3, CURLOPT_TIMEOUT, 30);
curl_setopt($ch3, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch3, CURLOPT_POSTFIELDS, json_encode($inputdata));
$data3 = curl_exec($ch3);
curl_close($ch3);
echo $data3;
}
//pushFile("your_username","your_personal_token","username/repository","repository_branch","path_of_targetfile_in_repository","base64_encoded_data");
?>