twilio SMS not received in mobile trial account - php

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.

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
)
);

Error when I call a variable in my PHP file Twilio send messages

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"
)
);

Twilio php Library SDK 5 curl response body to get sid

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.

Amazon SES PHPMail or SDK

I've registered with Amazon SES service with email limit setup and out of the sandbox. I've tried many PHPMailer function and all return me as error : Connexion time out (110). Is it possible the send mail from PHPMailer?
I have seen on Amazon SES site this link.
<?php
// Replace path_to_sdk_inclusion with the path to the SDK as described in
// http://docs.aws.amazon.com/aws-sdk-php/v2/guide/quick-start.html
define('REQUIRED_FILE','path_to_sdk_inclusion');
// Replace sender#example.com with your "From" address.
// This address must be verified with Amazon SES.
define('SENDER', 'sender#example.com');
// Replace recipient#example.com with a "To" address. If your account
// is still in the sandbox, this address must be verified.
define('RECIPIENT', 'recipient#example.com');
// Replace us-west-2 with the AWS region you're using for Amazon SES.
define('REGION','us-west-2');
define('SUBJECT','Amazon SES test (AWS SDK for PHP)');
define('BODY','This email was sent with Amazon SES using the AWS SDK for PHP.');
require REQUIRED_FILE;
use Aws\Ses\SesClient;
$client = SesClient::factory(array(
'version'=> 'latest',
'region' => REGION
));
$request = array();
$request['Source'] = SENDER;
$request['Destination']['ToAddresses'] = array(RECIPIENT);
$request['Message']['Subject']['Data'] = SUBJECT;
$request['Message']['Body']['Text']['Data'] = BODY;
try {
$result = $client->sendEmail($request);
$messageId = $result->get('MessageId');
echo("Email sent! Message ID: $messageId"."\n");
} catch (Exception $e) {
echo("The email was not sent. Error message: ");
echo($e->getMessage()."\n");
}
?>
I've copy all the codes, put my variable instead of showned in the demo script. Now I'm getting the error : You must use KEY ans SECRET_KEY to use this script... Where I cant put my KEY and SECRETKEY in the script? There is no explanation on how to do this.
Is there another way send email throught Amazon SES service?
Thanks!
So simple. I have to add key and secret in :
$client = SesClient::factory(array(
'version'=> 'latest',
'region' => REGION,
'credentials' => array(
'key' => 'XXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXX',
)
));
and set XXXXXXXXXXXXXXXX full access to api in amazon security credentials
As far as I know PHP Mailer was not working with AWS SES by API, you should use SES SMTP with PHP Mailer.
The correct ports are 25, 465 or 587.

Categories