How To Send Text Message From Website Using Nexmo API And PHP

Are you looking to send text message from your PHP powered website? By sending text messages, you can notify users about new events, deals or coupons. In this article, we show you how to send SMS from the website using the Nexmo API and PHP.

Nexmo provides a service for SMS, voice and phone verifications.

To get started with Nexmo for your application, on the sign up they provided you little credit amount for testing. You don’t need to pay in front for using their services. If you are happy with their end result, then you can buy their packages whichever is suitable for you.

Get Started

Nexmo provides SDK and libraries in all popular languages. Using these libraries we can interact with the Nexmo APIs.

In our case, we pick up a nexmo-php library.

For installation of a library, open the terminal in your project root directory and run the below command. You should have Composer installed on your system before running the command below.

composer require nexmo/client

Next step is, create an account with Nexmo and grab the API keys.

Nexmo API Keys

As we are testing with the free credit we need to add test numbers so that this number is whitelisted and will receive SMS.

Click on your profile name and select the Test numbers.

Select Test Numbers

On the next page, select your country code and add the number to which you want to send text message.

Add Test Number

Send Text Message Using Nexmo API

At this stage, we are ready with our API keys, nexmo-php library, and a test number. Now we can write a PHP code which will send text message to an added test number.

<?php
require_once "vendor/autoload.php";

$client = new Nexmo\Client(new Nexmo\Client\Credentials\Basic(YOUR_API_KEY, YOUR_API_SECRET));

$message = $client->message()->send([
    'to' => 'RECEPIENT_NUMBER_WITH_COUNTRY_CODE',
    'from' => 'Artisans Web',
    'text' => 'Test message from the Artisans Web',
]);

echo "Sent message to " . $message['to'] . ". Balance is now " . $message['remaining-balance'] . PHP_EOL;
?>

Make sure to replace the placeholders with actual values. When adding value for ‘to’ you need to add country code and the number. For example, +919999999999. Here +91 is the country code for India.

We are using a free trial so probably recipient will get a watermark [FREE SMS DEMO, TEST MESSAGE] in SMS. When we go for their paid service, of course, this watermark will be removed.

We hope you understand how to send text message from a website using the Nexmo API and PHP. Please share your thoughts in the comment below.

If you liked this article, then please subscribe to our Youtube Channel for video tutorials.

Posted in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *