Twilio REST API How to get Sid - twilio

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?

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.

Related

System Initiated IVR Call to trap Voice Response

I need to make an IVR app in php where the syetm initiates the outbound call and traps a voice response from user.
How may i go about doing this?
This is my code (does not trap response from user)
voice.xml contents
<?xml version="1.0" encoding="UTF-8"?>
<!-- page located at http://example.com/complex_gather.xml -->
<Response>
</Response>
completed.php contents
file_put_contents('voice.txt',$_SERVER['QUERY_STRING']);
Main Code
<?php
require_once 'vendor/autoload.php';
use Twilio\Rest\Client;
use Twilio\TwiML\VoiceResponse;
// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
$sid = "xxx";
$token = "xxx";
$twilio = new Client($sid, $token);
$call = $twilio->calls
->create("+1310XXXXXXX", // to
"+15676777774", // from
[
"method" => "GET",
"statusCallbackMethod" => "POST",
"url" => "http://xxxx.com/voice.xml"
]
);
$response = new VoiceResponse();
$gather = $response->gather(['action' => '/completed.php','method' => 'GET', 'input'=>'speech','timeout'=>3,''=>'true','speech_model'=>'phone']);
$gather->say('Enter something, or not');
echo $response;
Twilio developer evangelist here.
When you create a call through the Twilio API, as you are doing, you pass a URL parameter.
When the call connects, Twilio will make a webhook (HTTP) request to that URL and will follow the instructions that are returned in the form of TwiML.
Currently your /voice.xml endpoint returns an empty <Response/> which will cause the call to hang up as there is no TwiML to execute.
Instead of returning an empty <Response/> from /voice.xml you should return the TwiML you generate at the end of your script.
So, perhaps voice.xml should be voice.php and look a bit like this:
<?php
require_once 'vendor/autoload.php';
use Twilio\Rest\Client;
use Twilio\TwiML\VoiceResponse;
header('Content-Type: application/xml');
$response = new VoiceResponse();
$gather = $response->gather(['action' => '/completed.php','method' => 'GET', 'input'=>'speech','timeout'=>3,''=>'true','speech_model'=>'phone']);
$gather->say('Enter something, or not');
echo $response;
You should also return TwiML from your completed.php endpoint too.
<?php
file_put_contents('voice.txt',$_SERVER['QUERY_STRING']);
header('Content-Type: application/xml');
?>
<Response/>

How to switch an ongoing Twilio phone call to a <Pay> connector

I would like to use the < Pay> connector optionally while in an ongoing phone call. I cannot find out how to do trigger a new resource during an ongoing phone call.
You can modify an "in progress" call by passing new TwiML (XML) to execute which could contain your "<Pay>".
You must provide
the ID of the call you want to modify (the "CallSid" "CAe1644a7eed5088b159577c5802d8be38")
and an URL where Twilio will find the instructions (the "Url" "http://demo.twilio.com/docs/voice.xml")
I don't know what language you're using but in PHP with Twilio's library the code would look someting like this:
// see https://getcomposer.org/doc/01-basic-usage.md
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;
// Find your Account Sid and Auth Token at twilio.com/console
// DANGER! This is insecure. See http://twil.io/secure
$sid = "ACc0966dd96e4d55d26ae72df4d6dc3494";
$token = "your_auth_token";
$twilio = new Client($sid, $token);
$call = $twilio->calls("CAe1644a7eed5088b159577c5802d8be38")
->update(array(
"method" => "POST",
"url" => "http://demo.twilio.com/docs/voice.xml"
)
);
print($call->to);
You can read more about this here
(https://www.twilio.com/docs/voice/tutorials/how-to-modify-calls-in-progress).

Twilio Queue Sid

I am using the following code that I got from the twilio website. I need to get the QueueSid of my queue named "Sales". How do I go about doing this? If there is documentation for this subject please point me there as well. Thanks in advance!
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('/path/to/twilio-php/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$accountSid = "ACYYYYYYYYYY";
$authToken = "XXXXXXXXX";
$client = new Services_Twilio($accountSid,$authToken);
// Get an object from its sid. If you do not have a sid,
// check out the list resource examples on this page
$member = $client->account->queues->get('QU5ef8732a3c49700934481addd5ce1659')->members->get("Front");
$member->update(array(
"Url" => "https://dl.dropboxusercontent.com/u/11489766/learn/voice.xml",
"Method" => "POST"
));
echo $member->wait_time;
Twilio developer evangelist here.
You can search for all of your queues using the Queues list resource. Then you will want to filter by the Friendly name to get your queue. Try something like this:
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('/path/to/twilio-php/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$accountSid = "ACYYYYYYYYYY";
$authToken = "XXXXXXXXX";
$client = new Services_Twilio($accountSid, $authToken);
foreach($client->account->queues as $queue){
if ($queue->friendly_name == "Sales"){
$foundQueue = $queue;
break;
}
}
echo $foundQueue;

Twilio - Carrier Lookup

How do I access the phone carrier with Twilio's carrier lookup?
Here is some of my sample code:
require_once('twilio/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "--------------";
$token = "------------------";
$client = new Lookups_Services_Twilio($sid, $token);
$number = $client->phone_numbers->get("5555555555", array("CountryCode" => "US", "Type" => "carrier"));
//How do I access the carrier here?
echo $number->phone_number;
Twilio developer evangelist here.
When you call for the carrier details, they are all returned as an object on the number called carrier. You can see this in the example response on the Twilio Lookup page. So, with your code:
$number = $client->phone_numbers->get("5555555555", array("CountryCode" => "US", "Type" => "carrier"));
echo $number->carrier->name;
echo $number->carrier->type;
With Twilio 5.x SDK, things changed very slightly:
use Twilio\Rest\Client as Twilio;
$client = new Twilio("sid", "token");
$response = $client->lookups->phoneNumbers("+15551234567")->fetch(["type" => "carrier"]);
echo $response->carrier["type"] . "\r\n";
echo $response->carrier["name"];
See https://www.twilio.com/docs/api/lookups for the complete current documentation.
When fetching "type" => "caller-name", the response can be unwound with:
echo $response->callerName["caller_name"];
camelCase, hyphen and dash all in one use case!

Twilio Error : Authenticate

Hello Developers I am using the Twilio API and Purchased a Number.
I have also created a secure SSL site uploaded the following code. Then it will show the following error as
Error : Authenticate
<?php
// Include the Twilio PHP library
require 'Services/Twilio.php';
// Twilio REST API version
$version = '2010-04-01';
// Set our AccountSid and AuthToken
$sid = 'myWorkingSID';
$token = 'myWorkingAuthToken';
// Instantiate a new Twilio Rest Client
$client = new Services_Twilio($sid, $token, $version);
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";
}
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
Kindly help me in this issue
I had exactly the same issue. In my case, I had a subaccount. And each subaccount has its own account SID and Auth Token. As soon as I used the subaccount credentials it worked (and by "it worked" I mean I got a different error, which is always good news).

Resources