Sending SMS Messages with PHP
In this tutorial, we’re going to send SMS messages with as few lines of PHP as possible. Firstly, with a raw script, and secondly using a minimal web application framework.
Prerequisites
- PHP 8.1+
- Composer, for package management
- A Vonage API Account
To complete this tutorial, you will need a Vonage API account.
Vonage API Account
To complete this tutorial, you will need a Vonage API account. If you don’t have one already, you can sign up today and start building with free credit. Once you have an account, you can find your API Key and API Secret at the top of the Vonage API Dashboard.
Once you have an account, you can find your API Key and API Secret at the top of the Vonage API Dashboard.
How to Send Text with Raw PHP
We’re going to start by creating a new Composer project and pulling in the Vonage PHP SDK. Using the command line, add the following:
$ mkdir vonage-sms
$ cd vonage-sms
$ composer init
$ composer require vonage/client
Copy
touch send-sms.php
Open the new PHP file and make sure Composer’s autoload is pulled in:
<?php
require_once './vendor/autoload.php';
Copy
To send an SMS, we need three things:
- A set of credentials taken from the Vonage Dashboard
- A Client object
- A text to send
Given this, here are the final lines of code:
$credentials = new \Vonage\Client\Credentials\Basic(YOUR_API_KEY, YOUR_API_SECRET);
$client = new Vonage\Client($credentials);
$message = new Vonage\Messages\Channel\SMS\SMSText(
YOUR_NUMBER,
'Vonage',
'Hello from Vonage!'
);
$client->messages()->send($message);
Copy
Plug in the required details to the constant variable placeholders and hit send:
Four lines of code is pretty good! The second part is plugging this into a basic web framework so that you can POST a message instead of hard-coding it as we have above.
How to Send SMS via. Slim Framework
For the second example, we’re going to be using the Slim Framework, so install that with Composer:
$ composer require slim/slim:"4".*
$ composer require slim/psr7
Copy
The second command here installs Slim’s PSR-7 (Request Interface) implementation. We’re going to create a route that takes a POST request with a JSON body containing some text.
Here is the modified send-sms.php
file:
<?php
use Slim\Psr7\Request;
use Slim\Psr7\Response;require_once '../vendor/autoload.php';$app = \Slim\Factory\AppFactory::create();$app->post('/send', function (Request $request, Response $response) {
$rawBody = $request->getBody()->getContents();
$requestData = json_decode($rawBody);
$text = $requestData->text; $credentials = new \Vonage\Client\Credentials\Basic('232130c9', 's09IJad98fa0t9j09ad8fa90s');
$client = new Vonage\Client($credentials);
$message = new Vonage\Messages\Channel\SMS\SMSText(
YOUR_NUMBER,
'Vonage',
$text
); $client->messages()->send($message); $response->getBody()->write("Vonage sent this text!");
return $response;
});$app->run();
Copy
Instead of running this code, we’re mocking using a web platform instead, so we’re going to use PHP’s built-in webserver to fire up a development environment from the command line:
$ php -S localhost:8888 -t send-php.php
Copy
To send the message, you’ll want an HTTP tooling client that makes it easy to send POST requests — I’ve opted for Kong’s Insomnia but you could also use Postman or even a raw cURL request.
Conclusion
Now we have a route where you can send the message text to a server for it to complete the process. You can, of course, add additional keys to the JSON payload to add the from and to cellular numbers. Fancy building something out of it? Check out our Laravel integration or check out further reading on sending messages with this API in Symfony.
Additional Resources
Sending SMS from PHP with Failover: The Cupcake Bakery
Type Safety Done Right — PHP Array Hacking