I am testing Twilio's API in PHP. Currently I have a working module which allows me to place calls and record them. Now I am writing a module to report on those calls.
The code below is supposed to fetch a filtered list of calls and present my browser with a little info about that call, as well as link to the audio recording. This script fetches call logs. For each call it then calls a function to fetch the recording belonging to the current call. Problem is, it fetches the same audio recording every time.
$version = '2010-04-01';
// Set our AccountSid and AuthToken
$sid = 'abc123';
$token = 'fbc123';
// Your Account Sid and Auth Token from twilio.com/user/account
$client = new Services_Twilio($sid, $token,$version);
$dt = date("Y-m-d");
// 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-08-04",
"StartTime<" => "$dt"
)) as $call
) {
echo $call->sid.", ".$call->duration.", $".abs($call->price)." ".getRecording($call->sid)."<br/>";
}
function getRecording($callsid){
// Twilio REST API version
$version = '2010-04-01';
// Set our AccountSid and AuthToken
$sid = 'abc123';
$token = 'fbc123';
$client = new Services_Twilio($sid, $token);
// Loop over the list of recordings and echo a property for each one
foreach ($client->account->recordings->getIterator(0, 50, array(
"callSid" => '$callsid'
)) as $recording
) {
return " ->".$callsid." <strong><a href='http://api.twilio.com".$recording->uri."'>Audio</a></strong>";
}
}
The output is this (please notice that every audio file has the same URL):
CAab40cacf1690a86e604ba0f527153887, 1, $0.015 ->CAab40cacf1690a86e604ba0f527153887 <strong><a href='http://api.twilio.com/2010-04-01/Accounts/acctnumber/Recordings/REe9a199dec7376ef94d6af256749e7d81'>Audio</a></strong>
CAaf5629839a6d2095067a04359dc13809, 14, $0.015 ->CAaf5629839a6d2095067a04359dc13809 <strong><a href='http://api.twilio.com/2010-04-01/Accounts/acctnumber/Recordings/REe9a199dec7376ef94d6af256749e7d81'>Audio</a></strong>
CAa8610e49f6e49a71c8bf3e02d3e974f1, 11, $0.015 ->CAa8610e49f6e49a71c8bf3e02d3e974f1 <strong><a href='http://api.twilio.com/2010-04-01/Accounts/acctnumber/Recordings/REe9a199dec7376ef94d6af256749e7d81'>Audio</a></strong>
CA478704a99883f919a9932b52c6971cf7, 21, $0.015 ->CA478704a99883f919a9932b52c6971cf7 <strong><a href='http://api.twilio.com/2010-04-01/Accounts/acctnumber/Recordings/REe9a199dec7376ef94d6af256749e7d81'>Audio</a></strong>
CA00b2f9db896e3b8cfc82c93df5c8e11e, 9, $0.015 ->CA00b2f9db896e3b8cfc82c93df5c8e11e <strong><a href='http://api.twilio.com/2010-04-01/Accounts/acctnumber/Recordings/REe9a199dec7376ef94d6af256749e7d81'>Audio</a></strong>
CAcbd21d8dd3de1c06ce1f393c987bc6c7, 19, $0.015 ->CAcbd21d8dd3de1c06ce1f393c987bc6c7 <strong><a href='http://api.twilio.com/2010-04-01/Accounts/acctnumber/Recordings/REe9a199dec7376ef94d6af256749e7d81'>Audio</a></strong>
CAffb1d60f5f48b870af65329d7d4ca48f, 4, $0.015 ->CAffb1d60f5f48b870af65329d7d4ca48f <strong><a href='http://api.twilio.com/2010-04-01/Accounts/acctnumber/Recordings/REe9a199dec7376ef94d6af256749e7d81'>Audio</a></strong>
CA44fd1b5b9ef347f730d068abafffbd73, 15, $0.015 ->CA44fd1b5b9ef347f730d068abafffbd73 <strong><a href='http://api.twilio.com/2010-04-01/Accounts/acctnumber/Recordings/REe9a199dec7376ef94d6af256749e7d81'>Audio</a></strong>
Twilio developer evangelist here.
The parameters for queries are case sensitive so you need to capitalize the c in callSid. Also wrapping a string in single quotes doesn't substitute it.
foreach ($client->account->recordings->getIterator(0, 50, array(
"callSid" => '$callsid'
to
foreach ($client->account->recordings->getIterator(0, 50, array(
"CallSid" => $callsid
Please let me know if I can help further!
edit: To clarify what was happening, a request was being made to get all the recordings in the account since the query param was off and each time it was taking the first one of the collection and returning that. Thus causing them to all be the same.
Related
I am trying to send bulk SMS using Twilio notify API. I had looked at the documentation, and other StackOverflow resources but did not find the issue yet. What I am doing is:
$sid = "AC1e590cbb8eee064c3c71axxxxxxxxxxx";
$token = "94c2dc3e2e407c4ebd28cxxxxxxxxxxx";
$twilio = new Client($sid, $token);
$serviceSid = "IS32913ae9b083b809b1c06xxxxxxxxxxx";
$recipients = array();
foreach($phone_nos as $phone_no) {
array_push($recipients, $phone_no['phone_no']);
}
//recipients array print value is
//Array
//(
//[0] => +923105653361
//[1] => +923491457062
//)
$binding = array();
foreach ($recipients as $recipient) {
$binding[] = '{"binding_type":"sms", "address":"'.$recipient.'"}';
}
//binding array is print value is
//Array
//(
//[0] => {"binding_type":"sms", "address":"+923105653361"}
//[1] => {"binding_type":"sms", "address":"+923491457062"}
//)
$service = $twilio->notify->v1->services->create();
$notification = $twilio->notify->services($serviceSid)
->notifications->create([
"toBinding" => $binding,
"body" => 'Test message 5 notify'
]);
echo $notification->body;
echo '<pre>';print_r($notification->sid);exit;
The notify console is showing the messages are sent with no error.
Twilio developer evangelist here.
It looks like you haven’t connected a messaging service and a phone number to your Notify service. Follow the instructions in the documentation here to set that up, then try sending the messages again.
Twilio calls gets automatically disconnect after one ring. When it's the case for cell phone, everything works just fine but for the landline having ring back-tone the call gets disconnected after one ring. I have checked the call logs; and the status for each call was completed but the duration of each call was 3sec.
This is the code that I have wrote:
$sid = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$twilio = new Client($sid, $token);
$call = $twilio->calls
->create("+1XXXXXXX", // to
"+1XXXXXX", // from
[
"twiml" => "<Response><Say>Hi, A new order has been made by a customer!</Say></Response>"
]
);
I saw an example in twilio: https://www.twilio.com/docs/sms/tutorials/how-to-confirm-delivery-php
<?php
$sid = $_REQUEST['MessageSid'];
$status = $_REQUEST['MessageStatus'];
openlog("myMessageLog", LOG_PID | LOG_PERROR, LOG_USER);
syslog(LOG_INFO, "SID: $sid, Status: $status");
closelog();
I don't know what the code above exactly do, but what I want is to save the data to my local database.
The code in my post method(my statuscallback):
public function smsStatusCallback(Request $request){
$sms = SmsChannel::create([
'number' => $request['MessageSid'],
'body' => $request['MessageStatus'],
]);
}
I've found a solution already. I saw the possible solutions in twilio debugger: "Double check that your TwiML URL does not ...". So I tried making it as a twiml
public function smsStatusCallback(Request $request){
$response = new Twiml();
$sms = SmsChannel::create([
'sid' => $request['MessageSid'],
'status' => $request['MessageStatus'],
]);
return response($response)
->header('Content-Type', 'text/xml');
}
I've added my route to api.php since the URL should be accessible by twilio.
Route::post('sms-status-callback','CommunicationController#smsStatusCallback');
Our app brings together several participants into a conference. The Host, uses twilio client, while participants use either phone lines or twilio clients.
The host need to know when each participant joins the conference.
is there a way via the RESTful API to get in real-time who joined the conference?
Here's how to get the participants' numbers:
<?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
$sid = "ACXXXXX";
$token = "YYYYY";
$client = new Services_Twilio($sid, $token);
$response = new Services_Twilio_Twiml();
if( isset($_REQUEST['ConferenceSid']) ){
$participants = $client->account->conferences->get( $_REQUEST['ConferenceSid'] )->participants;
$cnt = count( $participants );
$response->Say( "There are ".$cnt." callers in this conference" );
foreach ($participants as $participant) {
$call = $client->account->calls->get( $participant->callsid );
$response->Say( $call->from );
}
}
$response->Redirect("conferencemod.xml");
print $response;
?>
Taken from: https://www.twilio.com/blog/2014/09/roll-call-roger-stringer-shows-you-how-to-take-a-headcount-during-a-twilio-conference-call.html
You can probably modify it to return $call->StartTime additionally. That would let the moderator know who called and when their call started. I hope that helps.
I have a twitter bot, where it searches for an #mention and replies to the user depending on what the user says.
It was working fine until this week, when I started getting this error:
Warning: Invalid argument supplied for foreach() in /home/reportax/public_html/reportaxi/twitterbot/config.php on line 14
I stripped down the code to the most basic form, which is the searching for the #mention and then tweeting something when it finds it, but I'm still getting this error. Any ideas?
As I mentioned before, this was working fine until this week, so I know the consumer key, secret, and all that is OK.
here's the code:
<?php
require_once('twitteroauth.php');
define('CONSUMER_KEY', 'MYKEY');
define('CONSUMER_SECRET', 'MYSECRET');
define('ACCESS_TOKEN', 'MYTOKEN');
define('ACCESS_TOKEN_SECRET', 'MYTOKENSECRET');
$twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
$twitter->host = "http://search.twitter.com/";
$search = $twitter->get('search', array('q' => '#repor_taxi', 'rpp' => 15));
$twitter->host = "https://api.twitter.com/1/";
foreach($search->results as $tweet) {
$status = 'RT #'.$tweet->from_user.' '.$tweet->text;
if(strlen($status) > 140) $status = substr($status, 0, 139);
$twitter->post('statuses/update', array('status' => $status));
}
?
any ideas?
I had the same problem. You need update your code for twitter API 1.1.
<?php
require_once('twitteroauth.php');
define('CONSUMER_KEY', 'MYKEY');
define('CONSUMER_SECRET', 'MYSECRET');
define('ACCESS_TOKEN', 'MYTOKEN');
define('ACCESS_TOKEN_SECRET', 'MYTOKENSECRET');
$twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
/*The search has change a little bit */
/* Remove this 2 lines */
/* $twitter->host = "http://search.twitter.com/"; */
/* $search = $twitter->get('search', array('q' => '#repor_taxi', 'rpp' => 15)); */
/* Put this new line */
$search = $twitter->get("https://api.twitter.com/1.1/search/tweets.json?q=#repor_taxi&count=15");
/* The Search URL is https://api.twitter.com/1.1/search/tweets.json?q= */
/* Everything after is parameter */
/* You can check parameters list here: https://dev.twitter.com/docs/using-search */
/* Twitter host updated too */
$twitter->host = "https://api.twitter.com/1.1/";
foreach($search as $tweet) {
...
?>
I hope this may help you. Good luck.