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',
));
?>
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!'
));
?>
Related
I'm configuring Twilio sendnotifications.php file as described on this website
When I launch "php sendnotifications.php" in the terminal with this code, everything works perfectly and I receive my SMS
<?php
// Required if your environment does not handle autoloading
require __DIR__ . '/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 = 'My id';
$token = 'My 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
'MyPhoneNumber',
array(
// A Twilio phone number you purchased at twilio.com/console
'from' => 'MyTwilioSendingNumber',
// the body of the text message you'd like to send
'body' => "My message"
)
);
But when i run it like this I have an error:
<?php
// Required if your environment does not handle autoloading
require __DIR__ . '/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 = 'My id';
$token = 'My token';
$client = new Client($sid, $token);
$phone='MyPhoneNumber';
// Use the client to do fun stuff like send text messages!
$client->messages->create(
// the number you'd like to send the message to
$phone,
array(
// A Twilio phone number you purchased at twilio.com/console
'from' => 'MyTwilioSendingNumber',
// the body of the text message you'd like to send
'body' => "My message"
)
);
The error is:
MacBook-Pro:Test envoi sms accueil Joris$ php sendnotifications.php
Fatal error: Uncaught Twilio\Exceptions\RestException: [HTTP 400] Unable to create record: The 'To' number 74663 is not a valid phone number. in /Users/Joris/Desktop/Test envoi sms accueil/Twilio/Version.php:85
Stack trace:
#0 /Users/Joris/Desktop/Test envoi sms accueil/Twilio/Version.php(219): Twilio\Version->exception(Object(Twilio\Http\Response), 'Unable to creat...')
#1 /Users/Joris/Desktop/Test envoi sms accueil/Twilio/Rest/Api/V2010/Account/MessageList.php(69): Twilio\Version->create('POST', '/Accounts/AC8ec...', Array, Array)
#2 /Users/Joris/Desktop/Test envoi sms accueil/sendnotifications.php(20): Twilio\Rest\Api\V2010\Account\MessageList->create('$phone', Object(Twilio\Values))
#3 {main}
thrown in /Users/Joris/Desktop/Test envoi sms accueil/Twilio/Version.php on line 85
MacBook-Pro:Test envoi sms accueil Joris$
I just re-tried with the second code and it's working, I don't know why :D
<?php
// Required if your environment does not handle autoloading
require __DIR__ . '/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 = 'My id';
$token = 'My token';
$client = new Client($sid, $token);
$phone='MyPhoneNumber';
// Use the client to do fun stuff like send text messages!
$client->messages->create(
// the number you'd like to send the message to
$phone,
array(
// A Twilio phone number you purchased at twilio.com/console
'from' => 'MyTwilioSendingNumber',
// the body of the text message you'd like to send
'body' => "My message"
)
);
I'm trying to integrate Twilio into my application. When I run their sample code, it runs perfectly fine:
require_once "twilio-php-master/Twilio/autoload.php";
use Twilio\Rest\Client;
$AccountSid = "XXXXXX";
$AuthToken = "XXXXXX";
$client = new Client($AccountSid, $AuthToken);
$sms = $client->account->messages->create(
"+15555555555",
array(
'from' => "+15556665566",
'body' => "Hey, Monkey Party at 6PM. Bring Bananas!"
)
);
However, when I try to integrate it into a class, I keep getting a PHP Fatal Error Class 'Twilio\Rest\Client' not found every time it executes. This is the code in my class:
require_once "twilio-php-master/Twilio/autoload.php";
use Twilio\Rest\Client;
class Customer{
//constructors etc
public function sendText(){
$AccountSid = "XXXXXX";
$AuthToken = "XXXXXX";
$client = new Client($AccountSid, $AuthToken);
$sms = $client->account->messages->create(
'+15555555555',
array(
'from' => "+15556665566",
'body' => "Hey, Monkey Party at 6PM. Bring Bananas!"
)
);
}
}
I've checked the opening tags in the Twilio Library, I've ensured that the file location is correct (it throws the error when the
$client = new Client($AccountSid, $AuthToken);
line is executed, not at the require_once) but I can't seem to figure out what's going wrong.
I am creating my own message logging when sending and receiving messages from/to twilio via the PHP REST api sdk 5. I want to get the message sid when sending a new message and digging around in the code I found I can get it from
Twilio\Http\CurlClient.php
line 36 starts:
36 list($head, $body) = ($parts[0] == 'HTTP/1.1 100 Continue')
37 ? array($parts[1], $parts[2])
38 : array($parts[0], $parts[1]);
I added line 39
39 $GLOBALS["curlResponseBody"] = $body;
so I can retrieve the json response which has an entry for 'sid' of the message just created.
There has go to be a method for getting that information but I just haven't seen it mentioned anywhere in respect to SDK5.
Here is the code I am using to create the message:
require_once 'Twilio/autoload.php'; // Loads the library
use Twilio\Rest\Client;
$client = new Client($account_sid, $auth_token);
$client->messages->create(
$toPhone,
array(
'from' => $fromTwilioPhone,
'body' => $responseMessage,
)
);
//This edit to the Twilio PHP Library is found in Twilio\Http\CurlClient.php
$curlResponseBody = json_decode($GLOBALS["curlResponseBody"]);
$newMessageSid = $curlResponseBody->sid;
Is there some way to use $client to get to the 'sid' of the message just created?
Twilio evangelist here.
The create() function returns an object that should let you get the message sid:
$client = new Client($account_sid, $auth_token);
$msg = $client->messages->create(
$toPhone,
array(
'from' => $fromTwilioPhone,
'body' => $responseMessage,
)
);
echo $msg.Sid
Hope that helps.
I'm trying to send mail using the following code and I'm using guzzlehttp, but getting Fatal error: Uncaught exception 'RuntimeException' with the message 'Puli Factory is not available'. Please help me find a solution, thanks!
Here is my code:
require 'vendor/autoload.php';
use Mailgun\Mailgun;
# Instantiate the client.
$mgClient = new Mailgun('key-');
$domain = "domain";
# Make the call to the client.
$result = $mgClient->sendMessage("$domain",
array('from' => 'Mailgun Sandbox <xxxxxx#sandbox.mailgun.org>',
'to' => 'John Doe<xxxxx#abc.com>',
'subject' => 'Hello John Doe',
'text' => 'Email Text'));
and i've replaced key and domain with my original.
I had the same problem.
Try:
$client = new \Http\Adapter\Guzzle6\Client();
$mailgun = new \Mailgun\Mailgun('api_key', $client);
Then:
$mailgun->sendMessage(.....)
Hope it help you.
Fatal error: Uncaught SoapFault exception: [Client] Function ("AuthenticationHeader") is not a valid method for this service in D:\xampp\htdocs\soap\index_2.php:132 Stack trace: #0 D:\xampp\htdocs\soap\index_2.php(132): SoapClient->__call('AuthenticationH...', Array) #1 D:\xampp\htdocs\soap\index_2.php(132): SoapClient->AuthenticationHeader(Array) #2 {main} thrown in D:\xampp\htdocs\soap\index_2.php on line 132
There are must be only one request, you send three. You must create something like this:
1) Set header for authentication:
$auth = $auth = new SOAPAuth('USERNAME', 'PASSWORD');
$header = new SOAPHeader('urn:example.org/auth', 'AuthenticationHeader', $auth);
$client->__setSoapHeaders($header);
2) Create create request with message text and contacts.
$result = $client->SendSMSToContacts(array("MessageText" => "some text", "contactIDs" => array(123456789, 123456789));
P.S. For the debugging create client with:
$client = new SoapClient($url, array('trace' => 1,
'exceptions' => 1,));
and after sending request, look on request that was sended and resopnse on it:
var_dump("REQUEST=", $client->__getLastRequest());
var_dump("RESPONSE=", $client->__getLastResponse());
if you look at the error code you will see AuthenticationHeader is not a method! the error is very clear!
use this
or download soap ui. (google it!)
and you will see what method are available!