GitHub WebHook POSTs not going through

爷,独闯天下 提交于 2019-12-11 19:45:56

问题


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 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');
}

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!

*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.

-Jeremy


回答1:


You need to use the following code to grab the JSON Data:

$data = json_decode( file_get_contents('php://input') );

If you need an entire solution; I've found little tools to help deploy your code from Github or Gitlab so I created Deepl.io to handle Web-Hooks and call scripts to deploy on your own server. This handles the JSON that's sent from github or gitlab and can be used for multiple repositories and branches etc. You can use your own PHP or shell scripts after receiving the pull notification and it sends you status e-mails after every deploy... Check it out: http://deepl.io




回答2:


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.

Here is the simple fix:

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


来源:https://stackoverflow.com/questions/16483905/github-webhook-posts-not-going-through

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!