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

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

极客们好,我正在尝试使用 PHP 将 nexmo facebook-messager 按摩 api 集成到我的应用程序中 然而,在提交我的数据后,我得到了这个错误

{"title":"Content type 'application/x-www-form-urlencoded' not supported for bodyType=com.nexmo.chatapp.sandbox.messages.Message"}

这是代码

$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);

我的目标是通过 nexmo 向 Messenger 发送消息 API。

与内容一起发送的 headers 出现了错误。 由于错误,

{"title":"Content type 'application/x-www-form-urlencoded' not supported for bodyType=com.nexmo.chatapp.sandbox.messages.Message"} The HTTP HEADERS where not defined. thus default Content type 'application/x-www-form-urlencoded applied

通过添加这行代码解决了问题

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

对于 NEXMO 使用 JSON 类型数据。 现在悬而未决