PHP 链接手机短信接口

以阿里大于为例:

下载官方sdk包地址:http://ytx-sdk.oss-cn-shanghai.aliyuncs.com/dysms_php.zip?spm=a2c4g.11186623.2.4.dAvEjn&file=dysms_php.zip

如果只是单纯发送手机短信,那么只使用aliyun-dysms-php-sdk/api_sdk/lib里面的两个文件即可(Api和Core),把这两个文件放入自己的框架中。

具体使用代码如下:

<?php
/**
 * Created by PhpStorm.
 * User: saber_H
 * Date: 2018/8/18 0018
 * Time: 下午 3:31
 **/
namespace App\Http\Controllers\Index;

ini_set("display_errors", "on");  //设置开启报错模式,可取消
use Illuminate\Http\Request;
use Aliyun\Core\Config;
use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;
use Aliyun\Api\Sms\Request\V20170525\QuerySendDetailsRequest;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

// 加载区域结点配置
Config::load();

class SmsComController extends Controller{

    static $acsClient = null;

    /**
     * 取得AcsClient
     *
     * @return DefaultAcsClient
     */
    public static function getAcsClient() {
        //产品名称:云通信流量服务API产品,开发者无需替换
        $product = "Dysmsapi";

        //产品域名,开发者无需替换
        $domain = "dysmsapi.aliyuncs.com";

        // TODO 此处需要替换成开发者自己的AK (https://ak-console.aliyun.com/)
        $accessKeyId = ""; // AccessKeyId

        $accessKeySecret = ""; // AccessKeySecret


        // 暂时不支持多Region
        $region = "cn-hangzhou";

        // 服务结点
        $endPointName = "cn-hangzhou";


        if(static::$acsClient == null) {

            //初始化acsClient,暂不支持region化
            $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);

            // 增加服务结点
            DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);

            // 初始化AcsClient用于发起请求
            static::$acsClient = new DefaultAcsClient($profile);
        }
        return static::$acsClient;
    }

    public function smssend(Request $request){
        header('Content-Type: text/plain; charset=utf-8');
//        $p_verify = I('post.p_verify','');
//        if(empty($p_verify)){
//            $this -> ajaxReturn('图片验证码不能为空');
//        }
//        $verify = new Verify();
//        $res = $verify->check($p_verify);
//        if(!$res){
//            $this -> ajaxReturn('图片验证码不正确');
//        }
        $phone = $request ->post('phone');
        $code = rand(1000,9999);
        $tmpcode_data = DB::table('tmpcode') -> where('email',$phone) -> first();
        if(!empty($tmpcode_data)){
            return '改手机号已注册请登录';
        }
        $response = self::sendSms(
            "", // 短信签名
            "", // 短信模板编号
            $phone, // 短信接收者
            Array(  // 短信模板中字段的值
                "code"=>$code,
            )
//            "123"   // 流水号,选填
        );
       if($response->Code == 'OK'){

           $arr = [
               'email' => $phone,
               'code' => $code,
               'time' => time()
           ];
           DB::table('tmpcode') -> insert($arr);
           return '短信发送成功';
       }else{
           return '短信发送失败';
       }

    }




    /**
     * 发送短信
     *
     * @param string $signName <p>
     * 必填, 短信签名,应严格"签名名称"填写,参考:<a href="https://dysms.console.aliyun.com/dysms.htm#/sign">短信签名页</a>
     * </p>
     * @param string $templateCode <p>
     * 必填, 短信模板Code,应严格按"模板CODE"填写, 参考:<a href="https://dysms.console.aliyun.com/dysms.htm#/template">短信模板页</a>
     * (e.g. SMS_0001)
     * </p>
     * @param string $phoneNumbers 必填, 短信接收号码 (e.g. 12345678901)
     * @param array|null $templateParam <p>
     * 选填, 假如模板中存在变量需要替换则为必填项 (e.g. Array("code"=>"12345", "product"=>"阿里通信"))
     * </p>
     * @param string|null $outId [optional] 选填, 发送短信流水号 (e.g. 1234)
     * @return stdClass
     */
    public static function sendSms($signName, $templateCode, $phoneNumbers, $templateParam = null, $outId = null) {

        // 初始化SendSmsRequest实例用于设置发送短信的参数
        $request = new SendSmsRequest();

        // 必填,设置雉短信接收号码
        $request->setPhoneNumbers($phoneNumbers);

        // 必填,设置签名名称
        $request->setSignName($signName);

        // 必填,设置模板CODE
        $request->setTemplateCode($templateCode);

        // 可选,设置模板参数
        if($templateParam) {
            $request->setTemplateParam(json_encode($templateParam));
        }

        // 可选,设置流水号
        if($outId) {
            $request->setOutId($outId);
        }

        // 发起访问请求
        $acsResponse = static::getAcsClient()->getAcsResponse($request);

//         打印请求结果
//         var_dump($acsResponse);

        return $acsResponse;

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值