Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Messenger机器人不工作_Php_Bots_Facebook Messenger - Fatal编程技术网

Php Messenger机器人不工作

Php Messenger机器人不工作,php,bots,facebook-messenger,Php,Bots,Facebook Messenger,我正在尝试建立一个非常简单的机器人。我生成了一个访问令牌,在我的web服务器上创建了一个简单的index.php文件(SSL认证),设置了一个指向我的index.php文件的webhook,订阅了我的网页事件(developers.facebook.com上的所有内容),给我的机器人发了消息,但没有得到任何答复。有什么问题吗 (我已检查所有内容:两个令牌,我是管理员等) 这是我的密码: <?php $access_token = "i-filled-this-out"; $verify_t

我正在尝试建立一个非常简单的机器人。我生成了一个访问令牌,在我的web服务器上创建了一个简单的
index.php
文件(SSL认证),设置了一个指向我的
index.php
文件的webhook,订阅了我的网页事件(developers.facebook.com上的所有内容),给我的机器人发了消息,但没有得到任何答复。有什么问题吗

(我已检查所有内容:两个令牌,我是管理员等)

这是我的密码:

<?php
$access_token = "i-filled-this-out";
$verify_token = "i-also-filled-this-out";
$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;
}

$input = json_decode(file_get_contents('php://input'), true);
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
$message_to_reply = '';

$message_to_reply = 'Huh! what do you mean?';

//API Url
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token='.$access_token;
//Initiate cURL.
$ch = curl_init($url);
//The JSON data.
$jsonData = '{
    "recipient":{
        "id":"'.$sender.'"
    },
    "message":{
        "text":"'.$message_to_reply.'"
    }
}';
//Encode the array into JSON.
$jsonDataEncoded = $jsonData;
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
//Execute the request
if(!empty($input['entry'][0]['messaging'][0]['message'])){
    $result = curl_exec($ch);
}

确保您的webhook正在返回HTTP 200对facebook发送的POST请求的响应

您正在运行什么web服务器?检查日志,查看您的webhook是否正在接收来自facebook的发帖请求(并返回一个200)

测试webhook的最佳方法是使用类似postman的东西来模仿facebook的标准帖子,并检查您提供的回复