phpwebhookswhatsapp

Whatsapp Webhook data in PHP


I have been playing with this now for a week and can't seems to find a solution. Whatsapp Business API is set up and working 100% in Live. I have verified my Webhook but are not able to see any data coming into the webhook file since the verification. Many variations of code have been tried to get data but still I don't see any data on the file, even when testing from the Quickstart > Configuration page.

The code I am currently running is from an answer provided by @Tarik at the following post: (for facebook itself but coding is the same) Facebook Messenger API - Can't verify webhook URL (PHP)

My webhook.php file is:

$message = date('Y-m-d H:i:s');
foreach ($_REQUEST as $key => $value){
    $message .= "$key => $value (".$_SERVER['REQUEST_METHOD'].")\n";
}
$input = file_get_contents("php://input");
$array = print_r(json_decode($input, true), true);
file_put_contents('log.txt', $message.$array."\nREQUEST_METHOD: $_SERVER[REQUEST_METHOD]\n----- Request Date: ".date("d.m.Y H:i:s")." IP: $_SERVER[REMOTE_ADDR] -----\n\n", FILE_APPEND);
if ( isset($_REQUEST['hub_challenge']) ){
    echo $_REQUEST['hub_challenge'];
}

When sending a test from the Webhook Configuration page, the log.txt file only returns this:

2024-08-08 13:17:38
REQUEST_METHOD: GET
----- Request Date: 08.08.2024 13:17:38 IP: 173.252.107.116 -----

I am a bit stuck here and also lost on ideas. Can anyone advise me how to actually read the data I should receive on the webhook? I was considering playing with the verification token to see if Meta require it for some sort of asynchronous communication but that don't seem to be the case. Any assistance will be appreciated.


Solution

  • The solution was the true URL. In my case I was not providing a / at the end of the URL (my Callback URL is only showing to the directory and not the file itself).

    It was something like : https://demourl.com/webhook

    It needed to be: https://demourl.com/webhook/

    For anybody facing the same situation, you can use https://www.whatsmydns.net/redirect-checker to check your true URL.

    Also note, I had to remove the Webhook subscription and add it anew in order to actually pick up the URL change. Just changing it didn't do the trick.

    Big shoutout to @CBroe for guiding me into the right direction!!!