Get Started with PHP

Sign up for a Plivo Account

Whenever a customer signs up with Plivo, they are provided with a free trial account to experiment with our services. This free trial account is given free credits for testing and using the same credits you can go ahead and purchase a number by navigating here. Once you have a number and credits in your account, you are all set to start testing our voice and SMS service.

Register here to get your free Plivo account today.

Below are the steps that are to be followed to successfully Sign up for a free trial account.

  1. Sign up with your corporate email address

  2. You will receive an activation email from Plivo. Click on the link mentioned in the email to activate your account.

  3. Enter an sms enabled, non-VOIP number to complete the phone verification.

Sign up with your corporate email address

Sign up with email addres

If you are facing any issues while creating a Plivo account, you can reach out to our Support Team

Install PHP and the Plivo PHP Server SDK

To get started with Sending your First SMS, you have to set up PHP and install Plivo PHP SDK.

Install PHP

Operating System Instructions
OS X You can install PHP using the official installer. You can also install it from here.
Linux To install PHP on Linux you can find the instructions here.
Windows To install PHP on Windows you can use the official installer.

Install Composer

Composer is a dependency manager for PHP which is used in all Modern PHP frameworks(Symfony, Laravel), we highly recommend using Composer as the package manager for your Web Project.

  1. Download the latest version of Composer.
  2. Run the following command in Terminal:

     $ php ~/Downloads/composer.phar --version
    
  3. Run the following command to make it executable:

     $ cp ~/Downloads/composer.phar /usr/local/bin/composer
     $ sudo chmod +x /usr/local/bin/composer
     $ Make sure you move the file to bin directory.
    
  4. To check if the path has /usr/local/bin, use

     $ echo $PATH
    

    If the path is different, use the following command to update the $PATH:

     $ export PATH = $PATH:/usr/local/bin
     $ source ~/.bash_profile
    
  5. You can also check the version of Composer by running the following command:

     $ composer --version.       
    

  1. Run the following command:

     $ curl -sS https://getcomposer.org/installer | php
    
  2. Run the following command to make the composer.phar file as executable:

     $ chmod +x composer.phar
    
  3. Run the following command to make Composer globally available for all system users:

     $ mv composer.phar /usr/local/bin/composer
    

  1. Download and run the Windows Installer for Composer.

    Note: Make sure to allow Windows Installer for Composer to make changes to your php.ini file.

  2. If you have any terminal windows open, close all instances and open a fresh terminal instance.
  3. Run the Composer command.

     $ composer -V
    

Install Plivo PHP Package

  • To install the stable release, run the following command in the project directory:

      $ composer require plivo/plivo-php
    
  • To install a specific release, run the following command in the project directory:

      $ composer require plivo/plivo-php:4.3.1
    
  • To test the features in the beta release, run the following command in the project directory:

      $ composer require plivo/plivo-php:v4.2-beta1
    
  • Alternatively, you can download this source and run

      $ composer install
    

This generates the autoload files, which you can include using the following line in your PHP source code to start using the SDK.

<?php
require 'vendor/autoload.php'

Send your first Outbound SMS

We’ll see how to use Plivo APIs to send SMS messages from your web application. Let’s have a look at some prerequisites before we dive into the code.

  • Plivo Auth Id and Auth Token: You will find your Plivo Auth Id and Auth Token on the home screen of your Plivo Console. Click here to sign-up for a Plivo account if you haven’t already!

credentials

  • Plivo Phone Number: You must have a SMS-enabled Plivo phone number to send messages to the US and Canada. You can purchase numbers from the Numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API.

Buy New Number

Create a file called SendSMS.php and paste the below code.

Code

1
2
3
4
5
6
7
8
9
10
<?php
require 'vendor/autoload.php';
use Plivo\RestClient;

$client = new RestClient("auth_id", "auth_token");
$message_created = $client->messages->create(
    'plivo_src_number',
    ['the_destination_number'],
    'Hello, world!'
);
Note:
  • Replace the placeholders auth_id & auth_token with your credentials from Plivo Console
  • Replace the placeholder plivo_source_number with the Phone number which you have purchased and the_destination_number with the phone number you will be sending SMS text messages to
  • Both plivo_source_number and the_destination_number should be in E.164 format, for example +15671234567.

save the file and use the below command to run it.

php SendSMS.php
Note: If you are using a Plivo Trial account for this example, you can only send sms to phone numbers that have been verified with Plivo. Phone numbers can be verified at the Sandbox Numbers page.

Set up PHP server for Incoming Messages & Callbacks.

In this section, you will learn how to set up a PHP server in under 5 minutes and start handling Incoming Messages & Callbacks.

Plivo supports receiving SMS text messages in 9 countries (see complete SMS API coverage). When an SMS is sent to a Plivo phone number, you can receive the text on your server by setting a Message URL in your Plivo app. Plivo will send the message along with other parameters to your Message URL.

Set up a PHP server

Use the following code snippet to start a local server.

1
2
3
4
5
6
7
8
<?php
require 'vendor/autoload.php';

$from_number = $_REQUEST["From"];
$to_number = $_REQUEST["To"];
$text = $_REQUEST["Text"];

echo("Message received - From $from_number, To: $to_number, Text: $text");

Save this code in any file (let’s say the file name is receive_sms.php). To run this file on the server, go to the folder where this file resides and use the following command:

php -S localhost:8000

You can choose to run it on any other IP or port that your machine has access to. And you should see your basic server app in action on http://localhost:8000/receive_sms.php

Exposing your local server to the internet

To receive Incoming Messages and to handle callbacks, your local server should be able to connect with Plivo API service, Ngrok is a tunneling software used to expose a web server running on your local machine to the internet. Using Ngrok you can set webhooks which can talk to Plivo server.

ngrok block diagram

You can download and install ngrok from here. Follow the detailed configuration instructions to get started.

Run ngrok on the port which currently hosts your application. For example, if your port number is 80, run the following command:

./ngrok http <port_on_which_your_local_server_is_running>

This will give you a UI with links that look like ngrok.io/* which you can use to access your local server using the public network.

ngrok

Create an Application

  1. Create an Application by visiting the Application Page and click on New Application or by using Plivo’s Application API.
  2. Give your application a name. Let’s call it Receive SMS. Enter your server URL (e.g. http://example.com/receive_sms.php) in the Message URL field and set the method as POST. See our Application API docs to learn how to modify your application through our APIs.
  3. Click on Create to save your application.

Create Application

Assign a Plivo number to your app

  1. Navigate to the Numbers page and select the phone number you want to use for this app.
  2. Select Receive SMS (name of the app) from the Plivo App dropdown list.
  3. Click on Update to save.

Assign Phone Number to Receive SMS App

Test it out

Send an SMS to your Plivo number using a regular mobile phone. Plivo will send a request to your Message URL with the parameters listed in the XML Request - Messages Documentation.

Reply to an incoming SMS

When an SMS is sent to an Plivo phone number, you can receive the text on your server by setting a Message URL in your Plivo app. Plivo will send the message along with other parameters to your Message URL. You can reply back using the Plivo Message XML.

Note: In order to reply to a US or Canadian phone number, the `'src'` (source) phone number must be a Plivo US or Canadian phone number that is SMS-enabled. You can purchase a Plivo number from the Buy Numbers tab on your Plivo Console and filter the Phone Numbers by selecting SMS in the features option to buy SMS enabled numbers.

Create a file called reply_to_sms.php and paste the below code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
require 'vendor/autoload.php';
use Plivo\XML\Response;

function sendResponseWithPlivo()
{
    $number = $_POST["From"];
    $response = new Response();
    $params = array(
        'src' => "plivo_src_number",
        'dst' => $number,
        'callbackUrl' => "https://www.foo.com/sms_status/",
        'callbackMethod' => "POST"
    );
    $message_body = "Testing reply to an incoming SMS";
    $response->addMessage($message_body, $params);
    return $response->toXML();
}
 header("Content-type: text/xml; charset=utf-8");
 echo sendResponseWithPlivo();
Note: Create an application in the Plivo console with your Server URL in the message_URL field and assign the app to a Plivo number.

Test it out

Send an SMS to your Plivo number using a regular mobile phone and an automatic response will be sent from your Plivo number to your mobile phone. Also, Plivo will send a request to your Message URL with the parameters listed in the XML Request - Messages Documentation.