PHP SDK

    The Plivo PHP SDK makes it simpler to integrate voice and SMS communications into your PHP applications using the Plivo REST APIs. Using the SDK, you’ll be able to make voice calls, send SMS messages, and generate Plivo XML documents to control your call flows.

    Installation

    To install Composer

    Globally in Mac

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

       $ php ~/Downloads/composer.phar --version
      
    3. To make Composer executable , run:

       $ 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 whether the path already includes /usr/local/bin, run:

       $ echo $PATH
      

      If necessary, update the $PATH by running:

       $ export PATH = $PATH:/usr/local/bin
       $ source ~/.bash_profile
      
    5. Check the version of Composer by running:

       $ composer --version.
      

    Globally in Linux

    1. Run:

       $ curl -sS https://getcomposer.org/installer | php
      
    2. To make the composer.phar file executable, run:

       $ chmod +x composer.phar
      
    3. To make Composer globally available for all system users, run:

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

    Windows 10

    1. Download and run the Windows Installer for Composer.

      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 them and open a fresh terminal instance.
    3. Run the Composer command.

       $ composer -V
      

    To install the Plivo package

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

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

        $ composer require plivo/plivo-php:4.3.1
      
    • Alternatively, you can download this source and run:

        $ composer install
      

    This command generates the autoload files, which you can include using a line in your PHP source code:

    <?php
    require 'vendor/autoload.php'
    

    Getting started

    Authentication

    To make the API requests, you need to create a RestClient and provide it with authentication credentials, which you can find on the Overview page of the Plivo console.

    We recommend that you store your credentials in the PLIVO_AUTH_ID and the PLIVO_AUTH_TOKEN environment variables, to avoid the possibility of accidentally committing them to source control. If you do this, you can initialize the client with no arguments and it will automatically fetch them from the environment variables:

    <?php
    require 'vendor/autoload.php';
    use Plivo\RestClient;
    
    $client = new RestClient();
    

    Alternatively, you can specify the authentication credentials while initializing the RestClient.

    <?php
    require 'vendor/autoload.php';
    use Plivo\RestClient;
    
    $client = new RestClient("<auth_id>","<auth_token>");
    

    Replace the auth placeholders with your authentication credentials from the Plivo console.

    The basics

    The SDK uses consistent interfaces to create, retrieve, update, delete, and list resources. The pattern is:

    <?php
    $client->resources->create($params) # Create
    $client->resources->get($id) # Get
    $client->resources->update($id, $params) # Update
    $client->resources->delete($id) # Delete
    $client->resources->list() # List all resources, max 20 at a time
    

    You can also use the resource directly to update and delete it. For example:

    <?php
    $resource = $client->resources->get($id)
    $resource->update($params) # update the resource
    $resource->delete() # Delete the resource
    

    Using $client->resources->list() lists the first 20 resources by default (the first page, with limit as 20, and offset as 0). Use limit and offset to get more pages of resources.

    Examples

    Send a message

    <?php
    require 'vendor/autoload.php';
    use Plivo\RestClient;
    
    $client = new RestClient();
    $message_created = $client->messages->create(
      [  
            "src" => "+14152345678",
            "dst" => "+14151112222",
            "text"  =>"Hello, world!",
            "url"=>"http://foo.com/sms_status/"
         ]
    );
    

    Replace the auth placeholders with your authentication credentials from the Plivo console. Replace the phone number placeholders with actual phone numbers in E.164 format (for example, +12025551234).

    Make a call

    <?php
    require 'vendor/autoload.php';
    use Plivo\RestClient;
    
    $client = new RestClient();
    $call_made = $client->calls->create(
        '<caller_id>',
        ['<destination_number>'],
        'https://<answer.url>'
    );
    

    Generate Plivo XML

    <?php
    require 'vendor/autoload.php';
    use Plivo\XML\Response;
    
    $response = new Response();
    $response->addSpeak('Hello, world!');
    echo($response->toXML());
    

    This generates the following XML:

    <?xml version="1.0" encoding="utf-8"?>
    <Response>
      <Speak>Hello, world!</Speak>
    </Response>
    

    Run a PHLO

    <?php
    /**
     * Example for API Request
     */
    require 'vendor/autoload.php';
    use Plivo\Resources\PHLO\PhloRestClient;
    use Plivo\Exceptions\PlivoRestException;
    $client = new PhloRestClient("<auth_id>", "<auth_token>");
    $phlo = $client->phlo->get("<phlo_id>");
    try {
        $response = $phlo->run(["field1" => "value1", "field2" => "value2"]); // These are the fields entered in the PHLO console
        print_r($response);
    } catch (PlivoRestException $ex) {
        print_r($ex);
    }
    ?>
    

    Replace the auth placeholders with your authentication credentials from the Plivo console. Replace the phlo_id placeholder with your PHLO ID from the Plivo console.

    More examples

    Refer to the Plivo API Reference documentation for more examples. Also refer to our guide to setting up dev environment for details on how to set up a simple PHP server and expose that server to the internet.

    Example response

    (
        [requestUuid:protected] => 7e778326-2a51-43d9-b1a1-6ac93f077943
        [_message] => call fired
        [apiId] => ecdd4ffc-9413-11ea-8769-0242ac110007
        [statusCode] => 201
    )
    

    Reporting issues

    Report feedback or problems with this SDK by opening an issue on GitHub.