When someone mentions SMS services, people tend to imagine a system of quick-messages transmission for communication between people. But in the modern world, SMS services provide high functionality useful for various tasks. Let’s have a look at some basic examples of their use:
Most of these systems use bulk SMS messaging. Messages are sent to several recipients, almost simultaneously. As a rule, messages are delivered sequentially to each recipient with a short delay (from 0.5 to 3 seconds, depending on the service or operator).
It’s possible to install such a system on a regular mobile phone if the number of recipients is between 10-20. But, if the number of recipients becomes larger (for example, for marketing purposes, they are usually more than 1000), this task is almost impossible to accomplish (due to hardware and software limitations of mobile phones).
Therefore, to implement the bulk SMS messaging, we’ll use special online services that allow creating a fast and reliable system by using their API and one of the programming languages.
One of the most popular and demanded languages for WEB programming is PHP.
Today, many services that enable creating modules for sending SMS messages to different individual sites or systems are based on PHP.
Let’s consider the website modules for creating codes or sending SMS messages using popular services: Nexmo SMS Messaging, Twilio SMS, and RapidAPI.
RapidAPI is the largest API marketplace out there. RapidAPI makes it easy to find, connect, and use APIs all in one platform. While many of the APIs found on RapidAPI’s marketplace are free, many of the SMS ones may require a subscription.
Without further ado, let’s learn how to send SMS messages using cURL-sessions in PHP.
Nexmo’s SMS Messaging API allows you to deliver text messages to mobile phone users around the globe through simple APIs. At the time of writing this article, the service had a 100% of Success Rate and 9.4/10 of Popularity Score. To begin using the Nexmo SMS API, you’ll need to subscribe to the API first. The APIs has a pay-per-use pricing model that charges you $0.0001 per request.
Nexmo SMS API is a service that provides SMS operations and enables you to scale them and solve various tasks:
However, it is only currently available to countries that don’t require a virtual number (for example, Japan).
To implement some of these functions, you need to use an endpoint at the RapidAPI’s Endpoints subsection.
To use an endpoint, you need to fill data in “Required parameters” and then test it by clicking the “Test Endpoint” button.
Description and overview of “Required” and “Optional” parameters are available at Nexmo SMS API Endpoints.
For illustration purposes, we’re not going to use all the parameters in our examples.
Header Parameters:
Required Parameters:
Optional Parameters:
Let’s define the necessary information for Nexmo SMS API as an array:
$data = [ 'from' => 'RapidAPI', 'to' => 'ХХХХХХХХХХХХ', 'text' => 'Hi! I\'m a test message from RapidAPI! Have a nice day!' ];
Next, we will create a cURL session and fill out the required “Headers parameters”:
$curl = curl_init($url . '?' . http_build_query($data)); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_HTTPHEADER, [ 'X-RapidAPI-Host: nexmo-nexmo-messaging-v1.p.rapidapi.com', 'X-RapidAPI-Key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'Content-Type: application/x-www-form-urlencoded' ]);
It is necessary to note that in the curl_init
function, we modified the requested URL and data format according to the type: “application/x-www-form-urlencoded” by using the http_build_query()
function.
After that, we have to execute the current cURL session and save the response from the server and then close this session and output the response.
$response = curl_exec($curl); curl_close($curl); echo $response . PHP_EOL;
Here it is all together:
<?php $url = 'https://nexmo-nexmo-messaging-v1.p.rapidapi.com/send-sms'; $data = [ 'from' => 'RapidAPI', 'to' => 'XXXXXXXXXXXX', 'text' => 'Hi! I\'m a test message from RapidAPI! Have a nice day!' ]; $curl = curl_init($url . '?' . http_build_query($data)); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_HTTPHEADER, [ 'X-RapidAPI-Host: nexmo-nexmo-messaging-v1.p.rapidapi.com', 'X-RapidAPI-Key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'Content-Type: application/x-www-form-urlencoded' ]); $response = curl_exec($curl); curl_close($curl); echo $response . PHP_EOL;
After executing this code in the terminal, we will get the following result:
{"message-count":"1","messages": [{"to":"'XXXXXXXXXXXX'","message-id":"XXXXXXXXXXXXXXXX","status":"0","message-price":"0.08614","network":"25503"}]}
Then the SMS message is sent to the device with the required number:
The message is delivered and the code for the SMS module is ready!
The Twilio SMS API is a service that provides SMS messages operations and allows you to scale them to solve various tasks. This service supports multiple applications and languages.
As in the previous case, you also need to make a subscription to use the Twilio SMS API. The Twilio API costs the same as the Nexmo SMS API ($0.0001/use).
The main Twilio SMS API functions are:
A detailed description and an overview of all endpoints for Twilio SMS API are available at the following link.
We’re going to use the endpoint, which is only for sending SMS messages – “POST Send SMS (Create a Message Resource)”.
Almost all “Required” parameters are the same as for Nexmo SMS API. The only difference is the appearance of the “accountSid” parameter, used for authentication in the Twilio’s SMS service.
So before using this service, you need to register using this link and get the authentication code.
Enter the required information and press “Start your free trial”.
To get the accountSid code, click on this link (or Twilio Сonsole – Settings – General).
Once we got the “accountSid” code, we’re ready to use Twilio SMS API.
Let’s define the necessary information for the Twilio SMS API as the following variables:
$accountSid = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; $url = 'https://twilio-sms.p.rapidapi.com/2010-04-01/Accounts/' . $accountSid . '/Messages.json';
$data = [ 'from' => 'RapidAPI', 'to' => 'XXXXXXXXXXXX', 'body' => 'Hi! I'm a test message from RapidAPI (Twillio)! Have a nice day!' ];
Note that the field name for SMS-message has been changed from “text” to “body”, in comparison to Nexmo SMS API.
Also, the values of the “Headers parameters”: “X-RapidAPI-Host” and “X-RapidAPI-Key” will be changed, because we are going to use another service: RapidAPI / Twilio SMS API.
curl_setopt($curl, CURLOPT_HTTPHEADER, [ 'X-RapidAPI-Host: twilio-sms.p.rapidapi.com', 'X-RapidAPI-Key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'Content-Type: application/x-www-form-urlencoded' ]);
The rest of the code (for the previous API) is unchanged. As a result, we should get the following PHP-file:
<?php $accountSid = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; $url = 'https://twilio-sms.p.rapidapi.com/2010-04-01/Accounts/' . $accountSid . '/Messages.json'; $data = [ 'from' => 'RapidAPI', 'to' => 'XXXXXXXXXXXX', 'body' => 'Hi! I'm a test message from RapidAPI (Twillio)! Have a nice day!' ]; $curl = curl_init($url . '?' . http_build_query($data)); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_HTTPHEADER, [ 'X-RapidAPI-Host: twilio-sms.p.rapidapi.com', 'X-RapidAPI-Key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'Content-Type: application/x-www-form-urlencoded' ]); $response = curl_exec($curl); curl_close($curl); echo $response . PHP_EOL;
After executing this code in the terminal, we should get this log result:
{"accountSid":"ХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХ","apiVersion":"2010-04-01","body":"Hi! I'm a test message from RapidAPI (Twillio)! Have a nice day!","dateCreated":"2019-08-14T20:21:19.000Z","dateUpdated":"2019-08-14T20:21:21.000Z","dateSent":"2019-08-14T20:21:19.000Z","direction":"outbound-api","errorCode":null,"errorMessage":null,"from":"RapidAPI","messagingServiceSid":null,"numMedia":"0","numSegments":"1","price":"0.00750","priceUnit":"USD","sid":"ХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХ","status":"delivered","subresourceUris":{"media":"/2010-04-01/Accounts/ХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХ/Messages/ХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХ/Media.json"},"to":"+ХХХХХХХХХХХХ","uri":"/2010-04-01/Accounts/ХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХ/Messages/ХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХХ.json"}
And again, the SMS message is sent to the device with the required number:
In this article, we described how to create the PHP code for sending SMS-messaging module using the Nexmo SMS API and Twilio SMS API in RapidAPI.
Comparing the received results, we can highlight the following differences for Nexmo and Twilio services:
The price of subscribing to these services in RapidAPI is about the same, so if you need simplicity and stability in the module for sending SMS-messages, select Nexmo SMS, and if you need the functionality, then choose Twilio SMS.
We're thrilled to announce the latest update to the Rapid Enterprise API Hub (version 2024.3)!…
Are you curious about what your API consumers are searching for? Is your Hub effectively…
The RapidAPI team is excited to announce the February 2024 update (version 2024.2) for the…
This January's release brings exciting features and improvements designed to empower you and your developers.…
Rapid API is committed to providing its users with the best possible experience, and the…
In today's fast-paced digital world, APIs (Application Programming Interfaces) have become the backbone of modern…
View Comments
It is very easy to integrate your website with your sms portal. Let me guide you with the process-
Step 1: Configure your website and create an SMS sender web application
Step 2: Let the webserver be able to manage the SMS web application
Step 3 Send a test SMS message from your website
Step 4: That's it, you are done.
I Still can not send sms to abroad phone number.
Example I test send sms to Cambodia but it 's not valid.