doutu2017 2017-06-27 22:10
浏览 122

Facebook Messenger Webhook JSON file_get_contents为空

I'm developing a bot (Facebook Messenger) and it is not responding, the 'file_get_contents' values returns null/empty.

$update_response = file_get_contents("php://input");

First I added an application to an existing page, it did not work. So I created a new page and a new application, but the error remained. I thought it might be a setting on the page or in the application.

What can be wrong?

<?php

require('parser.php');

define('BOT_TOKEN', 'FACEBOOK TOKEN');
define('VERIFY_TOKEN', 'MY TOKEN');
define('API_URL', 'https://graph.facebook.com/v2.6/me/messages?access_token='.BOT_TOKEN);

$hub_verify_token = null;

function processMessage($message) {
  // processa a mensagem recebida
  
  $sender = $message['sender']['id'];
  $text = $message['message']['text'];//texto recebido na mensagem
  
  if (isset($text)) {
        if ($text === "Mega-Sena") {
          sendMessage(array('recipient' => array('id' => $sender), 'message' => array("text" => getResult('megasena', $text))));
        } else if ($text === "Quina") {
          sendMessage(array('recipient' => array('id' => $sender), 'message' => array("text" => getResult('quina', $text))));
        } else if ($text === "Lotomania") {
          sendMessage(array('recipient' => array('id' => $sender), 'message' => array("text" => getResult('lotomania', $text))));
        } else if ($text === "Lotofácil" || $text === "Lotofacil") {
          sendMessage(array('recipient' => array('id' => $sender), 'message' => array("text" => getResult('lotofacil', $text))));
        } else {
          sendMessage(array('recipient' => array('id' => $sender), 'message' => array('text' => 'Olá! Eu sou um bot que informa os resultados das loterias da Caixa. Será que você ganhou dessa vez? Para começar, digite o nome do jogo (Lotomania, Quina, Mega ou Lotofácil) para o qual deseja ver o resultado.')));
        }
  } 
}

function sendMessage($parameters) {
  $options = array(
  'http' => array(
    'method'  => 'POST',
    'content' => json_encode($parameters),
    'header'=>  "Content-Type: application/json
" .
                "Accept: application/json
"
    )
);

$context  = stream_context_create( $options );
file_get_contents(API_URL, false, $context );
}

if(isset($_REQUEST['hub_challenge'])) {
    $challenge = $_REQUEST['hub_challenge'];
    $hub_verify_token = $_REQUEST['hub_verify_token'];
}
if ($hub_verify_token === VERIFY_TOKEN) {
    echo $challenge;
}

$update_response = file_get_contents("php://input");

//-----VERIFY LOG-----//
$fh = fopen("log.txt", "w+");
fwrite($fh, $update_response);
fclose($fh);

$update = json_decode($update_response, true);

if (isset($update['entry'][0]['messaging'][0])) {
  processMessage($update['entry'][0]['messaging'][0]);
}

?>

</div>
  • 写回答

1条回答 默认 最新

  • dtnrsmi824877 2017-07-02 10:59
    关注

    Can you try a stripped down code and check the following:

    1. If your web server access logs shows a hit from facebook

    2. If there are any errors (you can check this with a simple curl cli command too.

    3. If logs.txt shows anything.

    <?php
    
    $hub_verify_token = null;
    if(isset($_REQUEST['hub_challenge'])) {
        $challenge = $_REQUEST['hub_challenge'];
        $hub_verify_token = $_REQUEST['hub_verify_token'];
    }
    if ($hub_verify_token === VERIFY_TOKEN) {
        echo $challenge;
    }
    
    $update_response = file_get_contents("php://input");
    
    //-----VERIFY LOG-----//
    $fh = fopen("log.txt", "w+");
    fwrite($fh, $update_response);
    fclose($fh);
    
    ?>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器