【问题标题】:I do not understand how to configure the REST API Sinch with PHP我不明白如何使用 PHP 配置 REST API Sinch
【发布时间】:2019-02-25 19:52:50
【问题描述】:

我是在 PHP 中使用 cURL 的新手,我想使用此 API 发送 SMS,但是当我进行所有测试时,我收到以下错误:

HTTP/1.1 400 Bad Request Content-Length: 0 X-Application-Context: application:production:8080

我查看了我的代码,但我不明白到底发生了什么:

$data=array('from' => '506712xxxx', 'to' => '50671xxxx', 'body' => 'Hola este es un mensaje de prueba' );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://sms.api.sinch.com/xms/v1/xxxxx/batches");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Authorization: Bearer xxxxxx",
        "Content-Type: application/json",
      ));
    $res = curl_exec($ch);
    print_r($res);
    if(curl_errno($ch))
    {
        echo 'Curl error: ' . curl_error($ch);
    }

    curl_close($ch);

我查看了官方文档,不知道自己做错了什么,官方文档:

https://www.sinch.com/docs/sms/http-rest.html

谢谢,我用了翻译器

【问题讨论】:

    标签: php api


    【解决方案1】:

    您声称您正在发布 JSON:

    "Content-Type: application/json",
    

    但这就是您生成 POST 数据的方式:

    http_build_query($data)
    

    并且文档说:

    生成 URL 编码的查询字符串

    您需要发送实际的 JSON。使用json_encode

    【讨论】:

    • 我将它传递给 JSON,正如你所说,它向我显示了这条消息 $data=array('from' => '506XXXX', 'to' => '506XXXX', 'body' => 'Hola este es un mensaje de prueba' ); Change curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 它显示了这个错误:HTTP/1.1 400 Bad Request Content-Type: application /json Content-Length: 102 X-Application-Context: application:production:8080 Date: Mon, 25 Feb 2019 23:49:04 GMT {"code":"syntax_invalid_parameter_format","text":"参数格式to' 无效;值 ''。"} 谢谢
    • 说TO号无效,对比你提供的各种例子和this page,看来是对的。
    【解决方案2】:

    您需要询问您的 SMS 提供商是否将与您的 sms 帐户关联的用户名和密码与 api 一起使用。

    试试下面的代码

    <?php
    
    //Assuming you have your sms username and password from your SMS Providers
    $yoursms_username='xxxxxx';
    $yoursms_password='xxxxx';
    
    
    $data = array(
            //'username' => $yoursms_username,
            //'password' => $yoursms_password,
            'from'  => '506712xxxx',
            'to'  => '50671xxxx',
            'body'  => 'Hello Brother whats up.');
    
    
    
    
      // Send the POST request with cURL
    $ch = curl_init('https://sms.api.sinch.com/xms/v1/xxxxx/batches');
    curl_setopt($ch, CURLOPT_POST, true);
    
    $header[ ] = "Accept: text/xml,application/xml,application/xhtml+xml,";
    $header[ ] = "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[ ] = "Cache-Control: max-age=0";
        $header[ ] = "Connection: keep-alive";
        $header[ ] = "Keep-Alive: 300";
        $header[ ] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
        $header[ ] = "Accept-Language: en-us,en;q=0.5";
        $header[ ] = "Pragma: "; // browsers keep this blank.
    
    
    
    // also tried $header[] = "Accept: text/html";
    curl_setopt ($ch,    CURLOPT_HTTPHEADER, $header);
    //curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/4.0");
    
    
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $result = curl_exec($ch); //This is the result from Textlocal
    curl_close($ch);
    
    
    if($result === false) {
    echo '<font color=red>Message sending failed...</font>';
    
    }
    
     else {
    echo '<font color=green>SMS  Message successfully Sent</font>';
    }
    
    print($result);
    
    
    ?>
    

    【讨论】:

      【解决方案3】:

      解决方案:

      <?
      $data=array('from' => 'Oso', 'to' => array('57xx'), 'body' => 'Hola este es un mensaje de prueba' );
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "https://sms.api.sinch.com/xms/v1/xxxx/batches");
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
      curl_setopt($ch, CURLOPT_POSTFIELDS,  json_encode($data));
      curl_setopt($ch, CURLOPT_HEADER, true);
          curl_setopt($ch, CURLOPT_HTTPHEADER, array(
          "Authorization: Bearer xxxxx",
          "Content-Type: application/json",
        ));
      $res = curl_exec($ch);
      print_r($res);
      //var_dump(curl_getinfo($ch));
      if(curl_errno($ch))
      {
          echo 'Curl error: ' . curl_error($ch);
      }
      
      curl_close($ch);
      ?>
      

      -在“to”中增加一个额外的数组() - 使用 json_encode 函数转换的 Json -Posdata:“xxx”是密钥和帐号

      【讨论】:

        【解决方案4】:

        我使用 laravel Guzzle 的解决方案:

        /**
         * Internacional Class for SMS sending
         */
        class Internacional
        {
            private $api_token;
            private $rest_base_url;
        
            /**
             * Initialice variables
             */
            public function __construct($api_token)
            {
                $this->api_token = $api_token;
                $this->rest_base_url = config('sms.sms_internacional_route');
            }
        
            /**
             * Send the international SMS
             */
            public function enviar_sms($to, $message)
            {
                $now = new DateTime();
                $now = $now->format(DateTime::ISO8601);
        
                $full_phone = str_ireplace("+", "", $to);
                $numbers = [$full_phone];
        
                $promo_txt = " enviado desde Bachecubano.com";
                if (strlen($message) < 120)
                    $message = $message . $promo_txt;
        
                $data = array(
                    'from' => config('sms.international_from_number'),
                    'to' => $numbers,
                    'body' => $this->utf8_superencode($message),
                    'delivery_report' => 'full',
                    'send_at' => $now,
                );
        
                $headers = [
                    'Authorization' => 'Bearer ' . $this->api_token,
                    'Content-Type' => 'application/json',
                    'Accept'        => 'application/json',
                ];
                $client = new \GuzzleHttp\Client(['headers' => $headers]);
        
                $response = $client->request('POST', config('sms.sms_internacional_route'), ['body' => json_encode($data)]);
        
                $response = $response->getBody()->getContents();
        
                return $response;
            }
        
            //Super encode this
            private function utf8_superencode($text)
            {
                return utf8_encode($text);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2013-03-04
          • 2014-12-28
          • 1970-01-01
          • 1970-01-01
          • 2016-04-05
          • 1970-01-01
          • 1970-01-01
          • 2011-04-26
          • 1970-01-01
          相关资源
          最近更新 更多