Twilio PHP fatal error - php

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.

Related

Google speech api Credentials Not Working

I've enabled the Google text-to-speech api, generated an oAuth2 file and an api key, and when I run the code below from localhost and execution hits the "$textToSpeechClient =" line, both return the same error ("Could not load the default credentials."). I would prefer using the api key (in the commented out section), but I'll use either one. I installed the PHP client using Composer.
I tried getting paid support from Google, but I'm not able to sign up because "You must be assigned Support > Support Account Administrator for the organization in IAM to continue." When I go there, there is no apparent way to do that.
Help on any of these fronts would be appreciated.
require __DIR__ . '/vendor/autoload.php';
$client = new Google\Client();
$client->setAuthConfig(__DIR__ . '\googleAuthorization.json');
use Google\Cloud\TextToSpeech\V1\AudioConfig;
use Google\Cloud\TextToSpeech\V1\AudioEncoding;
use Google\Cloud\TextToSpeech\V1\SynthesisInput;
use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;
// $client->setDeveloperKey("AIzaSyAzSZjgfLS76q1234abcdefgtoGuKAqz5xE");
// $client->addScope(Google\Service\Drive::DRIVE);
// $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
// $client->setRedirectUri($redirect_uri);
// if (isset($_GET['code'])) {
// $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
// }
$textToSpeechClient = new TextToSpeechClient();
Problem solved (with the help of 3 google support staff in a video chat in at least 3 different time zones).
The Google text-to-speech api requires that the PHP command:
putdev()
be available so you can run:
putenv(
"GOOGLE_APPLICATION_CREDENTIALS=absolute-path-to-your-json-authentication-file.json"
);
In my default php.ini server configuration, putdev was included in the rather long disable_functions= list. Removing putdev from that list allowed authentication to proceed as expected.
Why this is needed for the text-to-speech api and not the cloud storage api is still a mystery.

make and receive call betwen browser and phone to discuss like a simple call

<?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.

voice call via twilio and php: parse error in autoload.php

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?

How to send SMS to a PHP array with Twilio API

Using the Twilio API, I've got my PHP functioning to send to one phone number, and can successfully send. We're looking to send to multiple numbers from one request and to do so, I've set up an array of numbers to iterate through, however, I keep getting a 500 error when I attempt to send the message by hitting the URL. Below is the file I'm working with.
Running PHP 7.2 on a Linux server. I'm running CentOS 7.7 and Apache 2.4.43 if that matters at all.
// 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/src/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 = 'XXXXXXXX';
$token = 'XXXXXXXX';
$client = new Client($sid, $token);
$a = array('+15555555555', '+15555555556');
$bodyTxt = “This is a test of sending the text message to multiple phone numbers.”
// Use the client to do fun stuff like send text messages!
foreach ($a as $v) {
$message = $twilio->messages
$client->messages->create($v, // to
[
"body" => $bodyTxt,
"from" => "+15555555557",
]
);
print($message->sid);
}
);
I'm not super familiar with PHP as I'm mostly in marketing, but I'm deputizing as developer in these crazy times because I know just enough to be dangerous. I'm thinking it is something with the foreach section, as that's the only piece that has changed from the single send.
Any help is appreciated!
Figured it out thanks to the help from #LuisE! I went through and figured out where I was missing the semicolons after the array, the $bodyTxt, and the $message = $twilio->messages.
// 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/src/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 = 'XXXXXXXXXX';
$token = 'XXXXXXXXX';
$client = new Client($sid, $token);
$a = array('+15555555555', '+15555555556');
$bodyTxt = 'This is a test of sending the text message to multiple phone numbers.';
// Use the client to do fun stuff like send text messages!
foreach ($a as $v) {
$message = $twilio->messages;
$client->messages->create($v, // to
[
"body" => $bodyTxt,
"from" => "+15555555557",
]
);
print($message->sid);
}

I got this fatal error while using twilio voice

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

Categories