PHP code example of tristankechlo / github-webhook-parser

1. Go to this page and download the library: Download tristankechlo/github-webhook-parser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tristankechlo / github-webhook-parser example snippets


 # index.php

ook\WebhookHandler;
use TK\GitHubWebhook\Exception\SignatureMismatchException;
use TK\GitHubWebhook\Exception\WebhookException;
use TK\GitHubWebhook\Exception\WebhookParseException;

try {
    $handler = new WebhookHandler();
    $handler->setSecret("**************");  // the same secret you entered while creating the webhook on github.com
    $handler->registerHandler(new \Your\Custom\PingHandler());
    $handler->handle();
} catch (SignatureMismatchException $e) {
    header("HTTP/1.1 401 Unauthorized");
    exit($th->getMessage());
} catch (\Throwable | WebhookException | WebhookParseException $e) {
    header("HTTP/1.1 400 Bad Request");
    exit($e->getMessage());
}


 # PingHandler.php

namespace Your\Custom;

use TK\GitHubWebhook\Event\EventTypes;
use TK\GitHubWebhook\Event\PingEvent;
use TK\GitHubWebhook\Handler\EventHandlerInterface;
use TK\GitHubWebhook\Request;
use TK\GitHubWebhook\Response;

class PingHandler implements EventHandlerInterface
{

    public function getTargetEvent(): array
    {
        return [EventTypes::PING];
    }

    public function handleEvent(Request $request, Response $response): Response
    {
        if ($request->event instanceof PingEvent) {
            $response->setStatusCode(200);
            $response->setMessage("Pong!");
            return $response;
        }

        // do stuff with the input
        $response->setStatusCode(200);
        $response->setMessage("Not Used");
        return $response;
    }
}