繁体   English   中英

Facebook Messenger Bot-PHP cURL消息

[英]Facebook Messenger Bot - PHP cURL messages

在过去的几天里,我一直在摆弄Facebook Messenger平台,但遇到了问题。 PHP一直是主要语言。

成功地,我已经能够通过纯文本在系统中实现几个API。 (见下图)

在此处输入图片说明

系统外观如下:

$input = json_decode(file_get_contents('php://input'), true);
$senderId = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
$answer = "I don't understand that. Is that another language? Type 'hi' to get started.";

if($message == "hi") {
    $answer = "Yo!";
}

如果您不熟悉,所有这些都来自Facebook Messenger入门

我现在想要做的是通过cURL将图像传递到JSON。 这是我不熟悉的东西,但是找到了两个有用的资源来帮助我完成此任务。 使用PHP cURL发布JSON数据从多维数组创建嵌套列表

结果如下:

if($message == "test") {
$data = array("message" => array("attachement" => array('"type" => "image"'),"payload" => array('"url" => "http://example.com"')));                                                                    
$data_string = json_encode($data);                                                                                   

$ch = curl_init('https://graph.facebook.com/v2.6/me/messages?access_token=TOKEN_GOES_HERE');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                               
$answer = curl_exec($ch);
}

这是我收到的回复: 在此处输入图片说明

我肯定知道,cURL不能正确地提取参数。 虽然,我对cuRL的了解有限,但还是有其他建议。 我的问题是,我怎么还能实现这一目标? 我希望能够使用PHP通过JSON将图像传递到Messenger中。

我认为您的发布请求工作正常,但是由于错误,您没有传递整个json数据。

以下是图像通用消息的外观,您将接收者放在数据的什么位置?

{
  "recipient":{
    "id":"USER_ID"
  },
  "message":{
    "attachment":{
      "type":"image",
      "payload":{
        "url":"https://petersapparel.com/img/shirt.png"
      }
    }
  }
}

参考: https : //developers.facebook.com/docs/messenger-platform/send-api-reference#guidelines

暂无
暂无

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

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