Twilio Twilio/Rest/Calls.php error - twilio

whenever I am trying to make a conference call it says application error and I get an error in error log as :
PHP Warning: strlen() expects parameter 1 to be string, array given in /home/aan/public_html/twilio/twilio-php-4.11.0/Services/Twilio/Rest/Calls.php on line 16
Here is the code
<?php
require("twilio-php-4.11.0/Services/Twilio/Twiml.php");
if($_REQUEST['Digits'] != '1') {
header("Location: twiml.php");
die;
}
$MODERATOR = $_GET['phone'];
$response = new Services_Twilio_Twiml();
$dial = $response->dial($MODERATOR);
$dial->conference('My conference', array(
'startConferenceOnEnter' => True
));
I have already made the call and gathered the digit , but when I dial second number and try to make these as conference I get this error

I usually use TwiML for making a conference call, It's simple to implement.
$my_conference = "My Conference";
$statusCallbackUrl = "https://example.net/Welcome/conference_control"; // call back url
<Response>
<Dial>
<Conference beep="false" statusCallback="<?php echo $statusCallbackUrl; ?>"
statusCallbackEvent="start end join leave mute hold" endConferenceOnExit="true"
startConferenceOnEnter="true">
<?php echo $my_conference; ?>
</Conference>
</Dial>
</Response>
Hopefully, it will help you.

header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<Dial><?php echo $phone ?></Dial>
</Response>
The snippet here was OP's fix for encountering an error using twilio-php library.

Related

Twilio handling calls

I am trying to achive is whenever I receive an text message with certain keywords, I will get a call saying that I have received a text message and press 1 to talk to the person who sent the text message.
I have done it by creating three files, these are below
Twilio.php
This receives the post request on a text message and call me
$name = $_POST['name'];
$phone = $_POST['phone'];
$client = new Services_Twilio($AccountSid, $AuthToken);
try {
// make call
$call = $client->account->calls->create(
$caller,
$number,
array("url" => "http://somewebsite.net/twilio/twiml.php?phone=$phone&name=$name")
);
} catch (Exception $e) {
echo 'Error starting phone call: ' . $e->getMessage();
}
The next file if twiml.php which handle the call and ask me if I want to speak and press 1 to speak
twiml.php
$nm = $_GET['name'];
$ph = $_GET['phone'];
$name = "Deepak";
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<Say>Hello <?php echo $name ?>.</Say>
<Gather numDigits="1" action="http://somewebsite.net/twilio/call.php?phone=<?php echo $ph ?>" method="POST">
<Say>You have a text message, press 1 to speak.</Say>
</Gather>
</Response>
The third file is called if I press 1 to speak and then it dials that number : below is the code:
Call.php
<?php
if($_REQUEST['Digits'] != '1') {
header("Location: twiml.php");
die;
}
$ph = $_GET['phone'];
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<Dial>+1 <?php echo $ph ?></Dial>
</Response>
I am trying to achieve is to merge these three files into one so I need not to make a Post request and I could handle the whole call in a single file or at least I can merge twiml.php and call.php
Is there a way to combine these?
Twilio developer evangelist here.
You could put it all into one file and use the URI that was requested ($_SERVER['REQUEST_URI']) to switch on the code you then run. Or, it might be more interesting to try a small PHP framework that abstracts that away. Something like Slim or Silex might work for you.
I would ask why you need to run this from one file when your feature looks like it works?

when we make call using rest api then what twiml we use on that url parameter

I am Creating the call using the rest api-
try{
// Initiate a new outbound call
$call = $this->client->calls->create(
// to call.
"num2",
// Step 5: Change the 'From' number below to be a valid Twilio number
// that you've purchased or verified with Twilio.
"num1",
array("url" => "url-tw",
'IfMachine'=>'Continue')
);
echo "Started call: " . $call->sid;
} catch(Exception $e){
echo "Error: " . $e->getMessage();
}
and on the url-tw what twiml should I use which can't disconnect the call.
Before I was handling the call using the TwiML but now I have to detect the AnsweredBy option which is only available if I make the call using the REST API so.
for now I m using the same twiml I have used before when I was making calls using the twiML like use the <Dial> which let to dial again but if I dont use any twiml it disconnect the call.So any advice where I m going wrong.
Twilio evangelist here.
The value of the url parameter should be a publicly accessible URL that returns TwiML containing the instructions that you want Twilio to execute to the caller.
Your PHP to start the call would look like:
// Initiate a new outbound call
$call = $this->client->calls->create(
"num2",
"num1",
array("url" => "http://example.com/answer.php", 'IfMachine'=>'Continue')
);
Then in answer.php, you can do two things:
Check the AnsweredBy parameter to see if Twilio detected a machine or a human.
Generate the Twiml you want to return based on that value
For example to say something different to a machine v a human, you could do something like this in your PHP:
<?php
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<?php if ($_REQUEST['AnsweredBy']=='machine'): ?>
<Response>
<Say>Hello machine</Say>
</Response>
<?php else: ?>
<Response>
<Dial>+15555555555</Dial>
</Response>
<?php endif ?>
Hope that helps.

Make a call between two numbers not registered in twilio

There's some way of make a call between two of my users? I mean... I have a twilio acount with a registered number and I have to make a call to my client "Bill" so when he answer that, the call should be redirected to another client, that Bill choosed, let's say "Joe". So, Bill click's a button and he's phone rings, he answer that and start to call Joe. When some of them hangup, the all call should be over. Have someone ever made that? Help me! And I'm sorry about my bad english
(oh yeah, I'm using php for that)
This is just something simple to get you going, you can also look at connecting to a conference room https://www.twilio.com/docs/api/twiml/conference
You will need to use the Twilio PHP Helper Library (the "Services" folder from there) download from https://www.twilio.com/docs/libraries/php#install-via-zip-file
Project structure
/
/Services
/callBill.php
/callJoe.php
callBill.php
<?php
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<Response>
<!-- // calling Bill -->
</Response>
<?php
// Include the Twilio PHP library
require 'Services/Twilio.php';
// Twilio REST API version
$version = "2010-04-01";
// Set our Account SID and AuthToken
$sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// A phone number you have at Twilio
$phonenumber = '5557779999';
// The URL Twilio will request when the call is answered
$twilioRequestUrl = "http://somewebsite.xyz/callJoe.php";
// Instantiate a new Twilio Rest Client
$client = new Services_Twilio($sid, $token, $version);
try {
// Initiate a new outbound call
$call = $client->account->calls->create(
$phonenumber, // The number of the phone initiating the call
'7779995555', // call Bill at 7779995555
$twilioRequestUrl
);
//echo 'Started call: ' . $call->sid;
} catch (Exception $e) {
//echo 'Error: ' . $e->getMessage();
}
?>
callJoe.php
<?php
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
<!-- call and connect Joe at 9995557777 with Bill's caller ID-->
<Response>
<Pause length="2"/>
<Say>Please wait, I'm calling Joe.</Say>
<Dial callerId="7779995555">9995557777</Dial>
</Response>
Request http://somewebsite.xyz/callBill.php with your browser or with a click on a button.

What is the best way to add CNAM (Caller ID Name) to a SMS processed through twilio

My twilio app is just like this sample app
<?php
header('Content-Type: text/html');
?>
<Response>
<Message to="<?=$_REQUEST['PhoneNumber']?>">
<?=htmlspecialchars(substr($_REQUEST['From'] . ": " . $_REQUEST['Body'], 0, 160))?>
</Message>
</Response>
I would like to add the CNAM (Caller ID name) to the message when I send it on to the destination, but twilio support says:
We unfortunately do not expose or provide caller ID name in the API response.
Is there any work around or other way to do this?
Try this :
<?php
header("content-type: text/xml");
?>
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Message from="<?php echo $_REQUEST["To"]; ?>" to="+1<?php echo $_REQUEST['FrwdNum']; ?>">
<?php echo htmlspecialchars(substr(substr($_REQUEST['From'],0,10) . ":" . $_REQUEST['Body'], 0, 160)); ?>
</Message>
</Response>
If your script is www.example.com/answer.php, then give below url as sms url for your twilio number
www.example.com/answer.php?FrwdNum=9876543210
You can also use Twilio Lookup to get the CNAM of a number.
Using the REST API, you would specify the CallerName property in your GET request:
https://www.twilio.com/docs/api/lookups#lookups-caller-name
And the output would look like:
{
"caller_name": "Caller Name",
...
...
}
Caller ID/CNAM is only displayed on landlines. Cell phones do NOT support CNAM because carriers defer to your personal contact list to populate names on inbound calls. The terminating carrier (the carrier for the number you are calling) is responsible for displaying CNAM.

Dialing Fallback with Twilo

I am working on my inbound TwiML. At one point I want to attempt to forward a call to my cell phone number.
So I got 5555555555 but I want to redirect to a different TwiML script if there is no answer or no human answer. For example if I do not answer and Twilo gets my Cell VM, I would instead want to have Twilo try someone else in another TwiML script rather than let them leave a message on my personal VM.
I know I can set it up so I have to press 1 to connect the call, then I am assuming if I don't press 1 it would continue processing the TwiML after is this possible without requiring me to press 1?
Joel from Twilio here.
The best way to do what you want is to use the call screening process that you describe ("press 1 to connect the call"). This is because automatically detecting voice mail is a very error prone process.
We have an in-depth Call Screening HowTo on how to do this. Here's the overview:
Create a file called "attempt_call.php" on your web server that contains the following code (pay particular attention to the $numbers array on line 4):
<?php
// Set the numbers to call
$numbers = array("<number to call 1>", "<number to call 2>", "<number to call n>");
$number_index = isset($_REQUEST['number_index']) ? $_REQUEST['number_index'] : "0";
$DialCallStatus = isset($_REQUEST['DialCallStatus']) ? $_REQUEST['DialCallStatus'] : "";
header("content-type: text/xml");
// Check the status of the call and
// that there is a valid number to call
if($DialCallStatus!="completed" && $number_index<count($numbers)){
?>
<Response>
<Dial action="attempt_call.php?number_index=<?php echo $number_index+1 ?>">
<Number url="screen_for_machine.xml">
<?php echo $numbers[$number_index] ?>
</Number>
</Dial>
</Response>
<?php
} else {
?>
<Response>
<Hangup/>
</Response>
<?php
}
?>
Next, in the same location as the "attempt_call.php" file, create a file called "screen_for_machine.xml" with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Gather action="complete_call.xml">
<Say>Press any key to accept this call</Say>
</Gather>
<Hangup/>
</Response>
Then, in the same location as the "attempt_call.php" file, create a file called "complete_call.xml" that contains the following:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Gather action="complete_call.xml">
<Say>Press any key to accept this call</Say>
</Gather>
<Hangup/>
</Response>
Finally, set the "Voice Request URL" on your Twilio number to the full publicly available URL for the "attempt_call.php" file you created.
For more details, all of this code above is also available with mode in-depth explanation on the Call Screening HowTo which is available on our website.

Resources