Javascript net::在sendotp.msg91 api中生成OTP代码时是否重置了错误连接?
我想为我的网站实现一个otp密码,因为我为otp选择的短信网关是sendotp.msg91 api,我注册到网站并为此创建了应用程序get key,然后我收到了php和JavaScript的代码,我更改了php文件中的API密钥,如下所示,以及其他配置,并将其托管到本地主机中,但它显示错误jquery.min.js:4 POST net::ERR_CONNECTION_RESET下面是我的代码 SENDOTP.PHPJavascript net::在sendotp.msg91 api中生成OTP代码时是否重置了错误连接?,javascript,php,jquery,html,json,Javascript,Php,Jquery,Html,Json,我想为我的网站实现一个otp密码,因为我为otp选择的短信网关是sendotp.msg91 api,我注册到网站并为此创建了应用程序get key,然后我收到了php和JavaScript的代码,我更改了php文件中的API密钥,如下所示,以及其他配置,并将其托管到本地主机中,但它显示错误jquery.min.js:4 POST net::ERR_CONNECTION_RESET下面是我的代码 SENDOTP.PHP <?php session_start(); class SendOTP
<?php
session_start();
class SendOTP
{
private $baseUrl = "https://sendotp.msg91.com/api";
public function callGenerateAPI($request)
{
$data = array(
"countryCode" => $request['countryCode'],
"mobileNumber" => $request['mobileNumber'],
"getGeneratedOTP" => true
);
$data_string = json_encode($data);
$ch = curl_init($this->baseUrl . '/generateOTP');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'application-Key: APP KEY'
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
public function saveOTP($OTP)
{
//save OTP to your session
$_SESSION["oneTimePassword"] = $OTP;
// OR save the OTP to your database
//connect db and save it to a table
return true;
}
public function generateOTP($request)
{
//call generateOTP API
$response = $this->callGenerateAPI($request);
$response = json_decode($response, true);
if ($response["status"] == "error") {
//customize this as per your framework
$resp['message'] = $response["response"]["code"];
return json_encode($resp);
}
//save the OTP on your server
if ($this->saveOTP($response["response"]["oneTimePassword"])) {
$resp['message'] = "OTP SENT SUCCESSFULLY";
return json_encode($resp);
}
}
public function verifyOTP($request)
{
//This is the sudo logic you have to customize it as needed.
//your verify logic here
if ($request["oneTimePassword"] == $_SESSION["oneTimePassword"]) {
$resp['message'] = "NUMBER VERIFIED SUCCESSFULLY";
} else {
$resp['message'] = "OTP INVALID";
}
return json_encode($resp);
// OR get the OTP from your db and check against the OTP from client
}
public function verifyBySendOtp($request)
{
$data = array(
"countryCode" => $request['countryCode'],
"mobileNumber" => $request['mobileNumber'],
"oneTimePassword" => $request['oneTimePassword']
);
$data_string = json_encode($data);
$ch = curl_init($this->baseUrl . '/verifyOTP');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'application-Key: Your-application-key'
));
$result = curl_exec($ch);
curl_close($ch);
$response = json_decode($result, true);
if ($response["status"] == "error") {
//customize this as per your framework
$resp['message'] = $response["response"]["code"];
} else {
$resp['message'] = "NUMBER VERIFIED SUCCESSFULLY";
}
return json_encode($resp);
}
}
$sendOTPObject = new SendOTP();
if (isset($_REQUEST['action']) && !empty($_REQUEST['action'])) {
echo $sendOTPObject->$_REQUEST['action']($_REQUEST);
} else {
echo "Error Wrong api";
}
?>
index.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> </script>
<script type="text/javascript">
function sendOTP() {
var ccode=document.getElementById('cCode').value;
var number=document.getElementById('mobNumber').value;
var data = {"countryCode":ccode, "mobileNumber": number};
$.ajax({
url: 'http://localhost/sms/sendotp.php?action=generateOTP',
type: 'POST',
dataType: 'json',
data: data,
success: function(data){
var resp = JSON.parse(response)
console.log(resp.status);
},
error: function(jqXHR, textStatus, ex) {
console.log(textStatus + "," + ex + "," + jqXHR.responseText);
}
});
}
function verifyOTP() {
var data = {"countryCode": "country code", "mobileNumber": "Mobile number to be verified","oneTimePassword":"One time password"};
$.ajax({
url: 'http://localhost/sms/sendotp.php?action=verifyOTP',
type: 'POST',
dataType: 'json',
data: data,
success: function(data){
var resp = JSON.parse(response)
console.log(resp.status);
},
error: function(jqXHR, textStatus, ex) {
console.log(textStatus + "," + ex + "," + jqXHR.responseText);
}
}) ;
}
</script>
<div class="send-otp">
<form onsubmit="return false;">
<label>Enter your phone number</label>
<div class="clearfix">
<input id="cCode" type="text" name="countryCode" placeholder="91" style="float:left; width:20%; margin-right:3%; color=#111111;">
<input id="mobNumber" type="text" name="number" placeholder="98XXXXXXXX" style="float:left; width:77%;color=#111111;">
</div><button onclick="sendOTP();" class="btn-primary btn">Send OTP</button> </form>
函数sendOTP(){
var ccode=document.getElementById('ccode')。值;
var number=document.getElementById('mobNumber')。值;
var data={“countryCode”:ccode,“mobileNumber”:number};
$.ajax({
网址:'http://localhost/sms/sendotp.php?action=generateOTP',
键入:“POST”,
数据类型:“json”,
数据:数据,
成功:功能(数据){
var resp=JSON.parse(响应)
控制台日志(各自的状态);
},
错误:函数(jqXHR、textStatus、ex){
日志(textStatus+”、“+ex+”、“+jqXHR.responseText);
}
});
}
函数verifyOTP(){
var数据={“国家代码”:“国家代码”,“手机号码”:“待验证手机号码”,“一次性密码”:“一次性密码”};
$.ajax({
网址:'http://localhost/sms/sendotp.php?action=verifyOTP',
键入:“POST”,
数据类型:“json”,
数据:数据,
成功:功能(数据){
var resp=JSON.parse(响应)
控制台日志(各自的状态);
},
错误:函数(jqXHR、textStatus、ex){
日志(textStatus+”、“+ex+”、“+jqXHR.responseText);
}
}) ;
}
输入您的电话号码
发送OTP
通常在DNS未解析时发生此错误。。尝试使用您的本地主机IP而不是它显示相同的错误