PHP Quickstart: Call Record Retrieval

Call logs from REST

The last component of this quickstart is retrieving your call logs using the Twilio REST API. You can find your logs by going to the the Logs page of your account on the Twilio web site, but getting access to this data programmatically is useful for automated reporting, or building other fully-featured systems like a voicemail application.

To get a list of recent calls, use the PHP Twilio Helper Library to retrieve call resources.

SDK Version:
  • 4.x
  • 5.x
<?php
    /* Get Twilio call logs. You can run this file by saving it
     * as call-log.php and running:
     *        php call-log.php
     */

    // Step 1: Get the Twilio-PHP library from twilio.com/docs/libraries/php, 
    // and move it into the folder containing this call-log.php file.
    require "Services/Twilio.php";

    // Step 2: Set our AccountSid and AuthToken from https://twilio.com/console
    $AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    $AuthToken = "your_auth_token";

    // Step 3: Instantiate a new Twilio Rest Client
    $client = new Services_Twilio($AccountSid, $AuthToken);

    try {
        // Get Recent Calls
        foreach ($client->account->calls as $call) {
            echo "Call from $call->from to $call->to at $call->start_time of length $call->duration \n";
        }
    } catch (Exception $e) {
        echo "Error: " . $e->getMessage();
    }
<?php
    /* Get Twilio call logs. You can run this file by saving it
     * as call-log.php and running:
     *        php call-log.php
     */

    // Step 1: Get the Twilio-PHP library from twilio.com/docs/libraries/php, 
    // following the instructions to install it with Composer.
    require_once "vendor/autoload.php";
    use Twilio\Rest\Client;
    
    // Step 2: Set our AccountSid and AuthToken from https://twilio.com/console
    $AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    $AuthToken = "your_auth_token";

    // Step 3: Instantiate a new Twilio Rest Client
    $client = new Client($AccountSid, $AuthToken);

    try {
        // Get Recent Calls
        foreach ($client->account->calls->read() as $call) {
            $time = $call->startTime->format("Y-m-d H:i:s");
            echo "Call from $call->from to $call->to at $time of length $call->duration \n";
        }
    } catch (Exception $e) {
        echo "Error: " . $e->getMessage();
    }
  • We request the Calls resource.
  • If no errors occur, we print out a list of the Call records.

Then run the script to retrieve your list of calls:

$ php call-log.php

For more information check out the Twilio REST API documentation. If you have questions, comments, or suggestions about this quickstart, then give us a howl on Stack Overflow.

Next: Turn your browser into a phone with Twilio Client »