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
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 will ask you a series of questions - you can leave these all to the defaults as we’re only doing the bare minimum of effort. The process should create your composer.json
file. So, we can add the Vonage PHP SDK now:
composer require vonage/client
Now, to create our script.
touch send-sms.php
Open the new PHP file and make sure Composer’s autoload is pulled in:
<?php
require_once './vendor/autoload.php';
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);
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
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();
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
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.
Have a question or something to share? Join the conversation on the Vonage Community Slack, stay up to date with the Developer Newsletter, follow us on X (formerly Twitter), and subscribe to our YouTube channel for video tutorials. Stay connected, share your progress, and keep up with the latest developer news, tips, and events!
Additional Resources
Sending SMS from PHP with Failover: The Cupcake Bakery
Type Safety Done Right - PHP Array Hacking
Top comments (0)