[英]Github WebHooks get branch details
我正在尝试执行以下操作:
问题是 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 始终为空 arrays []。
$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);
对于"X-Github-Event":"push",
和Content-Type: application/x-www-form-urlencoded
有效载荷是百分比编码 JSON 而不是null
POST /test HTTP/1.1
Host: lcherone.requestcatcher.com
Connection: close
Accept: */*
Connection: close
Content-Length: 11363
Content-Type: application/x-www-form-urlencoded
User-Agent: GitHub-Hookshot/8338482
X-Github-Delivery: e39cc102-318e-11eb-8301-d4f46caa1c8a
X-Github-Event: push
X-Github-Hook-Id: 265270862
X-Github-Hook-Installation-Target-Id: 316492816
X-Github-Hook-Installation-Target-Type: repository
X-Hub-Signature: sha1=0baa6d15c266c08111508642607acc86b15f1f43
X-Hub-Signature-256: sha256=6c58a95d4a5a3cde91bcc2621d6608a5190b1178be197db00ed6dd4364137898
payload=%7B%22ref%22%3A%22refs%2Fheads%2Fmain%22%2C%22before%22%3A%220000000000000000000000000000000000000000%22%2C%22after%22%3A%224872b45f3df6d2b0485a69adb8c7476934be7629%22%2C%22repository%22%3A%7B%22id%22%3A316492816%2C%22node_id%22%3A%22MDEwOlJlcG9zaXRvcnkzMTY0OTI4MTY%3D%22%2C%22name%22%3A%22wightsystems%22%2C%22full_name%22%3A%22wightsystems%2Fwightsystems%22%2C%22private%22%3Afalse%2C%22owner%22%3A%7...snip
要解码你会做什么:
$payload = json_decode(urldecode($_POST['payload']));
(可能不需要 urldecode,我没有检查)
如果您选择Content-Type: application/json
,则有效负载是原始 json:
POST /test HTTP/1.1
Host: lcherone.requestcatcher.com
Connection: close
Accept: */*
Connection: close
Content-Length: 8176
Content-Type: application/json
User-Agent: GitHub-Hookshot/8338482
X-Github-Delivery: 2c350428-3190-11eb-90a9-13e895014cf2
X-Github-Event: push
X-Github-Hook-Id: 265270862
X-Github-Hook-Installation-Target-Id: 316492816
X-Github-Hook-Installation-Target-Type: repository
X-Hub-Signature: sha1=20ea83c65ee1771913829db5553a13c09d5d44c0
X-Hub-Signature-256: sha256=eb89d594cfa830be18d11f00d502bd0fdb118fa9017496a8bf52c6a286b1ced6
{"ref":"refs/heads/main","before":"4872b45f3df6d2b0485a69adb8c7476934be7629","after":"ab03e7b705440088a6e22f06d31a660c8185bcb6","repository":{"id":316492816,"node_id":"MDEwOlJlcG9zaXRvcnkzMTY0OTI4MTY=","name":"wightsystems","full_name":"wightsystems/wightsystems","private":false,"owner":...snip}
这将是:
$payload = json_decode(file_get_contents('php://input'));
一旦解码,它就会有一堆数据,包括ref
的分支,并且要访问它是:
echo str_replace('refs/heads/', '', $payload->ref);
stdClass Object
(
[ref] => refs/heads/main
[before] => 0000000000000000000000000000000000000000
[after] => 4872b45f3df6d2b0485a69adb8c7476934be7629
...snip
NGINX 从 / 重定向到 /index.php 是问题所在,似乎所有数据都没有通过从 / 到 index.php 的重定向完全传递。
解决方案 将 webhook 设置为直接指向文件,在本例中为 index.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.