<!-- https://www.163yun.com/ -->
<!-- 网易云盾短信验证码 -->
<?php
function gen_signature($secretKey, $params)
{
ksort($params);
$buff = "";
foreach ($params as $key => $value) {
if ($value !== null) {
$buff .= $key;
$buff .= $value;
}
}
$buff .= $secretKey;
return md5($buff);
}
/**
* 将输入数据的编码统一转换成utf8
* @params 输入的参数
*/
function toUtf8($params)
{
$utf8s = array();
foreach ($params as $key => $value) {
$utf8s[$key] = is_string($value) ? mb_convert_encoding($value, "utf8", INTERNAL_STRING_CHARSET) : $value;
}
return $utf8s;
}
/**
* 易盾短信发送在线检测请求接口简单封装
* $params 请求参数
*/
function check($params)
{
$params["secretId"] = SECRETID;
$params["businessId"] = BUSINESSID;
$params["version"] = VERSION;
$params["timestamp"] = time()*1000;// time in milliseconds
$params["nonce"] = sprintf("%d", rand()); // random int
$params = toUtf8($params);
$params["signature"] = gen_signature(SECRETKEY, $params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, API_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_TIMEOUT, API_TIMEOUT);
$result = curl_exec($ch);
curl_close($ch);
$resArr = (array) json_decode($result);
if ($resArr === FALSE) {
return array("code" => 500, "msg" => "file_get_contents failed.");
} else {
return (array) json_decode($result);
}
}
// 验证码
function code(){
$str="0123456789";
$code='';
for($i=0;$i<4;$i++){
$code.=rand(0,9);
}
return $code;
}
$code=code();
$params=array(
'needUp'=>"false",
'templateId'=>"",//网易云盾短信模板id,需要提交资料审核
'mobile'=>"$phone",//电话号码
"params"=>"code=$code",//验证码
);
$result=check($params);
var_dump($result);
?>
$params["timestamp"] = time()*1000;// time in milliseconds