-4

I'm using Nexmo for OTP verification in my ionic project. I have made below php file for sending sms.

Please see this link. I'm using this tutorial. https://ampersandacademy.com/tutorials/ionic-framework-3/automate-sms-otp-verification-using-ionic3-with-nexmo-part1

<?php
require_once 'vendor/autoload.php';

header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Credentials: true');
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type");


$NEXMO_TO = $_GET['mobile'] ?? '';
$NEXMO_FROM='Ampersand OTP';
$MESSAGE='1551 Ampersand OTP';

$client = $client = new Nexmo\Client(new Nexmo\Client\Credentials\Basic('XXXX', 
'XXXXXXXX'));


$message = $client->message()->send([
'to' => $NEXMO_TO,
'from' => $NEXMO_FROM,  
'text' => $MESSAGE
]);

echo "Sent message to " . $message['to'] . ". Balance is now " . 
$message['remaining-balance'] . PHP_EOL;

?>

But when i hit this php file it gives me below error.

Fatal error: Uncaught Nexmo\Client\Exception\Request: Missing to param in C:\xampp\htdocs\nexmosms\vendor\nexmo\client\src\Message\Client.php:75 Stack trace: #0 C:\xampp\htdocs\nexmosms\send-sms.php(20): Nexmo\Message\Client->send(Object(Nexmo\Message\Message)) #1 {main} thrown in C:\xampp\htdocs\nexmosms\vendor\nexmo\client\src\Message\Client.php on line 75
Sagar Kodte
  • 3,723
  • 4
  • 23
  • 52
  • 1
    Seems `$_GET['mobile']` is not set. Are you sure that you are getting them? – Thamaraiselvam Feb 02 '18 at 11:08
  • Can you attach a link of NexmoSMS docs? – GiamPy Feb 02 '18 at 11:08
  • So what is the exact request you are making to get to your script? – jeroen Feb 02 '18 at 11:10
  • You posted your nexmo account credentials in the code, I've edited them out for now but you REALLY should go into the nexmo dashboard NOW and change your API Secret otherwise people will be able to use your account to send messages and burn through your credit – Sam Machin Feb 02 '18 at 11:13
  • I'm not PHP Developer. – Sagar Kodte Feb 02 '18 at 11:17
  • @SamMachin This credentials are use by who done this tutorial. – Sagar Kodte Feb 02 '18 at 11:18
  • @Smartpal I'm doing on localhost. So i used http://localhost:8080/script.php?mobile=919876543210. Got error Object not found – Sagar Kodte Feb 02 '18 at 12:14
  • @Smartpal Got error Fatal error: Uncaught Nexmo\Client\Exception\Request: Non White-listed Destination - rejected in C:\xampp\htdocs\nexmosms\vendor\nexmo\client\src\Message\Client.php:75 Stack trace: #0 C:\xampp\htdocs\nexmosms\send-sms.php(20): Nexmo\Message\Client->send(Object(Nexmo\Message\Message)) #1 {main} thrown in C:\xampp\htdocs\nexmosms\vendor\nexmo\client\src\Message\Client.php on line 75 – Sagar Kodte Feb 02 '18 at 12:27
  • Read more here at https://help.nexmo.com/hc/en-us/articles/204014853-Nexmo-trial-period-How-to-add-numbers-to-list-of-permitted-destinations-?mobile_site=true – Shahnawaz Kadari Feb 02 '18 at 12:33
  • @Smartpal. Actaully i directly used all values in 'to', 'from', 'text'. without declaring variables. My api works. I mean I'm getting the otp message. But in my ionic project the page is not redirecting to the OTP verify page. – Sagar Kodte Feb 02 '18 at 12:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164407/discussion-between-smartpal-and-sagar-kodte). – Shahnawaz Kadari Feb 02 '18 at 12:48

1 Answers1

2

The variable $_GET['mobile'] isn't set in the script.

Use this to check if it exists:

$NEXMO_TO = (!empty($_GET['mobile'])) ? $_GET['mobile'] : '';

From PHP 7 you must use the short form like this:

$NEXMO_TO = $_GET['mobile'] ?? '';
user2342558
  • 5,567
  • 5
  • 33
  • 54