Send and Confirm Verification OTP with PHP

A quick walk-through on how to seamlessly implement Sendchamp Verification API with PHP

Aliyu Abubakar

Aug 19, 2021

Learn how to quickly send and confirm verification OTP with PHP using sendchamp API.

In this guide we will walk you through:

  1. Signing up free sendchamp account
  2. Create a Sender ID
  3. Send verification OTP
  4. Confirm verification OTP

Prerequisite

Before you can get started, you need the following already set up: You have PHP installed. If you don't, read about how to install it on your system here.

  • A sendchamp account
  • Signing up free sendchamp account

If you already have an account? skip to the next section You can sign up for a free sendchamp account here.

image

When you sign up, you'll have to verify your email address. This allows sendchamp to verify your identity.

After you've verified your email address, you'll be asked a question in order to get you started in a way that's relevant to you.

After you finish setting up your account, you'll have access to your dashboard where you can access your API Key and perform dashboard communication functions.

Create a Sender ID

Once your account is created, you’ll need to add a new sender ID in order to send SMS Messages. Click channels on your dashboard under settings, click sms channel and add a sender ID. Take note of your ID, because you'll use it later.

image

Send a Verification OTP

First, you’ll need to create a file send_otp.php to save your code.


<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.sendchamp.com/api/v1/verification/create',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
    "channel":"sms",
    "sender":"Sendchamp",    
    "token_type":"numeric",
    "token_length":"5",
    "expiration_time":6,
    "customer_mobile_number":"2348119974190",
    "customer_email_address":"[email protected]",
    "meta_data":{"first_name":"segun"}
}',
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'Authorization: Bearer SECRET_KEY',
    'Content-Type: application/json'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

Replace CURLOTP_URL with either a test or live BASE URL depending on what you intend to do.

Replace Authorization under CURLOTP_HTTPHEADER with your test or live Access key depending on what you intend.

Parameters

  • Set channel to one of the following VOICE, SMS, WHATSAPP OR EMAIL.
  • Set sender to your unique sender ID.
  • Set token type as NUMERIC OR ALPHANUMERIC
  • Set token length to the length of the token you want to send to your customer. Minimum is 4
  • Set expiration time to how long you want to the to be active for in minutes. (E.g 10 means 10 minutes )

Run the following command:

php send_otp.php 

The code you used in the send_sms.php file sends a POST request to the sendchamp API endpoint to send your verification OTP.

Confirm a Verification OTP

Create a file confirm_otp.php to save your code.


<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.sendchamp.com/api/v1/verification/confirm',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
    "verification_code":"36284",
    "verification_reference": "VER-Jkrz2O4Hxet0F1muMqxw25jOv"
}',
}',
  CURLOPT_HTTPHEADER => array(
    'Accept: application/json',
    'Authorization: Bearer SECRET_KEY',
    'Content-Type: application/json'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

Parameters

  • Set verification code to the OTP that was sent to the customer..
  • Set verification reference to the unique reference that was returned as response when the OTP was created

Run the following command:

php confirm_otp.php 

The code you used in the confirm_otp.php file sends a POST request to the sendchamp API endpoint to confirm the OTP verification.

© 2022 Sendchamp. All rights reserved.