php实现短信验证

 PHP实现短信验证的整体思路:

  一、申请短信api ->申请网址https://s1.chanyoo.cn/login?url=%2f

 二、编写核心代码(thinkPHP5)

示例:

<?php
//命名空间
namespace app\index\controller;

use think\controller;
use app\index\module;

class Index extends controller{

public function Sms(){
//接受手机号 访问路由上index,php/phone/13012492778
$phone = post('phone');
$rand = rand(1000,9999);

$url = "http://api.chanyoo.cn/utf8/interface/send_sms.aspx?username=wgnoreognreo&password=cnjdskcnkds&content=验证码:".$rand."【短信提示】&receiver=".$phone; //URL为短信发送的api地址,返回为xml的方式
$file = file_get_contents($url); //跨域访问 访问外部网站get


//转换xml结果
$xml = simplexml_load_string($file);
$date = json_decode(json_encode($xml),TRUE); //如果成功,$date['message']返回“短信提交成功”

//进行对$date
if($date['message'] == "短信提交成功"){
$Sms = Sms::create([ //成功后返回json格式的数据
"phone" => $phone,
"rand" => $rand,
]);
echo "存入成功!".$Sms;
};
}

  } 

?>

浏览器打印出的结果如下:

猜你喜欢

转载自www.cnblogs.com/lvhaiqing/p/10012365.html