Facebook Webhook Integration Code in PHP


Improve your writing skills in 5 minutes a day with the Daily Writing Tips email newsletter.

Many types of applications that interact with Facebook will need to use webhooks. Messenger chat bots are an example.

If you follow Facebook’s setup guide, you’ll notice that the example provided to activate a webhook for the first time is in Node.js:

  app.get('/webhook', function (req, res) {
    if (req.query['hub.verify_token'] === 'YOUR_VERIFY_TOKEN') {
      res.send(req.query['hub.challenge']);
    } else {
      res.send('Error, wrong validation token');    
    }
  });

I saw on online forums many people looking for a PHP version of that code, so here you go:

<?php

$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];

if ($verify_token === 'my_token') {
    echo $challenge;
}
else{
    echo "Error, wrong validation token";
}

?>

Leave a Reply

Your email address will not be published. Required fields are marked *