Skip to content

Instantly share code, notes, and snippets.

@BlackMac
Last active April 26, 2017 10:57
Show Gist options
  • Save BlackMac/0d16c4564e785cdad9b9624e6dac4f2c to your computer and use it in GitHub Desktop.
Save BlackMac/0d16c4564e785cdad9b9624e6dac4f2c to your computer and use it in GitHub Desktop.
Verify a phone number using the SMS sending capabilities of the sipgate REST API.

SMS phone number verification

Use the sipgate REST-API to verify if a mobile phone number is valid.

To try this just type

mkdir smsauth
cd smsauth
curl -O "https://gist.githubusercontent.com/BlackMac/0d16c4564e785cdad9b9624e6dac4f2c/raw/verify_number.php"
TOKEN=[[YOUR BAERER TOKEN]] php -S localhost:3333

and point your browser to to http://localhost:3333/verify_number.php

This code is not meant to be deployed to production without changes

<?php
define("SESSION_STAGE_START",0);
define("SESSION_STAGE_VERIFY",1);
define("SESSION_STAGE_FINISHED",2);
define("SESSION_STAGE_ERROR",-1);
function cleanNumber($number) {
$cleanNumber = preg_replace("/\(0\)/", "", $number);
$cleanNumber = preg_replace("/[^0-9\+]/", "", $cleanNumber);
$cleanNumber = preg_replace("/^00/", "+", $cleanNumber);
$cleanNumber = preg_replace("/^0/", "+49", $cleanNumber);
$cleanNumber = preg_replace("/^([0-9])/", "+$1", $cleanNumber);
if (strlen($cleanNumber) !== 14) return -1;
return $cleanNumber;
}
function sendSms($recipient, $message) {
$c = curl_init();
curl_setopt($c,CURLOPT_URL, 'https://api.sipgate.com/v1/sessions/sms');
curl_setopt($c,CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$_ENV['TOKEN'],
'Content-Type: application/json',
'Accept: application/json'));
curl_setopt($c,CURLOPT_POST,true);
curl_setopt($c,CURLOPT_POSTFIELDS, json_encode(array(
"smsId"=>"s0",
"recipient"=> $recipient,
"message"=> $message
)));
$result = curl_exec($c);
$resultcode = curl_getinfo($c,CURLINFO_HTTP_CODE);
curl_close($c);
}
$stage = !!$_POST['tel'] + !!$_POST['verify'];
session_start();
$verified = false;
$error = "Es ist ein Fehler aufgetreten";
switch ($stage) {
case SESSION_STAGE_START:
$_SESSION["number"] = null;
$_SESSION["verification"] = rand(10000, 99999);
break;
case SESSION_STAGE_VERIFY:
$tel = cleanNumber($_POST['tel']);
if ($tel === -1) {
$error = "Die von Ihnen angegebene Rufnummer ist ungültig.";
$stage = SESSION_STAGE_ERROR;
break;
}
$_SESSION["number"] = $tel;
sendSms($tel, "Verifikationscode: ".$_SESSION["verification"]);
break;
case SESSION_STAGE_FINISHED:
if ($_POST['verify'] == $_SESSION["verification"] && $_POST["tel"] == $_SESSION["number"]) {
$verified = true;
} else {
$stage = SESSION_STAGE_ERROR;
$error = "Die Nummer kann nicht verifiziert werden.";
}
default:
break;
}
?>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Verify Number by SMS</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h1>Rufnummer verifizieren</h1>
<?php if ($stage == SESSION_STAGE_START): ?>
<p>Bitte geben Sie Ihre Handynummer ein.</p>
<form action="" method="post" class="form-inline">
<input type="tel" name="tel" placeholder="Handynummer" class="form-control"/>
<button type="submit" class="btn btn-default">Nummer prüfen</button>
</form>
<?php elseif ($stage == SESSION_STAGE_VERIFY): ?>
<p>Bitte geben Sie den Code ein, den wir Ihnen per SMS geschickt haben.</p>
<form action="" method="post" class="form-inline">
<input type="text" name="verify" placeholder="Verification Code" class="form-control"/>
<input type="hidden" name="tel" value="<?php echo $_SESSION["number"] ?>"/>
<button type="submit" class="btn btn-default">Verifizieren</button>
</form>
<?php elseif ($stage == SESSION_STAGE_FINISHED): ?>
<div class="alert alert-success" role="alert">Ihre Nummer <strong><?php echo $_SESSION["number"] ?></strong> wurde verifiziert.</div>
<?php else: ?>
<div class="alert alert-danger" role="alert">Fehler: <?php echo $error;?> <a href="">Erneut versuchen</a></div>
<?php endif ?>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment