PHP code example of plivo / php-sdk

1. Go to this page and download the library: Download plivo/php-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

plivo / php-sdk example snippets





livo\RestClient;

$client = new RestClient();


livo\RestClient;

$client = new RestClient("<auth_id>", "<auth_token>");


$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


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


livo\RestClient;

$client = new RestClient();
$message_created = $client->messages->create([ 
        "src" => "+14156667778", 
        "dst" => "+14156667777", 
        "text"  =>"Hello, this is a sample text from Plivo"
]);


livo\RestClient;

$client = new RestClient();
$call_made = $client->calls->create(
    '+14156667778',
    ['+14156667777'],
    'https://answer.url'
);


livo\RestClient;

$client = new RestClient("<auth_id>", "<auth_token>");
$response = $client->lookup->get("<number-goes-here>");


livo\XML\Response;

$response = new Response();
$response->addSpeak('Hello, world!');
echo($response->toXML());


/**
 * Example for API Request
 */

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);
}


livo\RestClient;

$client = new RestClient("<auth_id>","<auth_token>");

$template = '{ 
            "name": "template_name",
            "language": "en_US",
            "components": [
                {
                    "type": "header",
                    "parameters": [
                        {
                            "type": "media",
                            "media": "https://xyz.com/s3/img.jpg"
                        }
                    ]
                },
                {
                    "type": "body",
                    "parameters": [
                        {
                            "type": "text",
                            "text": "WA-Text"
                        }
                    ]
                }
            ]
          }';

$response = $client->messages->create([  
        "src" => "+14156667778",
        "dst" => "+14156667777",
        "type"=> "whatsapp",
        "template"=> $template,
        "url"=> "https://foo.com/wa_status/"
]);
print_r($response);


livo\RestClient;

$client = new RestClient("<auth_id>","<auth_token>");
$response = $client->messages->create([  
        "src"=> "+14156667778",
        "dst"=> "+14156667777",
        "text"=> "Hello, this is sample text",
        "type"=> "whatsapp",
        "url"=> "https://foo.com/wa_status/"
]);
print_r($response);


livo\RestClient;

$client = new RestClient("<auth_id>","<auth_token>");
$response = $client->messages->create([  
        "src"=> "+14156667778",
        "dst"=> "+14156667777",
        "text"=> "Hello, this is sample text",
        "type"=> "whatsapp",
        "media_urls"=> ["https://sample-videos.com/img/Sample-png-image-1mb.png"],
        "url"=> "https://foo.com/wa_status/"
]);
print_r($response);


livo\RestClient;

$client = new RestClient("<auth_id>","<auth_token>");

$interactive = '{
        "type": "button",
        "header": {
            "type": "media",
            "media": "https://xyz.com/s3/img.jpg"
        },
        "body": {
            "text": "Make your selection"
        },
        "action": {
            "buttons": [
                {
                    "title": "Click here",
                    "id": "bt1"
                },
                {
                    "title": "Know More",
                    "id": "bt2"
                },
                {
                    "title": "Request Callback",
                    "id": "bt3"
                }
            ]
        }
    }';

$response = $client->messages->create([
        "src"=> "+14156667778",
        "dst"=> "+14156667777",
        "type"=> "whatsapp",
        "interactive"=> $interactive
]);
print_r($response);


livo\RestClient;

$client = new RestClient("<auth_id>","<auth_token>");

$interactive = '{
        "type": "list",
        "header": {
            "type": "text",
            "text": "Welcome to Plivo"
        },
        "body": {
            "text": "You can review the list of rewards we offer"
        },
        "footer": {
            "text": "Yours Truly"
        },
        "action": {
            "buttons": [{
                "title": "Click here"
            }],
            "sections": [
                {
                    "title": "SECTION_1_TITLE",
                    "rows": [
                        {
                            "id": "SECTION_1_ROW_1_ID",
                            "title": "SECTION_1_ROW_1_TITLE",
                            "description": "SECTION_1_ROW_1_DESCRIPTION"
                        },
                        {
                            "id": "SECTION_1_ROW_2_ID",
                            "title": "SECTION_1_ROW_2_TITLE",
                            "description": "SECTION_1_ROW_2_DESCRIPTION"
                        }
                    ]
                },
                {
                    "title": "SECTION_2_TITLE",
                    "rows": [
                        {
                            "id": "SECTION_2_ROW_1_ID",
                            "title": "SECTION_2_ROW_1_TITLE",
                            "description": "SECTION_2_ROW_1_DESCRIPTION"
                        },
                        {
                            "id": "SECTION_2_ROW_2_ID",
                            "title": "SECTION_2_ROW_2_TITLE",
                            "description": "SECTION_2_ROW_2_DESCRIPTION"
                        }
                    ]
                }
            ]
        }
    }';

$response = $client->messages->create(
        [
                "src"=> "+14156667778",
                "dst"=> "+14156667777",
                "type"=> "whatsapp",
                "interactive"=> $interactive
        ]
);
print_r($response);


livo\RestClient;

$client = new RestClient("<auth_id>","<auth_token>");

$interactive = '{
      "type": "cta_url",
        "header": {
            "type": "media",
            "media": "https://xyz.com/s3/img.jpg"
        },
        "body": {
            "text": "Know More"
        },
        "footer": {
            "text": "Plivo"
        },
        "action": {
            "buttons": [
                {
                    "title": "Click here",
                    "cta_url": "https:plivo.com"
                }
            ]
        }
    }';

$response = $client->messages->create([
        "src"=> "+14156667778",
        "dst"=> "+14156667777",
        "type"=> "whatsapp",
        "interactive"=> $interactive
]);
print_r($response);


livo\RestClient;

$client = new RestClient("<auth_id>","<auth_token>");

$template = '{
        "name": "plivo_order_pickup",
        "language": "en_US",
        "components": [
            {
                "type": "header",
                "parameters": [
                    {
                        "type": "location",
                        "location": {
                            "longitude": "122.148981",
                            "latitude": "37.483307",
                            "name": "Pablo Morales",
                            "address": "1 Hacker Way, Menlo Park, CA 94025"
                        }
                    }
                ]
            }
        ]
    }';

$response = $client->messages->create([
        "src"=> "+14156667778",
        "dst"=> "+14156667777",
        "type"=> "whatsapp",
        "template"=> $template
]);
print_r($response);


livo\RestClient;

$client = new RestClient("<auth_id>","<auth_token>");

$location = '{
        "longitude": "122.148981",
        "latitude": "37.483307",
        "name": "Pablo Morales",
        "address": "1 Hacker Way, Menlo Park, CA 94025"
    }';

$response = $client->messages->create([
        "src"=> "+14156667778",
        "dst"=> "+14156667777",
        "type"=> "whatsapp",
        "location"=> $location
]);
print_r($response);