Download Recordings Using PHP

Overview

This guide shows how to retrieve recordings and download them to local storage. Plivo begins charging for stored recordings after 90 days. To avoid these charges, you can download recordings and store them elsewhere.

Prerequisites

To use Plivo APIs, follow our instructions to set up a PHP development environment and a web server and safely expose that server to the internet.

Download recordings to local storage

Here’s sample code you can use to retrieve recordings to a local directory.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/**
 * Example script for downloading recording files
 */
require '../../vendor/autoload.php';
use Plivo\RestClient;
use Plivo\Exceptions\PlivoRestException;
use GuzzleHttp\Client;

$AUTH_ID = "<auth_id>";
$AUTH_TOKEN = "<auth_token>";

$client = new RestClient($AUTH_ID,$AUTH_TOKEN);

try {
    $response = $client->recordings->list(
        [
            'add_time__gt' => "2023-04-01 00:00:00",
            'add_time__lt' => "2023-04-30 00:00:00",
            'limit' => 5,
            'offset' => 0
        ]
    );

    echo "Found " . count($response->resources) . " recordings." . PHP_EOL;

    // Directory where the recordings will be saved
    $dir = "./recordings";

    if (!file_exists($dir)) {
        mkdir($dir, 0777, true);
    }

    $http = new Client();

    foreach ($response as $recording) {
        $recording_url = $recording->recordingUrl;
        $recording_id = $recording->recordingId;
        $format = $recording->recordingFormat;

        echo "Downloading recording: " . $recording_url . PHP_EOL;

        $output_file = $dir . "/" . $recording_id . "." . $format;

        // Download the file
        $http->get($recording_url, ['sink' => $output_file]);

        echo "Downloaded file to: " . $output_file . PHP_EOL;
    }
}
catch (PlivoRestException $ex) {
    print_r($ex);
}

Delete recordings from Plivo storage

You can delete a recording by using the Delete a Recording API and specifying a recording ID, which you can retrieve from list all recordings API or the HTTP callback details stored in your database. You can also delete recordings from the Voice Recordings page of the Plivo console.