This guide shows how to set up a development environment in five minutes to trigger API requests related to our Messaging API. It also shows how to send and receive messages using tunneling software to expose the local dev server to the public internet.
To get started, install PHP, the Composer dependency manager, the Laravel web framework, and Plivo’s PHP SDK.
Operating System | Instructions |
---|---|
macOS | Install PHP using the official macOS installer or by downloading and installing it. |
Linux | Download and install PHP using your favorite package installer. |
Windows | Use the official Windows installer. |
All modern PHP frameworks use the Composer dependency manager; we highly recommend using it as the package manager for your web project. Follow the instructions to download and install Composer for macOS and Linux and for Windows, or follow the steps below.
Run this command in Terminal:
$ php ~/Downloads/composer.phar --version
Note: PHAR (PHP archive) is an archive format for PHP that can be run on the command line.
Copy the file to /usr/local/bin and make it executable:
$ cp ~/Downloads/composer.phar /usr/local/bin/composer
$ sudo chmod +x /usr/local/bin/composer
If your PATH doesn’t include /usr/local/bin directory, we recommend adding it so that you can access it globally. To check if the path has /usr/local/bin, enter
$ echo $PATH
If necessary, run these commands to update the $PATH:
$ export PATH = $PATH:/usr/local/bin
$ source ~/.bash_profile
You can also check the version of Composer by running this command:
$ composer --version.
Run the command
$ curl -sS https://getcomposer.org/installer | php
Make the composer.phar file executable:
$ chmod +x composer.phar
Note: PHAR (PHP archive) is an archive format for PHP that can be run on the command line.
Run this 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: Allow Windows Installer for Composer to make changes to your php.ini file.
Run the Composer command.
$ composer -V
Install the Laravel web framework by running the command
$composer require laravel/installer
Once you have Laravel installed, create a project directory using the command mkdir mylaravelapp
, then change to that directory and create a new Laravel project.
composer create-project laravel/laravel quickstart --prefer-dist
This command creates a directory named quickstart with the necessary folders and files for development.
To install the stable release of the Plivo SDK, run this command in the project directory:
composer require plivo/plivo-php
To install a specific release, run this command in the project directory:
composer require plivo/plivo-php:4.15.0
Alternatively, you can download source code from GitHub and run
composer install
The composer
command generates the autoload files, which you can include in your PHP source code by using the line
<?php
require 'vendor/autoload.php'
Now you can create a file in the project directory and execute code to trigger any Plivo API. Here’s some example code that sends an SMS message. Change to the quickstart directory and run this command to create a Laravel controller to send an outbound SMS message.
php artisan make:controller SMSController
This command generates a controller named SMSController in the app/http/controllers/ directory. Edit the app/http/controllers/SMSController.php file and paste into it this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
namespace App\Http\Controllers;
require '../../vendor/autoload.php';
use Plivo\RestClient;
use Illuminate\Http\Request;
class SMSController extends Controller
{
public function sendSMS()
{
$client = new RestClient("<auth_id>","<auth_token>");
$response = $client->messages->create(
[
"src" => "<sender_id>",
"dst" => "<destination_number>",
"text" =>"Hello, from PHP Laravel!",
]
);
header('Content-Type: application/json');
echo json_encode($response);
}
}
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
namespace App\Http\Controllers;
require '../../vendor/autoload.php';
use Plivo\RestClient;
use Illuminate\Http\Request;
class SMSController extends Controller
{
public function sendMMS()
{
$client = new RestClient("<auth_id>","<auth_token>");
$mediaURLs = ['https://media.giphy.com/media/26gscSULUcfKU7dHq/source.gif'];
$mediaIDs = ['801c2056-33ab-499c-80ef-58b574a462a2'];
$response = $client->messages->create(
[
"src" => "<sender_id>",
"dst" => "<destination_number>",
"text" =>"Hello, from Laravel!",
"type" => "mms",
"media_urls" => $mediaURLs,
"media_ids" => $mediaIDs
]
);
header('Content-Type: application/json');
echo json_encode($response);
}
}
?>
To add a route for the outbound function in the SMSController class, open the routes/web.php file and add this line at the end of the file:
Route::match(['get', 'post'], '/sendSMS', 'SMSController@sendSMS');
Route::match(['get', 'post'], '/sendSMS', 'App\Http\Controllers\SMSController@sendSMS');
Now the SMSController is ready. Use this command to initiate an outbound SMS message.
php artisan serve
Your local development server will be started and you can test the application for outbound messaging via the URL http://localhost:8000/sendSMS/.
You can follow the same approach to trigger other API requests. Refer to our detailed API reference to see all the API requests available on the Messaging API platform.
Now that we’ve sent a message, let’s set up a Laravel server to handle incoming messages.
Plivo supports receiving SMS text messages in several countries (see complete SMS API coverage). When someone sends a text message to a Plivo phone number, you can receive it on your server by setting a Message URL in your Plivo application. Plivo sends the message, along with other parameters, to your Message URL.
Edit app/http/controllers/SMScontroller.php and add this code in the SMSController class after the sendSMS function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
namespace App\Http\Controllers;
require '../../vendor/autoload.php';
use Plivo\RestClient;
use Plivo\XML\Response;
use Illuminate\Http\Request;
class VoiceController extends Controller
{
// Send outbound SMS
public function sendSMS()
{
.......
}
// Receive incoming SMS
public function receivesms()
{
$from_number = $_REQUEST["From"];
$to_number = $_REQUEST["To"];
$text = $_REQUEST["Text"];
echo("Message received - From $from_number, To: $to_number, Text: $text");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
namespace App\Http\Controllers;
require '../../vendor/autoload.php';
use Plivo\RestClient;
use Plivo\XML\Response;
use Illuminate\Http\Request;
class VoiceController extends Controller
{
//Send outbound SMS
public function sendSMS()
{
.......
}
// Receive incoming SMS
public function receivesms()
{
$from_number = $_REQUEST["From"];
$to_number = $_REQUEST["To"];
$text = $_REQUEST["Text"];
$media_url = $_REQUEST["Media0"];
echo("Message received - From $from_number, To: $to_number, Text: $text, Media: $media_url");
}
}
To add a route for the receivesms function in the SMSController class, open the routes/web.php file and add these lines after the sendSMS route:
Route::match(['get', 'post'], '/sendSMS', 'SMSController@sendSMS');
Route::match(['get', 'post'], '/receivesms', 'SMSController@receivesms');
Now SMSController is ready. Use this command to receive an inbound message.
php artisan serve
Your local development server will be started and you can test the application for inbound messages via the URL http://localhost:8000/receivesms/.
To serve XML documents, your local server must connect with Plivo API services. For that, we recommend using ngrok, which exposes local servers running behind NATs and firewalls to the public internet over secure tunnels. Using ngrok, you can set webhooks that can talk to the Plivo server.
Install ngrok and run it on the command line, specifying the port that hosts the application on which you want to receive messages (8000 in this case):
./ngrok http 8000
This starts the ngrok server on your local server.
You should be able to see your basic server application in action at https://<nrgok_URL>/inbound/.
Now people can send messages to your Plivo number.
You can follow the same approach to serve other XML documents to manage call flows. Refer to our detailed XML reference to see all the XML elements available on the Messaging API platform.