【问题标题】:Facebook Messenger API - Can't verify webhook URL (PHP)Facebook Messenger API - 无法验证 webhook URL (PHP)
【发布时间】:2016-04-19 16:25:17
【问题描述】:

我正在尝试设置一个 fb messenger 聊天机器人,但似乎无法验证 webhook 回调 url。每次我尝试验证它时,都会收到此错误消息 - 无法验证 URL。响应与挑战不匹配,期望值 = '1596214014',收到 =''

截图如下:

Screenshot

这是我正在使用的 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 请求,或者它不包含任何数据。谁能告诉我是否做错了什么,或者是否需要更改设置以确保 facebook 正确发送数据?

编辑 - 检查访问日志时,这是我发现的,看起来 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)

谢谢

【问题讨论】:

  • 您查看过 Facebook 发送给您的日志吗?
  • 是的,使用该信息编辑了上面的帖子。谢谢。
  • echo $_REQUEST['hub_challenge']; 是让它发挥作用的关键。确保您将其发送回去,并且您不再发送任何内容! (不要回显更多代码)并且,有时需要 8-9 次试验才能使其工作。如果您遇到错误,请依次点击“保存”。奇怪,但我的在 9 次试验后工作。 (不改变我的代码)

标签: php facebook webhooks messenger facebook-messenger


【解决方案1】:

只需尝试我的代码,它就会起作用。

 $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);
 }

【讨论】:

  • 不,仍然无法正常工作。仍然返回与以前相同的错误消息。
  • 您将我的代码粘贴到您的 fbmessanger 页面了吗?并用你的代币换了我的代币?
  • 是的,准确发布并将“您的应用令牌”更改为“token_my_token”,并将“令牌”更改为称为页面访问令牌的长字符串。这是正确的吗?
  • 嗯,你为你的应用创建了页面吗?
  • 是的,已经在现有页面以及带有新应用的全新页面上尝试过。
【解决方案2】:

你能试试我的 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设置等。

问题可能在 Facebook 方面,他们在过去几天遇到了一些问题......

【讨论】:

    【解决方案3】:

    在您的 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。

    【讨论】:

      猜你喜欢
      • 2016-08-03
      • 2020-01-30
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 2017-12-16
      • 2016-11-15
      • 2018-03-08
      • 1970-01-01
      相关资源
      最近更新 更多