Facebook Messenger API - Can't verify webhook URL (PHP)


Facebook Messenger API - Can't verify webhook URL (PHP)

我正试图建立一个fb信使聊天机器人,但似乎无法验证webhook回调url。每次我尝试验证它时,我都会收到这样的错误消息——URL无法验证。响应与质询不匹配,预期值="1596214014",收到的="

这是屏幕截图:

屏幕截图

这是我正在使用的php-

<?php
$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];
if ($verify_token === 'token_my_token') {
echo $challenge;
}

我也试过

echo $_GET['hub_challenge'];

只有

echo file_get_contents('php://input');

所有这些都会导致与上面相同的错误消息。基本上,据我所知,facebook没有向我的服务器发送GET请求,或者如果是,它也不包括任何数据。有人能告诉我是否做错了什么,或者是否有一个设置我需要更改以确保脸书正确发送数据吗?

编辑-当检查访问日志时,这就是我发现的,看起来脸书没有在获取请求中发送任何数据。

2a03:2880:1010:dffb:face:b00c:0:8000 - - [19/Apr/2016:20:50:06 +0000] "GET /wp-content/plugins/applications/fbmessenger.php HTTP/1.0" 200 - "-" "facebookplatform/1.0 (+http://developers.facebook.com)

感谢

只要尝试我的代码,它就会工作。

 $challenge = $_REQUEST['hub_challenge'];
  $verify_token = $_REQUEST['hub_verify_token'];
  if ($verify_token === 'Your's app token') {
  echo $challenge;
  }
  //Token of app
 $row = "Token";

 $input = json_decode(file_get_contents('php://input'), true);
//Receive user
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
 //User's message
 $message = $input['entry'][0]['messaging'][0]['message']['text'];

//Where the bot will send message
 $url = 'https://graph.facebook.com/v2.6/me/messages?access_token='.$row;

 $ch = curl_init($url);
//Answer to the message adds 1
if($message)
{
 $jsonData = '{
    "recipient":{
        "id":"'.$sender.'"
      }, 
    "message":{
        "text":"'.$message. ' 1' .'"
      }
 }';
};

 $json_enc = $jsonData;
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $json_enc);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));  
 if(!empty($input['entry'][0]['messaging'][0]['message'])){
    $result = curl_exec($ch);
 }

你必须返回挑战,这样Facebook才能验证其正确的Url和令牌匹配

 <?php
$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];
if ($verify_token === 'token_my_token') {
echo $challenge;
}

Facebook文档链接(在Node.js中)您可以在验证令牌后看到挑战返回

https://developers.facebook.com/docs/messenger-platform/getting-started/webhook-setup

你能试试我的API吗?https://github.com/Fritak/messenger-platform

如果你像示例中那样设置它,它应该可以工作:

// This is just an example, this method of getting request is not safe!
$stream  = file_get_contents("php://input");
$request = empty($stream)? $_REQUEST : $stream;
$bot = new 'fritak'MessengerPlatform(
        ['accessToken'      => 'token_for_app',
         'webhookToken'     => 'my_secret_token',
         'facebookApiUrl'   => 'https://graph.facebook.com/v2.6/me/' //2.6 is minimum
        ], $request);
    if($bot->checkSubscribe())
    {
        print $bot->request->getChallenge();
        exit;
    }

若并没有,那个么问题就出在Facebook和脚本之间,而不是PHP本身。去检查apache设置等

问题可能在脸书方面,他们在过去几天里遇到了一些问题。。。

在php文件中只有这段代码:(fbmessenger.php)

<?php 
// header('HTTP/1.1 200 OK');
/* GET ALL VARIABLES GET & POST */
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('fbmessenger.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);
echo $_REQUEST['hub_challenge'];

您会将请求保存在同一目录中名为"fbmessenger.txt"的文件中。

请注意,由于某些奇怪的原因,您可能需要向获得批准&已保存!(在fb之前,我打了8-9次"保存"批准的链接)

确保您使用https(SSL)连接,连接完成后,使用"hub_verify_token"验证您的令牌,以确保请求来自fb。