Skip to content

Instantly share code, notes, and snippets.

@hayden-t
Last active August 24, 2023 10:51
Show Gist options
  • Save hayden-t/e3a6a85d404897430188368fad53786a to your computer and use it in GitHub Desktop.
Save hayden-t/e3a6a85d404897430188368fad53786a to your computer and use it in GitHub Desktop.
GitHub Webhook Discord filter by branch for PHP
<?php
//filter githubs webhooks by using this conditional relay script
//used for discord, but also usable for other needs.
//set the webhook url in github to the public url to this php file
$headers = getallheaders();
//file_put_contents('/home/user/public_html/webhook_data.txt', json_ensode($headers) );//debug
$rawPostBody = file_get_contents('php://input');
//file_put_contents('/home/user/public_html/webhook_data.txt', $rawPostBody );//debug
$postData = json_decode($rawPostBody, true);
if(!isset($postData['ref']) || $postData['ref'] != "refs/heads/main")return;//push main only
//discord webhook url with /github on end
$webhookurl = '';
$request_headers = [
'Content-type: application/json',
'X-Github-Event: '.$headers['X-Github-Event'],
'X-Github-Delivery: '.$headers['X-Github-Delivery']
];
$ch = curl_init( $webhookurl );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $request_headers );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $rawPostBody);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
curl_close( $ch );
//file_put_contents('/home/user/public_html/webhook_data.txt', $response );//debug
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment