How can I set my php variables in Nexmo code - php

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.

Related

how to handle twilio error status when sending whatsapp message

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.

Twilio Sms not sending Php

I am trying to send sms using twilio but not getting any response or unable to send sms
I am using following code,where i am wrong ?
require __DIR__ . 'Twilio/autoload.php';
use Twilio\Rest\Client;
$sid = "xxxxxxxxxxxxxxxxxx";
$token = "xxxxxxxxxxxxxxxxxxx";
$twilio = new Client($sid, $token);
$message = $twilio->messages
->create("+91xxxxxxx", // to verified twilio number
array(
"body" => "This is the ship that made the Kessel Run in fourteen parsecs?",
"from" => "+xxxxxxx"
)
);
print($message->sid);
Following reasons may possible in this case.
Make sure that you have CURL module enable
var_dump($message);
Try to print Twilio object $twilio to make sure that library is correctly loaded .
Turn on php error by adding top of the script
error_reporting(E_ALL);
ini_set('display_errors','1');

Get a response from a twilio request

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
}

How to send bulk sms in twilio api without for loop

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

Twilio `$client->account->sms_messages->create()` not working, just loading page

$to=+11111111;
$body="hello Its not working";
$AccountSid = "*********************************";
$AuthToken = "*********************************";
$client = new Services_Twilio($AccountSid, $AuthToken);
$from = '+11111111';
$message = $client->account->sms_messages->create($from, $to, $body);
return true;
It is working on normal server but not on amazon server.
when i comment $message the code loads the page properly and when uncomment the $message, it just keeps on loading (i.e. the loading circle keeps on rolling not opens any page).
I am receiving both the numbers and the text message too thats not an issue but the $client->account->sms_messages->create() not working.
Someone help!
If you give "+11111111111" as from number. Twilio will show the error
Fatal error: Uncaught exception 'Services_Twilio_RestException' with message 'The 'From' number 11111111111 is not a valid phone number or shortcode.
So you should add valid twilio number that exist in your twilio account as from number. Then try this.
$sms = $client->account->messages->create(array(
'To' => "$toNumber",
'From' => "$myNumber",
'Body' => "$msg"));
}

Categories