php - Facebook Messenger 机器人不向发件人发送消息

标签 php facebook facebook-graph-api curl bots

我在关注这个 Tutorial使用 php 创建一个 Facebook Messenger 机器人

一切正常,除了发件人没有收到我的消息。

这是我的代码

<?php
    $access_token =""; //Token
    $verify_token = ""; //Verify Token
    $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 = "Huh? You are talking to me?";
    $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);
        }
?>

我可以收到消息并看到它,但是用户没有收到我的消息。

最佳答案

这是通过在//Execute the request 之前添加以下代码行来解决的

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

希望对您有所帮助。

关于php - Facebook Messenger 机器人不向发件人发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41751359/

相关文章:

PHP UFT8 编码 Internet Explorer 问题

Facebook sharer.php,如何拥有多个 og :image tags?

facebook - 从 Facebook 页面检索所有消息

android - 从开放图谱用户获取 Facebook 好友

php - 如何从某些 HTML 中删除所有格式

php - 依赖注入(inject)还是范围解析运算符?

php - Wamp 服务器不显示我的本地网站

ios - 在 swift ios 10 上使用 Facebook 登录时浏览器页面空白

使用访问 token 时 Java facebook api oAuthException

php - Facebook API : Get user profile details