Sending SMS messages via command line in PHP

Sending SMS messages via command line in PHP

Sign up with an account at Twilio and opt for the trial account. The trial account provides you with a set amount of funds. You need to apply for a phone number provided by Twilio.

The app makes http POST request to Twilio and Twilio sends the SMS.

Create a project directory eg: projects/php/sms. Use Composer to install the Twilio sdk $projects/php/sms composer require twilio/sdk

In the project directory – $projects/php/sms – create a send-message.php file and open in text editor.

Require the Twilio helper library just obtained via composer Declare to use the Twilio Rest client object.

Account credentials Include the $sid and $token – find these in your Twilio account dashboard. Place in environmental variables for security.

$sid = $_ENV[“TWILIOACCOUNTSID”]; $client = $_ENV[“TWILIOAUTHTOKEN”];

Use credentials to create a Twilio Rest client passing in SID and TOKEN $client = new Client($sid, $token);

<?php

require __DIR__ . '/vendor/autoload.php';
use Twilio\Rest\Client;

$sid = 'AC**********************************';
$token = 'e6********************************';

        $client = new Client($sid, $token);

        $client->messages->create(
            '+442323232323223',
            array(
                'from' => '+44123456789',
                'body' => 'Your text message goes here.'
                )
            );

?>

Run the script

Run the script which sends the message

$projects/php/sms send-message.php

Original Photo by Tracy Adams on Unsplash

Leave a Reply

Your email address will not be published.