Get Started with PHP
Sign up for a Plivo Account
You can sign up with Plivo and start with a free trial account to experiment with and learn about our services. This free trial account comes with free credits. If you wish to continue with our service, you can add more credits and buy a number by clicking here. Add a number and credits to your account to test the full range of our voice and SMS service features.
Sign up here to get your free Plivo account today.
Follow these steps to sign up for a free trial account:
- Sign up with your work email address
- Check your inbox for an activation email from Plivo. Click on the link in the email to activate your account.
- Enter your mobile number to complete the phone verification step.
Sign up with your work email address
If you have any issues creating a Plivo account, please reach out to our Support Team for help.
Install PHP and the Plivo PHP Server SDK
You must set up and install PHP, and Plivo’s PHP SDK to send your first SMS. Here’s how.
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.
- Download the latest version of Composer.
Run the following command in Terminal:
$ php ~/Downloads/composer.phar --version
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.
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
You can also check the version of Composer by running the following command:
$ composer --version.
Run the following command:
$ curl -sS https://getcomposer.org/installer | php
Run the following command to make the composer.phar file as executable:
$ chmod +x composer.phar
Run the following command to make Composer globally available for all system users:
$ mv composer.phar /usr/local/bin/composer
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.
- If you have any terminal windows open, close all instances and open a fresh terminal instance.
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
This section will guide you through how to use Plivo APIs to Send SMS from your application. First, let’s make sure you meet these 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!
- Plivo Phone Number: You must have a SMS-enabled Plivo phone number to send messages to the US and Canada. Purchase numbers from the Numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API.
Now, create a file called SendSMS.php
and paste the following 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(
'+14151234567',
['+14157654321'],
'Hello, world!'
);
- 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 +14157654321 with the phone number you will be sending SMS text messages to
- Both plivo_source_number and +14157654321 should be in E.164 format
Save the file and use the below command to run it.
php SendSMS.php
Set up PHP server for Incoming Messages & Callbacks.
In this section, we’ll walk you through how to set up a PHP server in under five 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.
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.
Create an Application
- Create an Application by visiting the Application Page and click on
New Application
. You can also use Plivo’s Application API. - Give your application a name. Let’s call it
Receive SMS
. Enter your server URL (e.g.http://example.com/receive_sms.php
) in theMessage URL
field and set the method asPOST
. See our Application API docs to learn how to modify your application through our APIs. - Click on
Create
to save your application.
Assign a Plivo number to your app
- Navigate to the Numbers page and select the phone number you want to use for this app.
- Select
Receive SMS
(name of the app) from the Plivo App dropdown list. - Click on
Update
to save.
Test and validate
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.
Create a file called reply_to_sms.php
and paste the following 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' => "+14151234567",
'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();
Test and validate
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.