【发布时间】:2021-03-11 01:18:07
【问题描述】:
我正在尝试执行以下操作:
- 创建一个 Github webhook(完成)
- 创建一个 GIHook 指向的 PHP/Node 应用程序(完成)
- 处理响应(这是问题所在)
- 根据提交的分支,向开发或实时服务器发出请求以执行 update.sh(完成)
问题是 GIHook 发给我这个:
{
"REQUEST_METHOD":"GET",
"CONTENT_TYPE":"application\/json",
"headers":{
"Content-Type":"application\/json",
"X-Hub-Signature-256":"sha256=xxxx",
"X-Hub-Signature":"sha1=xxx",
"X-Github-Hook-Installation-Target-Type":"repository",
"X-Github-Hook-Installation-Target-Id":"xxx",
"X-Github-Hook-Id":"xxx",
"X-Github-Event":"push",
"X-Github-Delivery":"xxx",
"Referer":"webhook url called",
"Accept":"*\/*",
"User-Agent":"GitHub-Hookshot\/8338482",
"Connection":"close",
"X-Accel-Internal":"\/internal-nginx-static-location",
"X-Real-Ip":"xxx",
"Host":"xxxx"
},
"JSON":"",
"POST":[],
"GET":[],
"payload":null
},
这并没有告诉我提交到哪个分支。
然后我又阅读了一些零碎的内容,发现还有更多信息需要获取,但问题是如何获取。所以我假设我会调用一个 api 并回传一个 id 或其他东西,经过一番谷歌搜索后,它看起来就像这样。所以我测试了:
# :HOOK_ID = X-Github-Hook-Id
curl -o -v - https://api.github.com/repos/:GitHubUserNameInUrl/:PrivateRepo/hooks/:HOOK_ID
通过 curl 和文件获取内容对 php 执行相同操作,只返回相同的结果:
{
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/reference/repos#get-a-repository-webhook"
}
有人知道如何从 webhook 给我的响应(PHP 或 Node)中获取有效负载吗?
谢谢
D
响应 1 感谢您的回复,但我没有得到任何信息,例如您从 webhook 中获得的信息。这是我在 webhook 调用我的 url 时用来测试我的响应数据的代码。 JSON 总是 "" 并且 POST,GET 总是空数组 []。
$file = 'log.json';
$dte = date("d-m-y H:i:s");
$headers = getallheaders();
$data = [];
$data["date"] = $dte;
$data["REQUEST_METHOD"] = $_SERVER["REQUEST_METHOD"];
$data["CONTENT_TYPE"] = $_SERVER["CONTENT_TYPE"];
$data["headers"] = $headers;
$data["JSON"] = json_decode(file_get_contents('php://input')); // Always null || ""
$data["POST"] = $_POST; // Always []
$data["GET"] = $_GET; // Always []
$hookid = $headers["X-Github-Hook-Id"];
$data["hook_id"] = $hookid;
$APIUrl = "https://api.github.com/repos";
$url = $APIUrl . "/GITHUB_USER/REPO/hooks/".$hookid;
$data["callback"] = $url;
// Using file_get_contents
// $result = file_get_contents($url);
// $payload = json_decode($result, true);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result = curl_exec($ch);
curl_close($ch);
$payload = json_decode($result, true);
$data["payload"] = $payload;
$out = json_encode($data) . ",\n";
file_put_contents($file, $out, FILE_APPEND | LOCK_EX);
【问题讨论】: