I would like to make a voice call, using the twilio php library.
I downloaded the php helper library from the mentioned Github link on the twilio website.
Then I used the php code that is provided there too:
<?php
// Include the bundled autoload from the Twilio PHP Helper Library
require __DIR__ . '/twilio-php-main/src/Twilio/autoload.php';
use Twilio\Rest\Client;
// Your Account SID and Auth Token from twilio.com/console
$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = 'your_auth_token';
// In production, these should be environment variables. E.g.:
// $auth_token = $_ENV["TWILIO_ACCOUNT_SID"]
// A Twilio number you own with Voice capabilities
$twilio_number = "+15017122661";
// Where to make a voice call (your cell phone?)
$to_number = "+15558675310";
$client = new Client($account_sid, $auth_token);
$client->account->calls->create(
$to_number,
$twilio_number,
array(
"url" => "http://demo.twilio.com/docs/voice.xml"
)
);
Unfortunately I am getting the error
Parse error: syntax error, unexpected ':', expecting ';' or '{' in /users/USER/www/twilio/twilio-php-main/src/Twilio/autoload.php on line 61.
I assumed that their can´t be an error in the library, and couldn´t find or solve it myself. Does somedbody have an idea?
Related
I'm trying to use Twilio's API to send a sms with PHP.
I followed the tutorial as it is presented on their website but it doesn't work. I correctly replaced the $sid, $token, "from number" and "to number" variables. I also tested it with Phyton and it worked correctly, but with PHP I get the following error:
Fatal error: Uncaught Twilio\Exceptions\ConfigurationException:
Credentials are required to create a Client in
C:\xampp\htdocs\Php\Twilio\vendor\twilio\sdk\src\Twilio\Rest\Client.php:172
Stack trace: #0 C:\xampp\htdocs\Php\Twilio\index.php(13):
Twilio\Rest\Client->__construct('', '') #1
I'm using PHP 7.3.21 and xampp. I've saw this same error with people using laravel and more complex codes, but I'm using "pure" PHP.
The code I'm using:
<?php
// Update the path below to your autoload.php,
// see https://getcomposer.org/doc/01-basic-usage.md
require_once 'vendor/autoload.php';
use Twilio\Rest\Client;
// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
$sid = getenv("TWILIO_ACCOUNT_SID");
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio = new Client($sid, $token);
$message = $twilio->messages
->create("+15558675310", // to
[
"body" => "This is the ship that made the Kessel Run in fourteen parsecs?",
"from" => "+15017122661"
]
);
print($message->sid);
You can find the tutorial that I followed here: https://www.twilio.com/docs/sms/tutorials/how-to-send-sms-messages-php
It was a problem with the environment variables. I hardcoded them but the getenv() was returning an empty string and I don't know why it was happening. So to fix it I set them following this tutorial (for windows): phoenixnap.com/kb/windows-set-environment-variable. For linux you can try using these commands:
export ACCOUNT_SID="YOUR_ACCOUNT_SID"
export AUTH_TOKEN="YOUR_AUTH_TOKEN"
And then, in your code, for windows and linux:
$sid = getenv('ACCOUNT_SID');
$token = getenv('AUTH_TOKEN');
Make sure to restart xampp (if you're using it) and your computer after setting your variables.
<?php
// Include the bundled autoload from the Twilio PHP Helper Library
require __DIR__ . '/vendor/autoload.php';
use Twilio\Rest\Client;
// Your Account SID and Auth Token from twilio.com/console
$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = '8XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
// In production, these should be environment variables. E.g.:
// $auth_token = $_ENV["TWILIO_ACCOUNT_SID"]
// A Twilio number you own with Voice capabilities
$twilio_number = "+12244073476";
// Where to make a voice call (your cell phone?)
$to_number = '+22678733599';
$client = new Client($account_sid, $auth_token);
try {
$client->account->calls->create(
$to_number,
$twilio_number,
array(
// two url can't function because the first url will be erase by the value of the second
"url" => "http://demo.twilio.com/docs/voice.xml"
)
);
// echo "Started call : " . $client->sid;
} catch (Exception $e) {
echo "Error : " . $e->getMessage();
}
?>
When I start the call to the phone number from twilio number, it's okay. But it is impossible to make communication.
How can I make communication after answering the call?
Twilio developer evangelist here.
Your title says you want to connect a browser to a phone. However, your code only shows use of the REST API.
To create calls from a browser to a phone you need to use the Twilio Voice SDK. I would recommend that you go through the instructions to run the Voice SDK quick start application. That will explain how making a call from a browser to a phone works and give you an example application to run and investigate. The quick start application is fully documented.
PHP Fatal error: Uncaught exception 'Twilio\Exceptions\RestException' with message '[HTTP 400] Unable to create record: Account not authorized to call +126878****. Perhaps you need to enable some international permissions: https://www.twilio.com/user/account/settings/international' in /home/gwalioro/public_html/swifnix.com/works/call/twilio-php-master/Twilio/Version.php:85
Code Here
require __DIR__ . '/twilio-php-master/Twilio/autoload.php';
use Twilio\Rest\Client;
// Your Account SID and Auth Token from twilio.com/console
$account_sid = 'ACf9bd660b13b0******';
$auth_token = '7e5d0992f716420dad9******';
// In production, these should be environment variables. E.g.:
// $auth_token = $_ENV["TWILIO_ACCOUNT_SID"]
// A Twilio number you own with SMS capabilities
$twilio_number = "+12015****";
// Where to make a voice call (your cell phone?)
$to_number = "+1268784****";
$client = new Client($account_sid, $auth_token);
$call = $client->account->calls->create(
$to_number,
$twilio_number,
array(
"url" => "http://demo.twilio.com/docs/voice.xml"
)
);
echo $call->sid;
I believe, the Error speaks for itself :)
You need to enable International Permissions. This is made for you not to pay more, in case you don't need that.
This is what Twilio manual tells us:
Need to make outbound calls outside of the U.S, and want to select the regions and countries you’ll allow to protect your project from high-cost routes and potential fraud?
see more in Twilio docs
Twilio developer evangelist here.
Your Twilio account does not have permission to call the country you are trying to call. You can fix this though. The link in the error ends up taking you to international SMS permissions, so you need to allow the country you are trying to call by logging into your Twilio account and visiting this page: https://www.twilio.com/console/voice/calls/geo-permissions
So I've been testing out twilio on a free web hosting server and this is what I get:
Fatal error: Class 'Twilio\Rest\Api' not found in /home/u512189195/public_html/twilio-php-master/Twilio/Rest/Client.php on line 263
This is my code:
<?php
// Require the bundled autoload file - the path may need to change
// based on where you downloaded and unzipped the SDK
require __DIR__ . '/twilio-php-master/Twilio/autoload.php';
// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;
// Your Account SID and Auth Token from twilio.com/console
$sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$token = 'your_auth_token';
$client = new Client($sid, $token);
// Use the client to do fun stuff like send text messages!
$client->messages->create(
// the number you'd like to send the message to
'+15558675309',
array(
// A Twilio phone number you purchased at twilio.com/console
'from' => '+15017250604',
// the body of the text message you'd like to send
'body' => "Hey Jenny! Good luck on the bar exam!"
)
);
?>
I've also tried without the __DIR__ .
Obviously, I've replaced the variables with my own data.
I've uploaded the php master library which we get from twilio documentation website. - https://www.twilio.com/docs/libraries/php
You can install twilio package via composer
https://packagist.org/packages/twilio/sdk
then;
<?php
require __DIR__ . '/vendor/autoload.php';
...
use Twilio\Rest\Client;
...
You should check that the class exists in the library you downloaded.
Look for:
twilio-php-master/Twilio/Rest/Api.php
If it is missing (and that is what the error message you get implies) then you need to download a more current version of the helper library.
See the section on 'Using without Composer' here.
Is there anyway to send a text message from Twilio so that the from number contains an extension?
Something like this:
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once('twilio-php/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "...";
$token = "...";
$client = new Services_Twilio($sid, $token);
$client->account->messages->sendMessage(
"+441337944066+123#", // From
"+441234567891", // To
"Hello world" // Body
);
At the moment attempting this produces this error message:
Fatal error: Uncaught exception 'Services_Twilio_RestException' with message 'The 'From' number +441337944066+123# is not a valid phone number or shortcode.'
Twilio Support here.
Unfortunately we do not support extensions when sending messages.
However if you're looking to dial an extension on voice calls you can use the SendDigits parameter:
https://www.twilio.com/docs/api/rest/making-calls
Tom
It was my understanding that you needed to send messages with a FROM value being a valid Twillio number. Since the wrapper/helper is using the REST API it should still be bound by those restrictions. See documentation about that here: http://www.twilio.com/docs/api/rest/sending-messages
So, unless you purchased the number from Twillio and it has an extension, unfortunately, I don't believe that is possible.