【发布时间】: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