Search code examples
twiliotwilio-php

Twilio REST API How to get Sid


I am still new to Twilio. I am writing a PHP script which will connect to Twilio and parse some information about calls. I am using the code from this page:

https://www.twilio.com/docs/api/rest/call

Here is my code:

require_once '../twilio-library/Services/Twilio.php';

// Twilio REST API version
$version = '2010-04-01';

// Set our AccountSid and AuthToken
$sid = 'abc132xxxxx';
$token = 'xxxxeeffv';

// Instantiate a new Twilio Rest Client
$client = new Services_Twilio($sid, $token, $version);

// Loop over the list of calls and echo a property for each one
foreach ($client->account->calls as $call) {
    echo "->".$call->Sid."<- <br/>";


}

The browser just outputs many blanks. No Sid values. What am I doing wrong?


Solution

  • Twilio Developer Evangelist here.

    Try doing the following to get call information.

    <?php
    require_once '../twilio-library/Services/Twilio.php';
    
    // Your Account Sid and Auth Token from twilio.com/user/account
    $sid = 'abc132xxxxx';
    $token = 'xxxxeeffv';
    $client = new Services_Twilio($sid, $token);
    
    // Loop over the list of calls and echo a property for each one
    foreach ($client->account->calls as $call) {
        echo $call->sid;
    }
    

    Notice that if you're using the latest version of the library, you don't need to specify a version on your request. Double check that the path ../twilio-library/Services/Twilio.php is correct for you though.

    If you're only testing, you could move the file Twilio.php to the same directory where your code is and just import Twilio.php on it.

    If you then decide to filter the logs by date, here's how you would do it.

    // Loop over the list of calls and echo a property for each one
    foreach ($client->account->calls->getIterator(0, 50, array(
            "Status" => "completed",
            "StartTime>" => "2015-03-01",
            "StartTime<" => "2015-05-10"
        )) as $call
    ) {
        echo $call->sid;
    }
    

    Let me know how it goes.