Reputation: 21
I am trying to send a simple SMS via Twilio with Php, but i get this Fatal error,
Uncaught exception 'Twilio\Exceptions\TwilioException' with message 'Unknown context accounts' in C:\xampp\htdocs\Twilio\vendor\twilio\sdk\Twilio\Rest\Client.php:687
Stack trace:
0 C:\xampp\htdocs\Twilio\twilio.php(24): Twilio\Rest\Client->__call('accounts', Array)
1 C:\xampp\htdocs\Twilio\twilio.php(24): Twilio\Rest\Client->accounts('AC8687f4eaba8c6...')
2 {main} thrown in C:\xampp\htdocs\Twilio\vendor\twilio\sdk\Twilio\Rest\Client.php on line 687
This is my local server code:
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once 'vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;
$account_sid = 'AC8687f4eaba8c68XXXXXXXXXXXXX';
$auth_token = '6baf210351f27a38850XXXXXXXXXXXXXXXX';
$client = new Client($account_sid, $auth_token);
$messages = $client->accounts('AC8687f4eaXXXXXXXXXXX')
->messages->create('+52722XXXXXXX', array(
'From' => '+151240XXXXX',
));
?>
Upvotes: 2
Views: 1837
Reputation: 101
use Twilio\Rest\Client;
$twilio = new Client(config('services.twilio.sid'), config('services.twilio.token'));
$message = $twilio->messages->create(
'+1234567890', // The recipient's phone number
[
'from' => config('services.twilio.from'),
'body' => 'Hello from Twilio!', // The SMS message content
]
);
// Check the response or handle any errors
dd($message->sid);
Upvotes: 0
Reputation: 73055
Twilio developer evangelist here.
It looks as though you are trying to send a message from the account you authorised the PHP library with in the first place. In this case, you do not need to call to the accounts resource first. It may have been an intentional omission, but I also notice your message doesn't have a body.
The following code should work for you:
<?php
// Get the PHP helper library from twilio.com/docs/php/install
require_once 'vendor/autoload.php'; // Loads the library
use Twilio\Rest\Client;
$account_sid = 'AC8687f4eaba8c68XXXXXXXXXXXXX';
$auth_token = '6baf210351f27a38850XXXXXXXXXXXXXXXX';
$client = new Client($account_sid, $auth_token);
$messages = $client->messages->create('+52722XXXXXXX', array(
'From' => '+151240XXXXX',
'Body' => 'Hello from my PHP code!'
));
?>
Upvotes: 6