滴滴短信接入THINKPHP

本文详细介绍了如何在工具箱中集成并使用SmsSDK.class.php实现短信发送功能,包括初始化SDK、发送单条和群发短信、获取短信状态及剩余可发短信数量等操作。

在工具箱下面添加SmsSDK.class.php文件,下面内容直接复制粘贴,配置自己的信息

<?php
namespace Vendor;
 class SmsSDK{

    function __construct($Address,$Port,$Account,$Token){
        $this->Address = "";//IP地址 不加http://
        $this->Port = "";//端口
        $this->Account = "";//账户
        $this->Token = "";//token
    }
    /**
     * 发起HTTP请求
     */
     function curl_post($url,$data,$header,$post=1)
     {
       //初始化curl
       $ch = curl_init();
       //参数设置  
       $res= curl_setopt($ch, CURLOPT_URL,$url);//请求地址
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);//不验证host
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//不验证证书
       curl_setopt($ch, CURLOPT_HEADER, 0);
       curl_setopt($ch, CURLOPT_POST, $post);//请求方式
       if($post){
          curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//post数据
       }
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt($ch,CURLOPT_HTTPHEADER,$header);//设置头信息
       $result = curl_exec ($ch);
       //连接失败
       if($result == FALSE){
          $result = "";
       }
       curl_close($ch);
       return $result;
     }
    /**
    * 发送短信(单条)
    * @param tempId 模板Id
    * @param mobile 手机号码
    * @param body 内容
    * @param rType 响应数据类型   0 Json 类型,1 xml 类型
    */       
    function send($tempId, $mobile, $body, $rType)
    {
        // 生成请求
        //$body = urlencode(mb_convert_encoding($body, 'utf-8', 'gb2312'));//url编码
        $url = "http://$this->Address:$this->Port/SMS/Send";
        $data = "account=$this->Account&token=$this->Token&tempid=$tempId&mobile=$mobile&content=$body&type=$rType";
        // 生成包头
        $header = array("charset=utf-8");
        // 发送请求
        return $this->curl_post($url,$data,$header,1);
    }
    /**
    * 发送短信(群发)
    * @param tempId 模板Id
    * @param mobile 手机号码,多个号码用英文分号;分割
    * @param body 短信内容
    * @param rType 响应数据类型   0 Json 类型,1 xml 类型
    */
    function sendMultiple($tempId, $mobile, $body, $rType)
    {
        // 生成请求
        $body = urlencode(mb_convert_encoding($body, 'utf-8', 'gb2312'));//url编码
        $url = "http://$this->Address:$this->Port/SMS/SendMultiple";
        $data = "account=$this->Account&token=$this->Token&tempid=$tempId&mobile=$mobile&content=$body&type=$rType";
        // 生成包头
        $header = array("charset=utf-8");
        // 发送请求
        return $this->curl_post($url,$data,$header,1);
    }
    /**
    * 获取短信状态
    * @param smsId 短信Id
    * @param rType 响应数据类型 0 Json 类型,1 xml 类型
    */
    function smsStatus($smsId, $rType)
    {
        // 生成请求
        $url = "http://$this->Address:$this->Port/SMS/SMSStatus";
        $data = "account=$this->Account&token=$this->Token&smsid=$smsId&type=$rType";
        // 生成包头
        $header = array("charset=utf-8");
        // 发送请求
        return $this->curl_post($url,$data,$header,1);
    }
    /**
    * 获取剩余可发短信条数
    * @param rType 响应数据类型 0 Json 类型,1 xml 类型
    */
    function smsNum($rType)
    {
        // 生成请求
        $url = "http://$this->Address:$this->Port/SMS/SMSNum";
        $data = "account=$this->Account&token=$this->Token&type=$rType";
        // 生成包头
        $header = array("charset=utf-8");
        // 发送请求
        return $this->curl_post($url,$data,$header,1);
    }
    function sendSMS($tempId,$mobile,$body,$rType,$mToken,$extno)
    {
        global $Address,$Port,$Account,$Token;
         //初始化SDK
         $rest = new KXTSmsSDK($Address,$Port,$Account,$Token);
         // 发送短信
         $result = $rest->send($tempId, $mobile, $body, $rType,$mToken,$extno);
         if($result == NULL ) {
             //echo "result error!";
             break;
         }
        //自己代码业务逻辑
        //echo $result;

    }
 }

    /**
     * 发送短信(单条)
     * 模板Id
     * 发送合法的手机号
     * 短信内容
     * 响应类型  0 json类型,1 xml类型
     * $mToken = "";//可选
     * $extno ="";//扩展号 可选
     */


 ?>

在对应的控制器里面引用SmsSDK.class.php

在命名空间下面加入use Vendor;

在触发短信的事件加入下面的代码

$SMS = new \Vendor\KXTSmsSDK;//实例化
$SMS->sendSMS("模板Id","模板内容",1,"","");//需对应短信模板列表

PHP开发乐园(鼠标手)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值