How to Use Newline in SMS Message When Sending via HTTP SMS API
We have been receiving the lot of enquiries to insert newline in the SMS message while sending SMS using HTTP SMS API.
So today, we have decided to post some sample code using PHP CURL method.
PHP CURL Sample code as follow:
<?php
$target_url = 'https://enterprise.smsgatewaycenter.com/SMSApi/rest/send';
$msg = "Hello Oscar,\r\nYour order dated 22/08/2016\r\nAmount INR 5000 Cash On Delivery\r\nYour Package will be delivered by tomorrow between 3PM to 7PM.\r\nRegards SMSGatewayCenter.com";
//single number api
$post = array(
'userId' => 'myusername',
'password' => 'xxxxxxx',
'sendMethod' => 'simpleMsg',
'msgType' => "text",
'format' => "json",
'mobile' => "98xxxxxxxx",
'msg' => $msg,
'senderId' => "SGCSMS");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
Message on Phone Screenshot:
Frequently Asked Questions on this topic:
Should I urlEncode the post data?
If you are using CURL post method, then CURL will encode the data for you. You just need to use raw field data into the fields array and then initiate the call. If you use the data as an array then the data will be automatically urlencoded by CURL and, if at all passed as a string, then you need to urlencode first before making the call.
CURLOPT_POST v/s CURLOPT_POSTFIELDS
CURLOPT_POST
TRUE, to do a regular HTTP POST. This POST is the normal application/x-www-form-urlencoded kind, most commonly used by HTML forms.
CURLOPT_POSTFIELDS
The full data to post in an HTTP “POST” operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ‘;type=mimetype’. This parameter can either be passed as a urlencoded string like ‘para1=val1¶2=val2&…’ or as an array with the field name as key and field data as value. If the value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix.
More info here
Hope this helps to many of our clients and our visitors. Feel free to comment on anything.