GitHub WebHook POST未通过 [英] GitHub WebHook POSTs not going through

查看:134
本文介绍了GitHub WebHook POST未通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了github webhoooks,以便在我推送到仓库时将POST发送到服务器上的PHP脚本. ( https://help.github.com/articles/post-receive-hooks)

I've got github webhoooks set up to send a POST to a PHP script on my server when I push to a repo. (https://help.github.com/articles/post-receive-hooks)

MY PHP脚本记录连接IP以及接收到的有效负载:

MY PHP script logs the connecting IP, as well as the payload received:

$date = strftime('%c');
file_put_contents('log.txt', PHP_EOL.trim($date).PHP_EOL, FILE_APPEND);
try
{
  $payload = json_decode($_REQUEST['payload']);
}
catch(Exception $e)
{
  exit(0);
}

//LOG THE POST REQUEST
file_put_contents('log.txt', print_r($payload, TRUE), FILE_APPEND);

//EXECUTE A SCRIPT WHEN THE POST REQUEST IS INITIALIZED
if ($payload->ref === 'refs/heads/master')
{
  exec('deploy.sh >> log.txt');
}

从日志中可以看到,当我推送到我的仓库时,一个IP已连接,但是没有记录POST数据.我使用RequestBin进行了测试,并确认存在POST数据.此外,如果我在URL中手动放置$ _GET有效负载变量,则会记录该变量(因为我正在检查$ _REQUEST而不是仅检查$ _POST).我怀疑我的PHP服务器配置为拒绝来自其他服务器的POST请求.我使用的是1and1共享托管,因此我没有专用的控件,但是可以使用SSH进行登录,也可以使用.ini文件在本地配置PHP设置.任何建议将不胜感激!

From the logs, I can see that an IP connects when I push to my repo, but no POST data is recorded. I tested with RequestBin, and confirmed that POST data present. Furthermore, if I manually put a $_GET payload variable in the URL, that is recorded (since I'm checking $_REQUEST and not just $_POST). My suspicion is that my PHP server is configured to deny POST requests from other servers. I'm using 1and1 Shared Hosting, so I don't have dedicated control, but I can SSH in, as well as configure PHP settings locally with an .ini file. Any suggestions would be greatly appreciated!

*我不在乎我是用PHP还是其他某种语言来执行此操作.如果您认为使用Python或其他方法可能会更容易,那也很酷.

*I don't care if I do this in PHP, or some other language. If you think this might be easier using a Python or something else, that's cool too.

-杰里米

推荐答案

好吧,我通过在执行JSON解码之前先打印原始发布数据来解决了这一问题.事实证明,我的PHP安装启用了魔术引号,并自动在JSON有效负载中的每个引号之前加上转义斜杠.显然,这使JSON无效,结果JSON_decode错误.

Ok I figured it out by first printing the raw post data before doing the JSON decode. Turns out my PHP installation has magic quotes enabled, and automatically put escape slashes before every quote in the JSON payload. Obviously, this made the JSON invalid, and JSON_decode errored-out as a result.

这是简单的解决方法:

$payload = json_decode(stripslashes($_REQUEST['payload']));

这篇关于GitHub WebHook POST未通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆