【问题标题】:Whispir, callbacks api is not workingWhispir,回调 api 不起作用
【发布时间】:2015-04-28 22:19:42
【问题描述】:

大家都在这里使用 whispir API 吗?

我正在尝试使用他们的回调功能,但它不起作用。根据他们的文档http://developer.whispir.com/docs/read/Whispir_API_Callbacks 回调是通过发送 http 请求触发的

HTTP 1.1 POST http://api.whispir.com/messages?apikey=<yourkey>
Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxx
Content-Type: application/vnd.whispir.message-v1+xml

请注意我使用的是 laravel 4.2

private static function curl_post($url, $username, $password, $curl_data)
        {
            $options = array(
                CURLOPT_RETURNTRANSFER  => TRUE,
                CURLOPT_FOLLOWLOCATION  => TRUE,
                CURLOPT_AUTOREFERER     => TRUE,
                CURLOPT_CONNECTTIMEOUT  => 90,
                CURLOPT_TIMEOUT         => 90,
                CURLOPT_MAXREDIRS       => 10,
                CURLOPT_URL             => $url,
                CURLOPT_HEADER          => false,
                CURLOPT_HTTPHEADER      => array(
                    'Authorization: Basic cGhwZGV2YWxwaGE6cGx1c3BsdXMxMjM=', 
                    'Content-Type: application/vnd.whispir.message-v1+json'
                ),
                CURLOPT_ENCODING        => "",
                CURLOPT_USERAGENT       => "'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13')",
                CURLOPT_POST            => 1,
                CURLOPT_POSTFIELDS      => $curl_data,
                CURLOPT_SSL_VERIFYHOST  => 0,
                CURLOPT_SSL_VERIFYPEER  => false,
                CURLOPT_VERBOSE         => 1,
                CURLOPT_USERPWD         => $username . ":" . $password
            );

            $ch = curl_init();
            curl_setopt_array($ch, $options);
            $data = curl_exec($ch);     

            if(curl_errno($ch))
                  throw new Exception(curl_error($ch));

            curl_close($ch);

            return $data;
        }

        private static function sendMessage( $to, $subject, $body )
        {
            $host       = $_ENV['SMS_HOST'];
            $apikey     = $_ENV['SMS_APIKEY'];
            $username   = $_ENV['SMS_USERNAME'];
            $password   = $_ENV['SMS_PASSWORD'];

            $data = array(
                "to"            => $to,
                "subject"       => $subject,
                "body"          => $body,
                "callbackId"    => "callback"
            );

            $json = json_encode($data);
            return Smscenter::curl_post( $host.$apikey, $username, $password, $json );
        }

如有任何澄清,请发表评论。

【问题讨论】:

    标签: php api callback whisper


    【解决方案1】:

    当您使用 Whispir API 发送的消息得到响应时,会触发 Whispir 回调。

    因此,您的代码将发送消息并引用应该使用哪个回调,并且通过 Whispir 的任何响应都会触发此回调。

            $data = array(
                "to"            => $to,
                "subject"       => $subject,
                "body"          => $body,
                "callbackId"    => "callback"
            );
    

    上面这段代码是正确的。只要在 Whispir 中你已经配置了一个名为 callback 的回调。

    例如

    一旦您在 Whispir 中创建回调,并发送带有此 callbackId 的出站消息,对该消息的任何响应都会导致 Whispir 向您的 URL 触发 POST 请求。

    这个请求的正文如下:

    HTTP 1.1 POST http://mycallbackserver.com/mycallback
    Content-Type: application/json
    X-Whispir-Callback-Key: some-auth-code
    
    {
        "messageId": "ABC4857BCCF484575FCA",
        "location" : "https://api.whispir.com/messages/ABC4857BCCF484575FCA",
        "from":{
            "name":"Fred Waters",
            "mri":"Fred_Waters.528798.Sandbox@Contact.whispir.com",
            "mobile":"04xxxxxxxx",
            "email":"test@test.com",
            "voice":"03xxxxxxxx"
         },
        "responseMessage":{
            "channel":"SMS",
            "acknowledged":"09/01/15 13:22",
            "content":"Yes, I accept. Will I need to bring steel cap boots?"
        }
    }
    

    您的应用程序可以捕获此回调并对其进行解析以获取 responseMessage 内容。

    这应该在您的应用程序中启动并运行回调。

    注意事项:

    首先,如果您还提供带有“授权”参数的 CURLOPT_HTTPHEADER,我认为您不需要设置 CURLOPT_USERPWD。他们基本上做同样的事情,所以我会选择 CURLOPT_USERPWD 选项。

    其次,由于您刚刚广播了您的授权标头,我建议您将其从这篇文章中删除,并更改您的密码。

    最后,我是 Whispir.io 的产品经理,所以如果您有任何其他问题,请随时通过电子邮件发送至 support@whispir.io,我们可以为您提供帮助。

    约旦

    【讨论】:

      猜你喜欢
      • 2014-06-22
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      • 2013-11-20
      • 2018-04-08
      • 2015-11-09
      • 2012-03-19
      • 2012-10-09
      相关资源
      最近更新 更多