简体   繁体   English

Facebook Messenger Webhook JSON file_get_contents为空

[英]Facebook Messenger Webhook JSON file_get_contents empty

I'm developing a bot (Facebook Messenger) and it is not responding, the 'file_get_contents' values returns null/empty. 我正在开发一个机器人(Facebook Messenger),它没有响应,'file_get_contents'值返回null / empty。

$update_response = file_get_contents("php://input"); $ 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\\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]); } ?> 

Can you try a stripped down code and check the following: 您可以尝试一个精简代码并检查以下内容:

  1. If your web server access logs shows a hit from facebook 如果您的Web服务器访问日志显示来自Facebook的命中

  2. If there are any errors (you can check this with a simple curl cli command too. 如果有任何错误(您可以使用简单的curl cli命令检查此错误。

  3. If logs.txt shows anything. 如果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); ?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM