PHP

互亿短信调用模板 php

Guooo
2019-01-14 / 0 评论 / 3,088 阅读 / 正在检测是否收录...

调用方法

//调用方法
$code = mt_rand(1000, 9999);
$sms = new Huyisms();
// 发送验证码
$result =   $sms->mobile($mobile)->code($code)->send();

// 或者自定义短信内容
// $result = $sms->mobile($mobile)->content('恭喜您的材料已经审核成功,请及时查看。')->send();

if ($result === TRUE){
    //短信验证码发送成功
}else{
    //短信验证码发送失败
    echo $sms->getError();
}  

把短信扩展保存成php文件,修改其中的配置信息即可

<?php

/**
 * 互忆短信发送
 * Class Huyisms
 * @package admin\huyisms\library
 */
class Huyisms
{

    private $config = [];
    private $_params = [];
    public $error = '';

    public function __construct($options = [])
    {
        $config = [
            'huyi_appid'    =>  '',  // todo 修改为你的 APPID
            'huyi_appsecret'    =>  '',  // todo 修改为你的 APPSECRET
            'huyi_temp'    =>  '您的短信验证码为{$code},请勿向任何人提供此验证码。' // 默认短信验证码模板,不用备案,可以不修改
        ];
        $this->config = array_merge($config, is_array($options) ? $options : []);
    }

    /**
     * 发送短信方法
     */
    public function send()
    {
        $this->error = '';
        $params = $this->_params();
        $send_url = 'http://106.ihuyi.cn/webservice/sms.php?method=Submit&';
        $response = $this->_curl($send_url,$params);

        if ($response !== FALSE){
            $res = (array) json_decode($response,true);
            if (isset($res['code'])){
                if ( $res['code'] == 2){
                    return TRUE;
                }
            }
            $this->error = isset($res['msg']) ? $res['msg'] : 'InvalidResultMsg';
        }else{
            $this->error = 'InvalidResult';
        }
        return FALSE;
    }

    /**
     * 获取剩余短信条数
     * @return mixed|string
     */
    public function getNum()
    {
        $this->error = '';
        $params = $this->_params();
        $get_url = 'http://106.ihuyi.com/webservice/sms.php?method=GetNum&';
        $response = $this->_curl($get_url,$params);

        if ($response !== FALSE){
            $res = (array) json_decode($response,true);
            if (isset($res['code']) && $res['code'] == 2){
                return $res['num'];
            }
            return $res['msg'];
        }
        return '';
    }


    /**
     * 接收验证码值,不能和content同时使用
     * @param string $code
     * @return $this
     */
    public function code($code = '')
    {
        $content = str_replace('{$code}',$code,$this->config['huyi_temp']);
        $this->_params['content'] = $content;
        return $this;
    }

    /**
     * 接收短信内容
     * @param string $content
     * @return $this
     */
    public function content($content = '')
    {
        $this->_params['content'] = $content;
        return $this;
    }

    /**
     * 设置参数
     * @param array $param
     * @return Huyisms
     */
    public function param(array $param = [])
    {
        foreach ($param as $k => &$v)
        {
            $v = (string) $v;
        }
        unset($v);
        return $this;
    }


    /**
     * 接收手机
     * @param string $mobile 手机号码
     * @return Huyisms
     */
    public function mobile($mobile = '')
    {
        $this->_params['mobile'] = $mobile;
        return $this;
    }

    /**
     * 返回错误信息
     * @return string
     */
    public function getError()
    {
        return $this->error;
    }

    private function _params()
    {
        return array_merge([
            'account'   => $this->config['huyi_appid'],
            'password'  => $this->config['huyi_appsecret'],
            'time'      => time(),
            'format'    => 'json'
        ],$this->_params);
    }


    //请求数据到短信接口,检查环境是否 开启 curl init。
    private function _curl($url,$params)
    {
        $uri = $url . http_build_query($params);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_URL, $uri);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $reponse = curl_exec($ch);
        curl_close($ch);
        return $reponse;
    }


}
0

评论 (0)

取消