> ## Documentation Index
> Fetch the complete documentation index at: https://plivo.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# PHP SDK

> Install and configure the Plivo PHP SDK for voice and SMS integration

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](https://getcomposer.org/download/).
2. Run in Terminal:

   ```sh theme={null}
   php ~/Downloads/composer.phar --version
   ```
3. To make Composer executable , run:

   ```sh theme={null}
   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:

   ```sh theme={null}
   echo $PATH
   ```

   If necessary, update the \$PATH by running:

   ```sh theme={null}
   export PATH = $PATH:/usr/local/bin
   source ~/.bash_profile
   ```
5. Check the version of Composer by running:

   ```sh theme={null}
   composer --version.
   ```

#### Globally in Linux

1. Run:

   ```sh theme={null}
   curl -sS https://getcomposer.org/installer | php
   ```
2. To make the composer.phar file executable, run:

   ```sh theme={null}
   chmod +x composer.phar
   ```
3. To make Composer globally available for all system users, run:

   ```sh theme={null}
   mv composer.phar /usr/local/bin/composer
   ```

#### Windows 10

1. Download and run the [Windows Installer](https://getcomposer.org/download/) 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.

   ```sh theme={null}
   composer -V
   ```

### To install the Plivo package

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

  ```sh theme={null}
  composer require plivo/plivo-php
  ```
* To install a **specific or beta release**, run this command in the project directory, specifying the release:

  ```sh theme={null}
  composer require plivo/plivo-php:4.3.1
  ```
* Alternatively, you can download this source and run:

  ```sh theme={null}
  composer install
  ```

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

```php theme={null}
<?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](https://cx.plivo.com/home) 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 theme={null}
<?php
require 'vendor/autoload.php';
use Plivo\RestClient;

$client = new RestClient();
```

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

```php theme={null}
<?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](https://cx.plivo.com/home).

## The basics

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

```php theme={null}
<?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 theme={null}
<?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 theme={null}
<?php
require 'vendor/autoload.php';
use Plivo\RestClient;

$client = new RestClient();
$message_created = $client->messages->create(
  [  
        "src" => "<sender_id>",
        "dst" => "<destination_number>",
        "text"  =>"Hello, world!",
        "url"=>"https://<yourdomain>.com/sms_status/"
     ]
);
```

Replace the auth placeholders with your authentication credentials from the [Plivo console](https://cx.plivo.com/home). Replace the phone number placeholders with actual phone numbers in [E.164 format](https://en.wikipedia.org/wiki/E.164) (for example, +12025551234).

### Make a call

```php theme={null}
<?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 theme={null}
<?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 theme={null}
<?xml version="1.0" encoding="utf-8"?>
<Response>
  <Speak>Hello, world!</Speak>
</Response>
```

### Run a PHLO

```php theme={null}
<?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](https://cx.plivo.com/home). Replace the phlo\_id placeholder with your PHLO ID from the [Plivo console](https://cx.plivo.com/agents).

### More examples

Refer to the [Plivo API Reference](/voice/api/overview/) documentation for more examples. Also refer to our [guide to setting up dev environment](/voice/quickstart/php-quickstart/) for details on how to set up a simple PHP server and expose that server to the internet.

## Example response

```php theme={null}
(
    [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](https://github.com/plivo/plivo-php/issues).
