In Context

The next-generation communications blog from Nexmo

< Back

Sending SMS Messages with PHP

September 20, 2017 Published by

The Nexmo SMS API allows you to send an SMS and receive an SMS by interacting with a simple, HTTP based API. You can read the docs if you’re interested but there’s no need to thanks to the Nexmo PHP client, which handles talking to the API for you.

Prerequisites

You’ll need PHP installed before working through this post. I’m running PHP 7.1, but the code here should work on PHP 5.6 and above. You’ll also need Composer to install the Nexmo PHP client.

Getting your API keys

Before we write any code, we’ll need to purchase a virtual phone number from Nexmo and get our API key from our dashboard (you’ll need to create an account if you’re not an existing user)

Sending an SMS with nexmo-php

The first thing we need to do is install nexmo/client using composer. This will install the Nexmo PHP client and all of it’s dependencies.

Once this completes, we’re only three lines of code away from sending an SMS using PHP. We’re going to create a file called send-sms.php, provide our API key and secret, create a Text with to, from and a message then call the send function. That’s really all there is to it.

Go ahead and create send-sms.php now with the following contents. Don’t forget to replace NEXMO_TO_NUMBER with your own phone number, making sure that starts with a country code e.g. 14155550100 rather than (415) 555-0100. You’ll also need to change NEXMO_FROM_NUMBER to set your SenderID. This generally has to be a Nexmo number, but in some coutries you can use an alphanumeric sender ID. You can read more about sender IDs in the Nexmo knowledge base and purchase a number, if required.

Save this file, then run it with php send-sms.php. You will receive a text message shortly to the number you provided in NEXMO_TO_NUMBER. Our script will also output the response from Nexmo, which contains information about the message you just sent along with how much credit you have remaining on your account.

Sending an SMS via a PHP API with SlimPHP

Whilst sending an SMS with three lines of code is pretty awesome, it’s not very flexible. Wouldn’t it be great if we could change the NEXMO_TO_NUMBER and message that we send dynamically by making calls to an API? Let’s do just that!

We’re going to be using SlimPHP to power our API. This means that the first thing we need to do is require it with composer. This will download and install Slim and all of it’s dependencies.

Once it’s installed, we can create an API that responds to our requests. Create a file named index.php with the following contents:

This creates a new instance of SlimApp and registers a route that we can call. This allows us to make a POST request to /sms/{number} and it’ll send a response back to us (but it won’t send an SMS yet!). Save your file and start up PHP‘s built in server by running php -S localhost:8000 -t ..

We’re going to make a HTTP POST request to http://localhost:8000/sms/<NEXMO_TO_NUMBER> using an application called Postman.

When we click on Send, we should get a response that says “Sending an SMS to [number]”. This lets us know that our Slim application is running correctly and that we can move on to building our SMS functionality.

Make a HTTP request with Postman

As we already have our route set up, we can take our existing code that sends an SMS and drop it in to place.

At this point, we can send a message to any phone number we like. There’s one last change to make though, as not everyone is interested in “How to send an SMS with PHP”. Let’s make that message customizable.

To customize the message, we need to read data from the request to our API. We can use the $request->getParsedBody() method to return the payload of the incoming request as an array. We could use key to contain our data, but we’re going to use text as that’s the parameter name that the Nexmo API uses. In addition to reading the request body, we’re going to perform some input validation to make sure that text has been provided before passing it in to our Text object.

This is all we need to send an SMS with PHP via Nexmo. Give it a go yourself via Postman.

We’re all done! By using SlimPHP we quickly bootstrapped an API that could receive requests and use the data contained in them to send an SMS to any phone number. Whilst this code works, there’s still a lot of additional validation that could be added, and we could make our Nexmo credentials and NEXMO_FROM_NUMBER configurable. Why not give it a go yourself?

Nexmo Developer Logo
@NexmoDev

Tags: , ,

Categorised in: , , ,

This post was written by