dqzuo0327
dqzuo0327
2017-08-13 16:58
浏览 108
已采纳

来自facebook messenger机器人的空消息

I followed the guide to build a facebook messenger bot from here

but in my software I receive alway an empty response.

here my bot code:



    define('PAGE_TOKEN',"xxx");
    define('VERIFY_TOKEN',"xxx");

    if(isset($_GET['hub_mode']) && isset($_GET['hub_challenge']) && isset($_GET['hub_verify_token'])) {
        if($_GET['hub_verify_token']==VERIFY_TOKEN && $_GET['hub_mode']=='subscribe') {
            echo $_GET['hub_challenge'];
        }
    }

    $input = json_decode(file_get_contents('php://input'), true);
    // Get the Senders Graph ID
    $sender = $input['entry'][0]['messaging'][0]['sender']['id'];
    // Get the returned message
    $message = $input['entry'][0]['messaging'][0]['message']['text'];

    error_log($input);
    error_log($sender);
    error_log($message);

the verify phase is fine, but when i send a messagge to my bot in my error_log file I found only empty values:



    [Sun Aug 13 17:35:49.617919 2017] [:error] [pid 22501] [client 173.252.124.11:18834]
    [Sun Aug 13 17:35:49.617951 2017] [:error] [pid 22501] [client 173.252.124.11:18834]
    [Sun Aug 13 17:35:49.617972 2017] [:error] [pid 22501] [client 173.252.124.11:18834]

Here there is also my access_log:



    173.252.124.30 - - [13/Aug/2017:17:35:48 +0200] "POST /webhook HTTP/1.1" 301 3621 "-" "-"
    173.252.124.11 - - [13/Aug/2017:17:35:49 +0200] "GET /webhook/ HTTP/1.1" 200 328 "-" "-"

so why the $input variable is alway empty?

图片转代码服务由CSDN问答提供 功能建议

我按照指南从这里

但在我的软件中,我总是收到一个空的回复。

这里我的机器人代码:

  
 
define('PAGE_TOKEN',“xxx”); 
define('VERIFY_TOKEN'  ,“xxx”); 
 
 if(isset($ _ GET ['hub_mode'])&& isset($ _ GET ['hub_challenge'])&& isset($ _ GET ['hub_verify_token'])  ){
 if($ _ GET ['hub_verify_token'] == VERIFY_TOKEN&& $ _GET ['hub_mode'] =='subscribe'){
 echo $ _GET ['hub_challenge']; 
} 
  } 
 
 $ input = json_decode(file_get_contents('php:// input'),true); 
 //获取发件人图ID 
 $ sender = $ input ['entry'] [0] ['  messaging'] [0] ['sender'] ['id']; 
 //获取返回的消息
 $ message = $ input ['entry'] [0] ['messaging'] [0] ['  message'] ['text']; 
 
 error_log($ input); 
错误 _log($ sender); 
 error_log($ message); 
 
   
 
 

验证阶段很好,但是当我向我的机器人发送一个消息时 error_log文件我发现只有空值:

  
 
 [Sun Aug 13 17:35:49.617919 2017] [:error] [pid 22501] [client 173.252.124.11  :18834] 
 [Sun Aug 13 17:35:49.617951 2017] [:错误] [pid 22501] [客户173.252.124.11:18834] 
 [Sun Aug 13 17:35:49.617972 2017] [:error] [  pid 22501] [客户端173.252.124.11:18834] 
nn  
nn

这里还有我的access_log:

  
 
 173.252.124.30  -   -  [13 / Aug / 2017:17:35:48 +0200]“POST / webhook HTTP / 1.1”301 3621“ - ”“ - ”
 173.252.124.11  -   -  [13  / Aug / 2017:17:35:49 +0200]“GET / webhook / HTTP / 1.1”200 328“ - ”“ - ”
 
   
 
 

所以 为什么 $ input 变量一直是空的?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doupai8095
    doupai8095 2017-08-14 07:29
    已采纳
    173.252.124.30 - - [13/Aug/2017:17:35:48 +0200] "POST /webhook HTTP/1.1" 301 3621 "-" "-"
    173.252.124.11 - - [13/Aug/2017:17:35:49 +0200] "GET /webhook/ HTTP/1.1" 200 328 "-" "-"
    

    Your server answers the POST request with a 301 redirect, so that the client (Facebook) has to make a GET request next, and therefor you see no more POST data.

    Seeing that the first URL is /webhook, and gets redirected to /webhook/, this is likely just an automatic trailing slash redirect, and should be fixed once you specify the correct URL with the trailing slash in your app settings to begin with.

    点赞 评论

相关推荐