Twilio php Library SDK 5 curl response body to get sid - php

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.

Related

How to get content of message from users in my site using twilio

How are you everyone?
My site is based on PHP.
I am new with twilio and building simple project with this.
So what I should implement is to send messages to users using twilio and then receive message from users again in my site.
<?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 = 'Azzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz';
$token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$twilio = new Client($sid, $token);
$message = $twilio->messages
->create("+12099216581", // to
[
"body" => "This is test",
"from" => "+15324394442",
"statusCallback" => ""
]
);
print($message->sid);
This is for sending message to one users that I am using now..
Then...
<?php
require_once './vendor/autoload.php';
use Twilio\TwiML\MessagingResponse;
$response = new MessagingResponse();
$response->message('This is message 1 of 2.');
$response->message('This is message 2 of 2.');
echo $response;
I think this code will return text message to users with text...
If I am wrong, Please teach me...
So I am trying to do it now, but I can't know how to receive the content of message in my site.
If you are experience in this fields, Please teach me.
Thanks in advance.
You need to set the webhook for that messaging response
smsUrl = 'url';
$response = $client->request("POST", "IncomingPhoneNumbers/$phone_id.json", ['auth' => [$sid, $token], 'form_params' => array("SmsUrl" => $webhook_url, "BundleSid" => $bundle_id)]);
this URL will post the incoming message and reply message

Catching Twilio API exceptions- PHP

I am using Twilio to send SMS to users. On an invalid number, I am getting 400. How can I catch that exception or HTTP Status code so that I can handle it accordingly? It does return these codes and I want to know the HTTP Code received in the API response.
$client = new Client($account_sid, $auth_token);
$client->messages->create(
$to, array(
'body' => $message,
'from' =>$SMS_FROM
)
);

twilio 'Unknown context accounts

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!'
));
?>

twilio SMS not received in mobile trial account

I have created trial account on twilio and used test API credentials for sending the SMS, after sending the SMS it shows me SID in success but no message received by the phone number which i have added in 'TO' field.
You must use live credentials to send messages:
<?php
// this line loads the library
require('/path/to/twilio-php/Services/Twilio.php');
// you must use your live credentials!!!!
$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = '[AuthToken]';
$client = new Services_Twilio($account_sid, $auth_token);
$client->account->messages->create(array(
'To' => "+15558675309",
'From' => "+15017250604",
'Body' => "Hey, hope this works!",
'MediaUrl' => "http://farm2.static.flickr.com/1075/1404618563_3ed9a44a3a.jpg",
));
It is explained here - https://www.twilio.com/docs/api/rest/test-credentials - test credentials do not connect to real phone numbers.

Twilio PHP connector / Cant echo twillio answer

I'm trying to connect to the twilio for sending SMS. It was working but it stopped. After i returned from holidays.
Well SMS sending is working, but i cant echo the answer from Twilio.
I found out that my php file is working sweet on PHP 5.3 but on 5.6 it is throwing an error. So it has something to do with echoing $client but i dont know whats wrong.
Here is my code:
<?php
// this line loads the library
require dirname(__FILE__) . "../../../includes/Services/Twilio.php";
$account_sid = 'XXX';
$auth_token = 'XXX';
$client = new Services_Twilio($account_sid, $auth_token);
//Get the submitted data
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$mamiMobile = $request -> mamiPhone;
$text = $request -> smsoffer;
$client->account->messages->create(array(
'To' => $mamiMobile,
'From' => "+41798071977", //From Number from Twilio
'Body' => $text
));
//This works on php 5.3 but on 5.6 it is not working!
echo ($client);
;?>
The Error i get in the PHP.log:
[03-Mar-2016 18:18:16 Europe/Zurich] PHP Catchable fatal error: Method Services_Twilio::__toString() must return a string value in /Applications/MAMP/htdocs/angular-bootstrap-admin-web-app-with-angularjs/angular/includes/php/sms_connector_twillio_offer.php on line 34
Twilio developer evangelist here.
In order to echo the response from Twilio you shouldn't be echoing the $client itself, but the response to $client->account->messages->create. Why not try something like:
$response = $client->account->messages->create(array(
'To' => $mamiMobile,
'From' => "+41798071977", //From Number from Twilio
'Body' => $text
));
echo ($response);
In answer to what changed between PHP 5.3 and 5.6, my guess is that something happened between those versions to how $this responds to foreach such that this code no longer works as expected.
Could you raise a bug in the GitHub issues for the twilio-php project so that someone can take a look at it? Thanks!

Categories