Setup a webhook in PHP

Hi,

Here is some of my code, don’t hesitate to ask for more.

I am using the “official” PHP lib from Asana.

Adding the webhook

$webhook = $client->webhooks->create(array(
        'resource' => $project,
        'target' => $ROOT_URL.'webhooks-callback.php'
));

webhook-callback.php to react to call

<?php
require 'lib/functions.php';
require_once 'vendor/autoload.php';

$headers = apache_request_headers();

// if headers contain X-Hook-Secret, we need to answer with 200
if (array_key_exists('X-Hook-Secret', $headers)) {
    $xHookSecret = $headers['X-Hook-Secret'];

    // header needs to be resent
    header('X-Hook-Secret: ' . $xHookSecret);
    header("HTTP/1.1 200 OK");
    exit;
}

$entries = json_decode(file_get_contents('php://input'),true);
$events = $entries['events'];

foreach ($events as $event) {
   // do something
   // you can access the resource, user or type with $event['resource'],$event['user'],$event['type']
}

Does it help?

5 Likes