I am using twilio to send sms to mobile, when i run the script it sends an SMS like it should to my mobile but the problem is i don't know the status of the sent SMS, how do i get that?
What i tried so far is below
<?php
require __DIR__ . '/vendor/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 SMS capabilities
$twilio_number = "+0987654321";
$client = new Client($account_sid, $auth_token);
$client->messages->create(
// Where to send a text message (your cell phone?)
'+1234567890',
array(
'from' => $twilio_number,
'body' => 'I sent this message in under 10 minutes!'
)
);
after doing proper research, and having all kind of issues with the webhook, here is what i settled on
$phoneString = $phoneNumber->asNorthAmericanDialingString();
$mi = $client->messages->create(
// the number you'd like to send the message to
$phoneString ,
array (
// A Twilio phone number you purchased at twilio.com/console
'from' => Config::getCurrentConfig()->smsConfig->fromNumber ,
// the body of the text message you'd like to send
'body' => $message ,
'ProvideFeedback' => "true"
)
);
$msg = $mi->fetch();
$sid = $msg->sid;
$status = $msg->status;
if ($status == 'sent' || $status == 'delivered') { // f'ing twilio, random status
self::getClassLogger()->debug("Sent text message[$sid], status is [$status]");
return new TextMessageStatus($sid , $status);
} else {
// do whatever is appropriate for your case
}
Related
when I try to send a whatsapp message to a mobile number that isn't registered on whatsapp, how do I know that it failed? because I want to send the message using regular SMS instead. but my code below doesn't give any different result between success and failed process:
public function sendMessage($to, $msg, $params=[])
{
$client = new Client($this->sid, $this->token);
$from = $this->from_number; // my twilio number e.g. +1786xxxx
if ( ! empty($params['via']) && $params['via'] == 'whatsapp') {
$to = 'whatsapp:'.$to;
$from = 'whatsapp:'.$from;
}
$options = [
// A Twilio phone number you purchased at twilio.com/console
'from' => $from,
// the body of the text message you'd like to send
'body' => $msg,
];
// Use the client to do fun stuff like send text messages!
$response = $client->messages->create(
$to,
$options,
);
return $response;
}
// end public function sendMessage
public function do_send_msg()
{
$to = '+628123456789';
// this message already uses the same format as the approved message template
$msg = "Your otp code for Login Process is 123456";
$params = [
'via' => 'whatsapp',
];
$send = $this->twilio->sendMessage('+628123456789', $msg, $params);
var_dump($send->status);
}
I wanted to make the code like this instead but this code is unable to differentiate the value of $send->status whether it's successful or failed:
public function do_send_msg()
{
$to = '+628123456789';
// this message already uses the same format as the approved message template
$msg = "Your otp code for Login Process is 123456";
$params = [
'via' => 'whatsapp',
];
$send = $this->sendMessage($to, $msg, $params);
// if sending via whatsapp failed, try sending via regular SMS instead
if ( ! $send->status ) {
$params['via'] = 'SMS';
$send = $this->sendMessage($to, $msg, $params);
}
}
I'm afraid Meta/WhatsApp doesn't expose this information at this point in time. Therefore, I'd recommend that you let the users choose whether they want to receive a WhatsApp message or a regular SMS.
I an setting up a website to send SMS automatically by API key Nexmo. But whene I add my variables into the Nexmo code, this one not work. How can I add my variables please?
I added my php variables to the Nexmo SMS default code but no result, but whene I trayed there code the stuff work fine
my file phone.php , with $row["phone_number"]=212981416807 at this exemple
$text1 = "Hello";
$text2 = " this is my company";
$MyNexmoID_Account = "3896321";
$MyNexmoAPI_Key = "yhg784frds78jkim";
$to = $row["phone_number"];
$from = "my company";
$text = "$text1 $text2";
// Code to Send SMS with Code Recharge ------
require_once "vendor/autoload.php";
//composer require nexmo/client;
$basic = new \Nexmo\Client\Credentials\Basic('$MyNexmoID_Account', '$MyNexmoAPI_Key');
$client = new \Nexmo\Client($basic);
$message = $client->message()->send([
'to' => '$to',
'from' => '$from',
'text' => '$text'
]);
if ($message && $client && $basic){echo " Recharge Code Sent Correctlly.";}else{echo "Failed! Recharge Code Not Sent.";}
// End Code to Send SMS with Code Recharge -----
This is default Nexmo code:
require_once "vendor/autoload.php";
//composer require nexmo/client;
$basic = new \Nexmo\Client\Credentials\Basic('3896321', 'yhg784frds78jkim');
$client = new \Nexmo\Client($basic);
$message = $client->message()->send([
'to' => '212981416807',
'from' => 'Nexmo',
'text' => 'Hello Nexmo'
]);
You don't need to quote variables. Try this:
$message = $client->message()->send([
'to' => $to,
'from' => $from,
'text' => $text
]);
Not sure if you have corrected this but do check your country code, I had a similar error with variable (also do listen to Michael Heap), where the hard code for the US phone number was 19091234567 and my database number was 9091234567. Unlike Twilio, Nexmo needs the country code. Hope it helps.
I'm trying to send bulk sms through twilio Api. Is there any method to pass the array of all phone numbers in a single API request.
Twilio developer evangelist here.
Yes there is now! It's known as the passthrough API (as it allows you to pass through many different messaging systems and send bulk messages. It's part of the Notify API and you can use it to send bulk SMS messages. You need to set up a messaging service and a notify service in your console, then you can use the following code:
<?php
// NOTE: This example uses the next generation Twilio helper library - for more
// information on how to download and install this version, visit
// https://www.twilio.com/docs/libraries/php
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;
// Your Account SID and Auth Token from https://www.twilio.com/console
$accountSid = "your_account_sid";
$authToken = "your_auth_token";
// your notify service sid
$serviceSid = "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
// Initialize the client
$client = new Client($accountSid, $authToken);
// Create a notification
$notification = $client
->notify->services($serviceSid)
->notifications->create([
"toBinding" => [
'{"binding_type":"sms", "address":"+15555555555"}',
'{"binding_type":"sms", "address":"+12345678912"}'
],
"body" => "Hello Bob"
]);
Checkout the documentation on sending multiple messages with the Notify passthrough API for all the details.
In case someone else had troubles with preparing the toBinding parameter from a PHP array() as I had, here is an example for that:
<?php
require_once '/path/to/vendor/autoload.php';
use Twilio\Rest\Client;
$accountSid = "your_account_sid";
$authToken = "your_auth_token";
$serviceSid = "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$client = new Client($accountSid, $authToken);
$recipients = array($num1, $num2, ...); // Your array of phone numbers
$binding = array();
foreach ($recipients as $recipient) {
$binding[] = '{"binding_type":"sms", "address":"+1'.$recipient.'"}'; // +1 is used for US country code. You should use your own country code.
}
$notification = $client
->notify->services($service_sid)
->notifications->create([
"toBinding" => $binding,
"body" => $text
]);
?>
First of all, you need to configure your twilio number properly for notification. Then your to use below code to send bulk SMS.
$message = 'Any text message';
$to = array();
foreach ($users as $user) {
$to[] = '{"binding_type":"sms", "address":"'.$user->phone_number.'"}';
}
$sid = 'TWILIO_ACCOUNT_SID';
$token = 'TWILIO_AUTH_TOKEN';
$services_id = 'TWILIO_SERVICE_ID';
$twilio = new Client($sid, $token);
$notification = $twilio
->notify->services($services_id)
->notifications->create([
"toBinding" => $to,
"body" => $message
]);
I want to send SMS using the smsgateway.me API, but I'm getting this response:
{"response":false,"status":0}
Here is my code:
include "smsGateway.php";
$smsGateway = new SmsGateway('xxxxxx#xxxxxxx.com', 'xxxxxx');
$deviceID = xxxxx;
$number = '+92xxxxxxxxxx';
$message = 'Hello World!';
$options = [
'send_at' => strtotime('+1 minutes'), // Send the message in 10 minutes
'expires_at' => strtotime('+1 hour') // Cancel the message in 1 hour if the message is not yet sent
];
$result = $smsGateway->sendMessageToNumber($number, $message, $deviceID, $options);
echo json_encode($result);
please verify that cURL is enabled in your host
I am sending an SMS message to an Android phone using Twilio.
The domain has a hypen in it. e.g. http://my-domain.com
When the SMS message arrives on Android, only the initial portion of the text is included in the hyperlink.
So in the above example the hyperlink is "http://my"
How is it possible to escape a hyperlink being send to android? I am using the PHP Twilio client.
It might be you done any small mistake. And, it not depend on android or iOS.
I also tried and it worked as its not specific to Android or iOS.
<?php
require '../Services/Twilio.php'; // Include the Twilio PHP library
$version = "2010-04-01"; // Twilio REST API version
// Set our Account SID and AuthToken
$sid = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy';
$token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$client = new Services_Twilio($sid, $token, $version); //initialise the Twilio client
$message = 'http://my-domain.com';
try {
// Initiate a new outbound call
$call = $client->account->messages->create(array(
'To' => "+YYYYYYYYYY",
'From' => "+1XXXXXXXXXX",
'Body' => $message,
));
echo 'TWILIO SMS';
echo 'Sending.... ';
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
?>
Here, my code. From this am getting this url as same in SMS from Twilio.
You please check your code once. And, I hope my code will help you.