How to send an SMS with PHP – a simple guide
More and more businesses are using SMS as a way of communicating with their customers.
Despite SMS being nearly thirty years old and incredibly basic, A2P SMS volumes are predicted to rise by at least 4.4% every year until 2027, according to Juniper research.
Most companies are looking for a fast and simple integration that will be hassle free, reliable and low cost.
For many that will mean using PHP as the coding language for SMS integration.
What is PHP?
PHP is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.
Why is PHP so popular for sending SMS?
PHP is often the go to coding language for sending SMS. There are a few reasons why it’s so popular.
1 PHP is easy to learn
PHP is really easy to get to grips with. Compared to other languages there are fewer challenging concepts to grapple with.
No other language is as easy for a beginner to get visible results
Bill Karwin– SQL developer, consultant, trainer
2.PHP is flexible and forgiving
Minor coding mistakes don’t cause the code to stop working, so it’s perfect if you need to get a proof of concept up and running fast.
Quick and Dirty is the default.
Bob Fanger, Developer
3.PHP is well documented
There are countless tutorials, training programmes and video courses on PHP.
Whatever you’re trying to do, there will be a wealth of resources available.
4. PHP is free
Since it began back in 1995, PHP has always been a free, collaborative project.
3 simple steps to integrate SMS with PHP
It’s a really simple process to send SMS with PHP.
Here are the 3 simple steps you need to take.
1.Set up a free account
Your free SMS API account will come with 50 free text credits, so you test us at no cost.
2.set up authentication
Generate an API Key & Secret and use these to generate a JSON Web Token.
The JSON web token is passed in the ‘Authorization’ header with all calls to the base URL https://api.thesmsworks.co.uk/v1
3. Use our simple methods to send and receive messages
There are 3 options.
1. Choose from our SDKs
2. Simple integration with cURL using our PHP code snippets
3. Make regular HTTP POST and GET calls to our SMS API end-points
SMS PHP documentation
All the documentation for sending SMS with PHP, along with sample code can be found on our developers’ page. Developers – SMS API method docs, Download SDKs | The SMS Works
Sample PHP code to send an SMS in PHP
The following PHP code can be used to send texts using The SMS Works SMS API.
<!?php
/* requires SMS Works PHP SDK */
require_once(__DIR__ . ‘/vendor/autoload.php’);
// Configure API key authorization: JWT
$config = \Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey(‘Authorization’, ‘Your JSON Web Token goes here’);
$apiInstance = new \Swagger\Client\Api\MessagesApi(
new \GuzzleHttp\Client(),
$config
);
$sender = ‘MyCompany’;
$destination = ‘07777777777’;
$content = ‘My super awesome message’;
$schedule = ”;
$data = array(“sender”=>$sender,”destination”=>$destination,”content”=>$content,”schedule”=>$schedule);
$sms_message = new \Swagger\Client\Model\Message($data);
try {
$result = $apiInstance->sendMessage($sms_message);
print_r($result);
} catch (\Swagger\Client\ApiException $e) {
echo ‘Exception when calling MessagesApi->sendMessage: ‘, $e->getMessage(), PHP_EOL;
}
?>
Parameter definitions for sending an SMS in PHP
sender
The sender of the message. Should be no longer than 11 characters for alphanumeric or 15 characters for numeric sender ID’s. No spaces or special characters. Read more details about SMS sender ID. https://thesmsworks.co.uk/sms-sender-id
destination
The mobile number or MSISDN that you’re sending to using PHP. Numbers can be either in UK format, 07900123456 or International format, 447900123456. Do not start numbers with the + sign.
content
The message itself. A standard text is 160 characters, including spaces. You can send messages of up to 1280 characters in length. This article explains more about message length when sending a message in PHP https://thesmsworks.co.uk/maximum-length-sms-text-message
deliveryreporturl
The url to which we should POST delivery reports to for this message. If none is specified, we’ll use the global delivery report URL that you’ve configured on your account page.
schedule
The schedule date format should match the JavaScript date format, which is the same as the ISO 8601 syntax. E.g Sun Sep 03 2017 15:34:23 GMT+0100 (BST)
tag
You can add a tag to each message which can be useful for identifying all numbers that were sent a particular message in PHP or are part of the same campaign. The tag can be alphanumeric and contain spaces. The maximum number of characters is 280, including spaces.
ttl
Time To Live. The optional number of minutes before the message is deleted, defined as an integer. Optional. Omit this parameter to prevent delivery report deletion.
responseemail
An optional array of email addresses to forward responses to this specific message to. An SMS Works Reply Number is required to use this feature.
metadata
Include an array of metadata objects on messages you send with the SMS Works API. Any supplied metadata will be stored on delivery reports and included in the API response sent for calls to the /message/send end-point.
Metadata must be provided with ‘key’ and ‘value’ parameters, as shown in the following example:
“metadata”: [
{ “key”:”branchGUID”, “value”: “e60e5f31-etc”},
{ “key”:”messageQueueID”, “value”: “9648523”}
]
This will be returned in the delivery report formatted as follows:
“metadata”: {
“branchGUID”: “e60e5f31-etc”,
“messageQueueID”: “9648523”
}
validity
The optional number of minutes to attempt delivery before the message is marked as EXPIRED. The default is 2880 minutes. Integer.
How much does it cost to send a text in PHP?
There are probably over 100 SMS providers that allow you to send text messages in PHP.
We’ve created a list of over 100 SMSAPI providers here.
Apart from offering a competitive price, you should select a company that’s based in your country or the country in which you wish to send messages using PHP
There’s quite a wide range of SMS pricing, from about 7 pence to just under 3 pence, excluding VAT
Here’s a list of some of the providers that allow you to send text messages in PHP.
SMS API Provider | Cost per text in pence |
The SMS Works | 3.15p |
Esendex | 7.0p |
Text Local | 4.9p |
Clockwork | 4.0p |
Dragon Fly | 3.9p |
Bulk SMS | 4.5p |
Kapow | 6.25p |
Intellisms | 4.4p |
Fast SMS | 3.5p |
Click SMS | 3.7p |
SendMode | 3.4p |
FireText | 4.9p |
Text Anywhere | 6.4p |
Text Marketer | 3.3p |
Red SMS | 4.5p |
Text Tank | 3.5p |
Green Text | 3.6p |
2sms | 10p |
Think Message | 3.9p |
Text Magic | 4.0p |
The SMS Works operates a sliding scale of SMS message costs, with message rates starting at just 2.99 pence + VAT per text.
We’ve also created an SMS price comparison tool that allows you to compare SMS prices for different providers.
Support for sending texts in PHP
We’re here to help.
So whatever your query, please get tin touch. We do support by email, so any queries, please contact us on [email protected]
We’re really responsive and aim to get back to you within the hour, often we’re even quicker than that.
Related articles
SMS API documentation The home for our complete set of SMS API documentation, containing code snippets in PHP and all other popular languages.
Virtual mobile numbers Do you need to be able to receive replies to your outbound SMS? This article explains how it works and what’s involved.
14 things to check before your SMS API integration goes live Once you’ve set up your SMS service using PHP, here are some important things that you need to check before it goes live. (Obviously this only applies if you’ve used The SMS Works for your integration.)