首页 > 编程 > PHP > 正文

php 阿里短信接口

2019-11-06 08:21:46
字体:
来源:转载
供稿:网友
<?php/** *  阿里大于短信接口 */namespace Home/Controller;use Think/Controller;class IndexController extends Controller{    //处理数据    public function index(){        $phone = "15222222222";  //传值:手机号        $number = mt_rand('000000','999999'); //获取随机六位验证码        $rest = $this->sendsms($phone,$number);        $rest_arr = $this->object_to_array($rest);        if(!empty($rest_arr['result']) &&  $rest_arr['result']['err_code'] == '0'){            session('sms_code',$number);            echo "1";exit; //success        }else{            echo "2"; exit;//fail        }    }    //封装发送短信api    public function sendsms($phone,$number){        require './ThinkPHP/Library/Org/Util/sendsms/ToPSDk.php';//引入加载相关的类文件        $c = new /TopClient();        $c->appkey = '';        $c->secretKey = '';        $c->format = 'json';        $req = new /AlibabaAliqinFcSmsNumSendRequest();        $req->setSmsType("normal");        $req->setSmsFreeSignName("");  //阿里大于管理中心设置的短信签名        $req->setSmsParam("{'number':'".$number."'}"); // 根据短信模板发送的内容(阿里大于管理中心配置)        $req->setRecNum($phone);  //接收短信的号码        $req->setSmsTemplateCode("");  //上面使用的短信模板id(阿里大于管理中心配置)        $resp = $c->execute($req);        return $resp;    }    /**     * @todo 将对象转换成数组     * @param unknown_type $obj     * @return unknown     */    public function object_to_array($obj){        $_arr = is_object($obj) ? get_object_vars($obj) :$obj;        foreach ($_arr as $key=>$val){            $val = (is_array($val) || is_object($val)) ? $this->object_to_array($val):$val;            $arr[$key] = $val;        }        return $arr;    }}?>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表