How to send SMS using PHP
In this tutorial, we will learn how to send SMS using PHP. There are many SMS API service providers like Twilio, Nexmo, MSG91, Text local that you can use to send SMS using the PHP programming language.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php //requested parameters $data =array( 'username'=>'phpgurukul', 'apikey'=>'czczx423ssdfsd', 'message'=>'Hello World!', 'mobilenumber'=>'1234567890', 'signature'=>'PHPG'); //Generate URL-encoded query string $datastring= http_build_query($data); //initialising the Curl library $ch = curl_init('http://api.example.com/http/sendsms?'); //Set an option for a cURL transfer curl_setopt($ch,CURLOPT_POST,true); curl_setopt($ch,CURLOPT_POSTFIELDS,$datastring); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); $result =curl_exec($ch); curl_close($ch); |
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 a HTTP “POST” operation. This parameter can either be passed as a URL-encoded string like ‘para1=val1¶2=val2&…’ or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. Files can be sent using CURLFile, in which case value must be an array.
CURLOPT_SSL_VERIFYPEER
false
to stop cURL from verifying the peer’s certificate. Alternate certificates to verify against can be specified with the CURLOPT_CAINFO
option or a certificate directory can be specified with the CURLOPT_CAPATH
option.
CURLOPT_RETURNTRANSFER
true to return the transfer as a string of the return value of curl_exec() instead of outputting it directly.
curl_exec
Perform a cURL session
curl_close
Close a cURL session
You can change the requested parameters as per your SMS API.