Andrés O.
Andrés O.

Reputation: 13

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

I am very new using cURL in PHP, I would like to use this API to send SMS but when I do all the tests I get the following error:

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

I have reviewed my code and I do not understand what is really happening:

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

I have reviewed the official documentation and I do not know what I am doing wrong, the official documentation:

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

Thanks, i used a translator

Upvotes: 1

Views: 602

Answers (4)

Erich García
Erich García

Reputation: 1874

My solution with 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);
    }
}

Upvotes: 0

Andr&#233;s O.
Andr&#233;s O.

Reputation: 13

Solution:

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

-Add an additional array () in "to" -converted Json with the json_encode function -Posdata: The "xxx" were the key and the account number

Upvotes: 0

Nancy Moore
Nancy Moore

Reputation: 2470

You will need to ask your SMS provider if you will use the username and password associated to your sms account along with the api.

Try this code below

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


?>

Upvotes: 0

Quentin
Quentin

Reputation: 943142

You claim you are POSTing JSON:

"Content-Type: application/json",

But this is how you generate the data you POST:

http_build_query($data)

And the documentation for that says:

Generate URL-encoded query string

You need to send actual JSON. Use json_encode.

Upvotes: 2

Related Questions