用php来接入短信验证码接口

在做项目的初期,我对这个业务毫无头绪,所以我就询问了贴吧里的大神,但是一开贴广告铺天盖地,大神没有招来,打广告的招来很多,我当时感觉就有点儿不靠谱,想说自己找找看,就百度了关键字短信平台,前五家里面随便找了一家可以试用的,结果接入起来很顺利,还有条数赠送,很适合我这样刚开始做项目的。
我选择的是互亿无线短信平台(www.ihuyi.com)大家可以参考一下
现在我把当时开发的技术源码分享给大家,希望能对大家有所帮助

<?php

header("Content-Type:text/html;charset=utf-8");

$apikey = "xxxxxxxxxxxxxxx"; //修改为您的apikey(https://www.yunpian.com)登录官网后获取

$mobile =$_POST['mobile']; //获取传入的手机号

// $mobile = "xxxxxxxxxxx"; //请用自己的手机号代替

$num = rand(1000,9999);   //随机产生四位数字的验证码

setcookie('shopCode',$num);

$text="【蒙羊羊】您的验证码是".$num."。";

$ch = curl_init();

  

/* 设置验证方式 */

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',

'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));

/* 设置返回结果为流 */

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  

/* 设置超时时间*/

curl_setopt($ch, CURLOPT_TIMEOUT, 10);

  

/* 设置通信方式 */

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

  

// 取得用户信息

$json_data = get_user($ch,$apikey);

$array = json_decode($json_data,true);

// echo '<pre>';print_r($array);

  

// 发送短信

$data=array('text'=>$text,'apikey'=>$apikey,'mobile'=>$mobile);

$json_data = send($ch,$data);

$array = json_decode($json_data,true);

// echo '<pre>';print_r($array);

  

// 发送模板短信

// 需要对value进行编码

$data = array('tpl_id' => '1', 'tpl_value' => ('#code#').

'='.urlencode($num).

'&'.urlencode('#company#').

'='.urlencode('蒙羊羊'), 'apikey' => $apikey, 'mobile' => $mobile);

// print_r ($data);

$json_data = tpl_send($ch,$data);

$array = json_decode($json_data,true);

  

  

echo $num;

  

  

// 发送语音验证码

// $data=array('code'=>$num,'apikey'=>$apikey,'mobile'=>$mobile);

// $json_data =voice_send($ch,$data);

// $array = json_decode($json_data,true);

// echo $num;

  

// 发送语音通知,务必要报备好模板

/*

模板: 课程#name#在#time#开始。 最终发送结果: 课程深度学习在14:00开始

 */

  

$tpl_id = 'xxxxxxx'; //修改为你自己后台报备的模板id

$tpl_value = urlencode('#time#').'='.urlencode($num).'&'.urlencode('#name#').'='.urlencode('蒙羊羊');

$data=array('tpl_id'=>$tpl_id,'tpl_value'=>$tpl_value,'apikey'=>$apikey,'mobile'=>$mobile);

$json_data = notify_send($ch,$data);

$array = json_decode($json_data,true);

// echo $num;

  

  

curl_close($ch);

  

/************************************************************************************/

//获得账户

function get_user($ch,$apikey){

curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/user/get.json');

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apikey)));

$result = curl_exec($ch);

$error = curl_error($ch);

checkErr($result,$error);

return $result;

}

function send($ch,$data){

curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

$result = curl_exec($ch);

$error = curl_error($ch);

checkErr($result,$error);

return $result;

}

function tpl_send($ch,$data){

curl_setopt ($ch, CURLOPT_URL,

'https://sms.yunpian.com/v2/sms/tpl_single_send.json');

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

$result = curl_exec($ch);

$error = curl_error($ch);

checkErr($result,$error);

return $result;

}

function voice_send($ch,$data){

curl_setopt ($ch, CURLOPT_URL, 'http://voice.yunpian.com/v2/voice/send.json');

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

$result = curl_exec($ch);

$error = curl_error($ch);

checkErr($result,$error);

return $result;

}

function notify_send($ch,$data){

curl_setopt ($ch, CURLOPT_URL, 'https://voice.yunpian.com/v2/voice/tpl_notify.json');

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

$result = curl_exec($ch);

$error = curl_error($ch);

checkErr($result,$error);

return $result;

}

  

function checkErr($result,$error) {

if($result === false)

{

echo 'Curl error: ' . $error;

}

else

{

//echo '操作完成没有任何错误';

}

}

  

?>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值