Error: Voice call using twilo in php code - php

I using twilio for call system, but my error:
Error: Account not authorized to call +84983xxxxxx. Perhaps you need
to enable some international permissions:
https://www.twilio.com/user/account/settings/international
I checked in Vietnam (+84) via https://www.twilio.com/user/account/settings/international but still encounter the same error
My php code:
<?php
require 'twilio-php/Services/Twilio.php';
function call_notify($number) {
// Twilio REST API version
$version = "2010-04-01";
// Set our Account SID and AuthToken
$sid = 'AC34962eb3f445dfa40893e64bb8xxxxxx';
$token = '9b0667c68299cf484228344d67xxxxxx';
// A phone number you have previously validated with Twilio
$phonenumber = '+155920xxxxx';
// 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
$number, // The number of the phone receiving call
'http://103.255.238.29/notify.xml' // The URL Twilio will request when the call is answered
);
echo 'Started call: ' . $call->sid;
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
}
call_notify("+84983xxxxxx");
Please help me, thanks

Twilio developer evangelist here.
I clicked on the URL to the international settings but was redirected to the Messaging international settings.
Can you go to the Voice international settings here https://www.twilio.com/console/voice/settings/geo-permissions and ensure you have Vietnam checked on that page too.
I'll feedback that the error is pointing to the wrong page.
Let me know if that helps!

Related

Twilio - get error code on sms send failure

Our code sends twilio sms messages by doing the following:
// send the text message to the member's mobile phone
try {
// attempt to send the message through Twilio
$tw_msg = $twilio_client->messages->create(
"+1".$recipient['address'],
array (
'From' => "+1".$org['twilio_number'],
'Body' => $txtmsg,
'StatusCallback' => CALLBACK_LINK.'/text_message_handler.php'
)
);
// else trap the error since the message could not be sent and the callback routine is not called
} catch (Exception $e) {
// process the text message error
process_text_msg_error($db, $e, $org, $msg, $recipient);
}
In the v4 library we would get the error code by doing the following:
// get the twilio error components
$twilio_error_status = $e->getStatus();
$twilio_error_code = $e->getCode();
$twilio_error_msg = $e->getMessage();
This is not giving us what we expected using the V5 library. How do we get the error status and code using the V5 lib?
Twilio developer evangelist here.
It looks to me like you need to update one of the methods you call on the exception to get the status code. The exception is now a RestException and has the method getStatusCode(). You should update to:
// get the twilio error components
$twilio_error_status = $e->getStatusCode();
$twilio_error_code = $e->getCode();
$twilio_error_msg = $e->getMessage();
Let me know if that helps at all.

Error while making outbound call on twilio

I am new to twilio.I am using twilio trial account and I am using php.I have created my account and the verification code was sent to my number .But when i try to make a call i get the error
Error: The source phone number provided, +1937xxxxxx6, is not yet verified for your account. You may only make calls from phone numbers that you've verified or purchased from Twilio.
Here is my code
<?php
// Include the Twilio PHP library
require 'twilio-php-master/Services/Twilio.php';
// Twilio REST API version
$version = "2010-04-01";
// Set our Account SID and AuthToken
$sid = 'Axxxxxxxxxxxxxxxxxxffa58';
$token = 'f878xxxxxxxxxxxxxxxeb05';
// A phone number you have previously validated with Twilio
$phonenumber = '+19xxxxxxxx6';
// 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
'+91xxxxxxxx7', // The number of the phone receiving call
'http://demo.twilio.com/welcome/voice' // The URL Twilio will request when the call is answered
);
echo 'Started call: ' . $call->sid;
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
?>
It happened to me when using the test account/token.
I double checked that the my phone number is verified but it was giving me this error.
Then I replaced the test account with my real account sid/token and the call succeeds.
I consider this as a bug in their system.
tested with python 3.6, twilio 6.16.2

i want to record response get from statuscallback twilio

ini_set('max_execution_time', 3000);
require 'Services/Twilio.php';
$version = "2010-04-01";
$sid = 'xxxxxx';
$token = 'xxxxxx';
$phonenumber = 'xxxxxxxxxx';
$client = new Services_Twilio($sid, $token, $version);
try {
$call = $client->account->calls->create($phonenumber, "3104200693", "http://demo.twilio.com/docs/voice.xml", array(
"Method" => "GET",
"StatusCallback" => "http://localhost/twilio/call-response.php",
"StatusCallbackMethod" => "GET",
"StatusCallbackEvent" => array("initiated", "ringing", "answered", "completed"),
"IfMachine" => "Continue",
));
//header("Location: http://localhost/twilio/call-response.php");
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
This is my twilio code to call a number, i am successfully calling with this, but i want to store response in database like call completed or not and if answered then who answered call human or machine.
Please help.
The Twilio servers will send an HTTP request to the URL given as StatusCallback. Quite obviously, hopefully, Twilio cannot contact your localhost. 1) your computer is probably not physically reachable from outside your network, and 2) localhost is something different for everyone.
You need to provide a URL which is publicly accessible from the internet. That means you either need to run your code in a server, or you use a service like http://ngrok.com to establish a tunnel to your local machine during development.
I dont think "StatusCallback" => "http://localhost/twilio/call-response.php" will make you get call response. You should give a server url.
For example
"StatusCallback" => "http://example.com/twilio/call-response.php"
In call-response.php,
<?php
file_put_contents(__DIR__."/status_log.txt", json_encode($_REQUEST));
// twilio call status and other sata will be written to this file. Please check this file after call.
// I am giving because i dont know correct twilio returning params
?>
More info here

Using PHP to access Gmail using Service Account

I am trying to list emails from a standard Gmail account (not a Google Apps account) using OAuth2 and a Service Account (actually, I want to send emails – but that can wait).
I have created the Project, created the service account, downloaded the private key, and enabled the Gmail API (and the Calendar API). I can successfully access my calendar using code very similar to the code below. However, I am receiving the following error when attempting to list mail messages.
Error refreshing the OAuth2 token, message: error_description "Unauthorized client or scope in request."
(For info. If I comment out the $credential->sub = … line, I get the following error: “(500) Backend Error” and I can see that this is logged in the Usage tab of the Developers API page).
Can anyone confirm that what I am trying to do is possible? And if so, any ideas where to go next?
Here is my code:
class mail
{
private $service;
public function __construct($clientid, $keyfile, $account_name, $app_name)
{
$client = new Google_Client();
$client->setApplicationName($app_name);
$this->service = new Google_Service_Gmail($client);
// Load the key in PKCS 12 format
$key = file_get_contents($keyfile);
$client->setClientId($clientid);
$credentials = new Google_Auth_AssertionCredentials(
$account_name, array('https://mail.google.com/'),$key);
$credentials->sub = 'myemailaddress...#gmail.com';
$client->setAssertionCredentials($credentials);
}
public function list_mail()
{
try
{
$result = $this->service->users_messages->listUsersMessages("me");
}
catch (Exception $e)
{
throw new Exception("Gmail API error: ".$e->getMessage()."<br />");
}
return $result;
}
}
$mail_obj = new mail(CLIENT_ID, KEY_FILE, SERVICE_ACCOUNT_NAME, APP_NAME);
$result = $mail_obj->list_mail();

How to access the callback status twilio when the call is finished?

I'm trying to access the status AFTER the call is done, and display it in the first page, not in the callback page.
i have calling page:
require 'twilio/Services/Twilio.php';
$sid = "xxx"; // Your Account SID from www.twilio.com/user/account
$token = "xxx"; // Your Auth Token from www.twilio.com/user/account
$client = new Services_Twilio($sid, $token);
try {
$call = $client->account->calls->create(
'000', // From a valid Twilio number
$number, // Call this number
// Read TwiML at this URL when a call connects (hold music)'
"http://myurl.com/voice.xml", array('Method' => 'GET','StatusCallback'=>'http://myurl.com/callback.php'));
if($call->status =='failed')result(array('status'=>'failed status '));
else result(array('status'=>$call->status));
} catch (Exception $e) {
result(array('status'=>'not a valid number'));
}
my issue is that i'm not understanding how i can get the $_POST info that twilio will send to my callback.php on my calling page.php....
Please read Twilio API explaintion Click here for reference from Twilio Site to know the post parameters from Twilio on your callback back page.
http://myurl.com/callback.php
$callDur = $_POST['CallDuration'];
$recdDur = $_POST['RecordingDuration'];
To get these values on other page store them in session and use them where you want.

Categories