0

I'm trying to get the FB Messenger Bot API to work. I'm currently on the step where I'm trying to subscribe a webhook. I currently have a script of the following form:

#!/usr/bin/php

<?php

$challenge = $_REQUEST['hub_challenge'];
echo $challenge;  # HERE!
$verify_token = $_REQUEST['hub_verify_token'];

if ($verify_token === 'token') {
echo $challenge;
}

?>

However when I try to "Verify and Save" the callback URL I get an error of the form:

 The URL couldn't be validated. Response does not match challenge, expected value = '401537941', received=''

namely that my script is sending an empty string. However, if I change the line marked "HERE!" above to "echo 'cat' ", the error message is the same except "received='cat'" as expected. Thus, my script is being executed and is trying to send some content back to FB, but for some reason the $challenge variable is empty. Why could this be the case?

Thanks!

MEric
  • 946
  • 3
  • 12
  • 30
  • When you were setting up the webhook did you set the 'verify token' as token ? If yes try taking out the # HERE! line entirely. – Sritam Apr 21 '16 at 01:07
  • Yes I added the #HERE! line after the fact just to see whether $challenge was returning anything. It's not actually in my code right now. – MEric Apr 21 '16 at 16:41

2 Answers2

1
 if($_GET['hub_verify_token'] === "validation_token"){
     echo($_GET["hub_challenge"]);
 } else {
     echo("error");
 }
Sritam
  • 3,022
  • 1
  • 18
  • 17
  • hi Sritam, I tried this and my returned message is "error", because it appears that $_GET['hub_verify_token'] is empty. How can I check to see the contents of the GET request FB is sending my callback? – MEric Apr 21 '16 at 16:41
0

There are probably extra string in your response as you are not exiting after printing challenge. Try your script in browser and inspect html to see if there is anything extra.

Use the following code as you would need to seperate verification code from your work webhook calls. Also verify token is not something you create from Facebook, its your own keyword

/* validate verify token needed for setting up web hook */ 
if (isset($_GET['hub_verify_token'])) { 
   if ($_GET['hub_verify_token'] === 'YOUR_SECRET_TOKEN') {
       echo $_GET['hub_challenge'];
       return;
   } else {
       echo 'Invalid Verify Token';
       return;
   }
}

So in this case your verify token is YOUR_SECRET_TOKEN, now when you are setting up web hook, Type YOUR_SECRET_TOKEN in the verification token.

I wrote recently a step by step guide with screen shots here.

Nadeem Manzoor
  • 760
  • 5
  • 14