简体   繁体   English

NEXMO:通过 nexmo 向 facebook 发送消息时出现内容类型错误

[英]NEXMO: Content type error while sending a message to facebook via nexmo

Hullo Geeks, am trying to intergrate nexmo facebook-messager massaging api to my application using PHP However, after submiting my data, i get this error Hullo Geeks,我正在尝试使用 PHP 将 nexmo facebook-messager 按摩 api 集成到我的应用程序但是,在提交我的数据后,我收到此错误

{"title":"Content type 'application/x-www-form-urlencoded' not supported for bodyType=com.nexmo.chatapp.sandbox.messages.Message"} {"title":"bodyType=com.nexmo.chatapp.sandbox.messages.Message 不支持内容类型 'application/x-www-form-urlencoded'"}

Here is the code这是代码

$message = array(
            "from" => array("type" => "messenger", "id" => "107XXXXXXXX"),
            "to" => array("type" => "messenger", "id" => $FB_RECIPIENT_ID),
            "message" => array("content" => array(
                "type" => "text",
                "text" => "This is a Facebook Messenger Message sent from the Messages API. Ashan, please enjoy"
            )
            )
        );

    $message = json_encode($message);

    //echo $message; die();
    $ch = curl_init();
    $url = "https://messages-sandbox.nexmo.com/v0.1/messages";
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_USERNAME, "7af532c1:lWk9QFKaaGFgLz6u");
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, "Content-Type: application/json");
    curl_setopt($ch, CURLOPT_HEADER, "Accept: application/json");
    curl_setopt($ch, CURLOPT_NOBODY, FALSE); // remove body

    curl_setopt($ch, CURLOPT_POSTFIELDS, $message);

    $head = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

My goal is to send a message to messenger through nexmo API.我的目标是通过nexmo API向Messenger发送消息。

The error has been with the headers sent along with the content.错误出现在与内容一起发送的标头上。 As the error was,由于错误是,

{"title":"Content type 'application/x-www-form-urlencoded' not supported for bodyType=com.nexmo.chatapp.sandbox.messages.Message"} The HTTP HEADERS where not defined. {"title":"bodyType=com.nexmo.chatapp.sandbox.messages.Message 不支持内容类型 'application/x-www-form-urlencoded'"} HTTP HEADERS 未定义。 thus default Content type 'application/x-www-form-urlencoded applied因此默认内容类型 'application/x-www-form-urlencoded 已应用

Solved the issue by adding this line of code通过添加这行代码解决了这个问题

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Accept: application/json"));
        //curl_setopt($ch, CURLOPT_HTTPHEADER, "Accept: application/json");

For NEXMO uses JSON type data.对于 NEXMO,使用 JSON 类型数据。 Now am up in the air现在我在空中

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

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