【问题标题】:Facebook Messenger Webhook JSON file_get_contents emptyFacebook Messenger Webhook JSON file_get_contents 为空
【发布时间】:2017-06-27 22:10:39
【问题描述】:

我正在开发一个机器人 (Facebook Messenger),但它没有响应,'file_get_contents' 值返回 null/empty。

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

首先我向现有页面添加了一个应用程序,但它不起作用。 所以我创建了一个新页面和一个新应用程序,但错误仍然存​​在。 我认为这可能是页面或应用程序中的设置。

可能有什么问题?

<?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\r\n" .
                "Accept: application/json\r\n"
    )
);

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

?>

【问题讨论】:

  • 我也有同样的问题,空文件请求。你找到解决方案了吗?

标签: php facebook webhooks messenger


【解决方案1】:

您能否尝试精简代码并检查以下内容:

  1. 如果您的网络服务器访问日志显示来自 facebook 的点击

  2. 如果有任何错误(您也可以使用简单的 curl cli 命令进行检查。

  3. 如果 logs.txt 显示任何内容。

<?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);

?>

【讨论】:

  • 我尝试了您的建议并开始使用 nGrok 从应用程序中获取结果/返回。但是页面什么也没有返回,没有数据,或者错误。
  • 您在 Web 服务器的访问日志和错误日志中看到了什么?我不确定它如何通过 nGrok 工作。您可能想尝试使用真实的服务器。最简单的方法是创建一个 AWS 实例,使用 Let's Encrypt(免费)设置 SSL,您将在 30 分钟内获得结果。
猜你喜欢
  • 2016-11-09
  • 2016-10-12
  • 2017-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-16
相关资源
最近更新 更多