Search code examples
javascriptphpionic-frameworkionic3vonage

Missing to param in php


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

Solution

  • 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'] ?? '';