前几天在开发一个app的api接口时候遇到个短信登录的问题,但是查了网络上的资料都是有问题的,于是找了阿里大于的短信api,主要是阿里巴巴肯定是相对可靠的。
so。首选还是阿里大于了。
准备以下资源:
Thinkphp 3.2.3:http://www.thinkphp.cn/down/611.html
阿里大于官方api文档:https://api.alidayu.com/doc2/apiList.htm
引用的第三方类:https://github.com/flc1125/alidayu
导入第三方类
先下载第三方类并将src下的Alidayu文件夹导入到Thinkphp 3.2.3的Vendor目录下
ThinkPHP\Library\Vendor
注册发短信帐号
登录阿里大于->创建应用->创建短信签名->创建验证码短信模版->等待审核。
审核完毕后,即可将下图标出的信息拷贝下来,放入到代码中使用了。
配置代码
头部引用namespace及use
namespace Home\Controller; use Think\Controller; use Flc\Alidayu\Client; use Flc\Alidayu\App; use Flc\Alidayu\Requests\AlibabaAliqinFcSmsNumSend; use Flc\Alidayu\Requests\IRequest;
控制器代码
/** * 获取随机位数数字,用于生成短信验证码 * @param integer len 长度 * @return string */ protected function rand_string(len = 6){ chars = str_repeat('0123456789',len); chars = str_shuffle(chars); str = substr(chars, 0, len); returnstr; } /** * 短信验证码发送 * @author github.com/godcheese * @link https://github.com/godcheese/alidayu4thinkphp3_2_3 * @fork https://github.com/flc1125/alidayu */ public function send_sms(){ // 手动加载类, 因为thinkphp 3.2.3 还未支持composer,所以只能自己手动用 Vendor 加载 Vendor('Alidayu.App'); Vendor('Alidayu.Client'); Vendor('Alidayu.Support'); Vendor('Alidayu.Requests.IRequest'); Vendor('Alidayu.Requests.Request'); Vendor('Alidayu.Requests.AlibabaAliqinFcSmsNumSend'); // Alidayu发送短信配置信息 alidayu_config=array( 'app'=>array( 'app_key'=>'23531770', 'app_secret'=>'83b81a43f4ffd4bba1e7y18b12337dd7' ), 'sign'=>'呼啦呼啦', // 短信签名 'sms_code_template_id'=>'SMS_31680012' // 短信模板ID );mobile_phone='18888888888'; // 接收短信的手机号码 client = new Client(new App(alidayu_config['app'])); request = new AlibabaAliqinFcSmsNumSend;request->setRecNum(mobile_phone) ->setSmsParam([ 'number' =>this->rand_string() // 验证码参数 ]) ->setSmsFreeSignName(alidayu_config['sign']) ->setSmsTemplateCode(alidayu_config['sms_code_template_id']); response =client->execute(request); var_dump(response);// 打印请求结果 }
运行结果
object(stdClass)[10] public 'result' => object(stdClass)[11] public 'err_code' => string '0' (length=1) public 'model' => string '104846809366^1106684995108' (length=26) public 'success' => boolean true public 'request_id' => string 'z25t9celcru1' (length=12)
实例下载
访问: (测试发短信)
http://yourDomain/Home/Index/send_sms
例子完整代码:https://github.com/godcheese/alidayu4thinkphp3_2_3