Sending SMS Messages with PHP
最后更新 January 29, 2024

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 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';
</code-->

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:

Screenshot of a phone device showing the recieved SMSOur SMS has been sent!

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.

Screenshot of Insomnia sending a POST requestAPI Tooling is super helpful

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

Scrub Up! Cleaning Your PHP Application With PHPStan

Vonage PHP SDK

James SecondeSenior PHP Developer Advocate

A trained actor with a dissertation on standup comedy, I came into PHP development via. the meetup scene. You can find me speaking and writing on tech, or playing/buying odd records from my vinyl collection.

Ready to start building?

Experience seamless connectivity, real-time messaging, and crystal-clear voice and video calls-all at your fingertips.

Subscribe to Our Developer Newsletter

Subscribe to our monthly newsletter to receive our latest updates on tutorials, releases, and events. No spam.