Setup a webhook in PHP

Hi @Bastien_Siebman your PHP webhook idea sounds like a real game changer when it comes the my team’s current workflow.

Any chance you can share that with me? Thanks a ton in advance, and happy new year!

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

@Bastien_Siebman woah, super fast. This definitely helps. Thanks a ton! Hope I can return the favor in the future.

No problem. If you ever need mire in depth work, I am available for freelancing work, and you can also find other developers on Asana Experts.

Hi,

What is insided “$project” ? The Id of project or the whole project?

Thank you

Hey @Charlie1, the id of the project

1 Like