繁体   English   中英

我不明白如何使用 PHP 配置 REST API Sinch

[英]I do not understand how to configure the REST API Sinch with PHP

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

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

谢谢,我用了翻译器

您声称您正在发布 JSON:

 "Content-Type: application/json",

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

 http_build_query($data)

文档说:

生成 URL 编码的查询字符串

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

您需要询问您的 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);


?>

解决方案:

<?
$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”是密钥和帐号

我使用 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);
    }
}

暂无
暂无

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

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