Receive MMS using PHP

    Overview

    Sending an outbound MMS is only one way to deliver your message to a recipient; in order to increase the interaction, two-way communication is required. In this guide, we’ll show you how to receive MMS on a Plivo phone number.

    Start your implementation with Plivo to receive MMS using PHLO or the traditional API way. PHLO allows you to create and deploy your flows using its intuitive canvas in few clicks. Refer to the instructions from the respective tabs below to start your integration with PHLO or API as you wish.

    To receive incoming MMS, you can create and deploy a PHLO with a few clicks on the PHLO canvas. PHLO also lets you visually construct your entire use-case. With PHLO, you only pay for MMS you send/receive, and building with PHLO is free. You can refer to the below instructions in the Using PHLO tab to begin your implementation.

    Outline

    Receive MMS

    Implementation

    In this section, we will guide you to create a PHLO to Receive MMS.

    Prerequisites

    1. Create a Plivo Account(if you don’t have one already): You can sign up with your work email address and complete the phone verification step using your mobile number.
    2. Plivo Phone Number(Optional): To receive MMS, you must have a Plivo phone number that supports MMS. Numbers can be purchased from the Numbers section of your Plivo Console and use the same as the source number/from number for the outbound MMS. This number will also help you receive incoming MMS as you must have a MMS-enabled Plivo phone number to do the same. Please note that you can also purchase numbers using the Numbers API.

    Create the PHLO

    You can create a PHLO by referring to the below instructions to Receive MMS:

    Create a PHLO to receive SMS

    • On the side navigation bar, click PHLO. The PHLO page will appear and display your existing PHLOs, if any exist. If this is your first PHLO, then the PHLO page will be empty.
    • Click Create New PHLO to build a new PHLO.
    • In the Choose your use-case window, click Build my own. The PHLO canvas will appear with the Start node.
      Note: The Start node is the starting point of any PHLO. You can choose between the four available trigger states of the start node; Incoming SMS, Incoming Call, and API Request. For this PHLO, we will use the Incoming Message trigger state.
    • From the list of components on the left-hand side, drag and drop the HTTP Request component onto the canvas. This will add a HTTP Request node onto the canvas.
    • Connect the Start node with the HTTP Request node, using the Incoming Message trigger state.
    • Configure the HTTP Request node to send the details to your Web Server.
    • Once you have configured the node, click Validate to save the configurations.
    • After you complete the configurations, provide a recognizable name for your PHLO and click Save. Your PHLO is now ready.

    Assign the PHLO to a Plivo Number for Incoming MMS

    Once you have created and configured your PHLO, assign your PHLO to a Plivo number.

    To assign a PHLO to a number

    1. Log in to your Plivo Console
    2. On the Product Navigation bar, click Phone Numbers.
    3. On the Numbers page, under Your Numbers, click the phone number you wish to use for the PHLO.
    4. In the Number Configuration window, select PHLO from the Application Type list.
    5. From the PHLO Name list, select the PHLO you wish to use with the phone number, and then click Update Number.

    Assign PHLO to a Plivo Number

    If you have not purchased a phone number yet, you can buy a phone number(s) by referring to the instructions available here.

    Test and Validate

    You can now receive a message to your Plivo phone number to receive an incoming MMS and see how the inbound MMS is handled using PHLO.

    For more information about creating a PHLO app, see the PHLO User Guide.
    For information on components and their variables, see the PHLO Components Library.

    To receive your first incoming MMS, you can refer to the instructions in the following section to begin your implementation.

    Outline

    Receive MMS

    Implementation

    In this section, we will guide you in setting up an app to receive a MMS on your Plivo number.

    Prequisites

    1. Create a Plivo Account (if you don’t have one already): You can Sign up with your work email address and complete the phone verification step using your mobile number.
    2. Buy a Plivo Number: You must have a mms-enabled Plivo phone number if you are willing to receive incoming MMS. You can purchase numbers from the Numbers section of your Plivo Console. It is also possible to purchase numbers using the Numbers API.

    Set Up Laravel server for Incoming MMS

    In this section, we’ll walk you through how to set up a Laravel server in under five minutes and start handling incoming MMS.

    Install PHP

    Operating SystemInstructions
    macOSYou can install PHP using the official installer. You can also install it from here.
    LinuxTo install PHP on Linux you can find the instructions here.
    WindowsTo install PHP on Windows you can use the official installer.

    Install Composer

    Composer is a dependency manager for PHP that is used in all modern PHP frameworks, such as Symfony and 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 in order to run the composer:

       $ php ~/Downloads/composer.phar --version
      

      Note: PHAR (PHP archive) is an archive format for PHP that can be run on the command line

    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
      

      Note: If your PATH doesn’t include /usr/local/bin directory, we recommend adding it so that you can access it globally.

    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
      

      Note: PHAR (PHP archive) is an archive format for PHP that can be run on the command line

    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 Laravel & Create a Laravel Project

    • Use the below command to install Laravel:

      $ composer require laravel/installer
      

    As we have Laravel and its dependencies installed, we can use them to create a new Laravel project. As the initial step, using Laravel we can auto-generate code in the Laravel folder structure.

    • Change the directory to our project directory in the command line:

      $ cd mylaravelapp
      
    • Use the below command to start your Laravel project:

      $ composer create-project laravel/laravel quickstart --prefer-dist
      
    • To install the stable release, run the following command in the project directory:

      $ composer require plivo/plivo-php
      

    This will create a quickstart directory with the necessary folders & files for development.

    Install Plivo

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

      $ composer require plivo/plivo-php:4.15.0
      
    • 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'
    

    Create a Laravel Controller for Incoming MMS

    Change the directory to our newly created project directory, i.e, quickstart directory and run the below command to create a Laravel controller for inbound MMS.

    $ php artisan make:controller MMSController
    

    This will generate a controller named MMSController in the app/http/controllers/ directory. Now, You have to open the app/http/controllers/voiceController.php file and add the following code:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    <?php
    
    namespace App\Http\Controllers;
    require '../../vendor/autoload.php';
    use Plivo\RestClient;
    use Illuminate\Http\Request;
    
    class VoiceController extends Controller
    {
        public function receivemms()
        {
             $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");
        }
    }
    

    Add a Route

    Now, you need to add a route for the receivemms function in MMSController class, open the routes/web.php file and add the below line at the end of the file:

    Route::match(['get', 'post'], '/receivemms', 'MMSController@receivemms');
    

    Now the SMSController is ready for your first inbound MMS, you can use the below command to handle your first inbound SMS using Laravel and Plivo PHP SDK.

    $ php artisan serve
    

    Your local development server will be started and you can test the laravel app for inbound mms via the URL http://127.0.0.1:8000/receivemms/.

    Exposing your local server to the internet

    To receive incoming MMS, your local server should be able to connect with the 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 that can talk to the 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 8000, run the following command:

    $ ./ngrok http 8000
    

    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.

    Sample ngrok CLI

    Create an Application

    1. Create an Application by visiting the Application Page and click on New Application. You can also use Plivo’s Application API.
    2. Give your application a name. Let’s call it Receive MMS. Enter your server URL (e.g. http://example.com/receivesms/) 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 MMS (name of the app) from the Plivo App dropdown list.
    3. Click on Update to save.

    Assign Phone Number to Receive MMS App

    Test and validate

    Receive MMS to your Plivo number using a regular mobile phone. Plivo will send a request to your Message URL with the parameters listed in the Messages Documentation.

    Common Use Cases for Receive MMS

    The majority of the use cases include an Receive MMS. For example,MMS Surveys, MMS chat and so on.