Laravel引入第三方短信平台

2020-05-25   阅读:2048   分类:后端    标签: Laravel

Laravel引入第三方短信平台,实现发现邮箱

一、创蓝短信平台:https://zz.253.com/

SDK下载:PHP https://github.com/cl253/chuanglan253-php

ChuanglanSmsApi.php

<?php
namespace App\Lib;
header("Content-type:text/html; charset=UTF-8");
/* *
 * 类名:ChuanglanSmsApi
 * 功能:创蓝接口请求类
 * 详细:构造创蓝短信接口请求,获取远程HTTP数据
 * 版本:1.3
 * 日期:2017-04-12
 * 说明:
 * 以下代码只是为了方便客户测试而提供的样例代码,客户可以根据自己网站的需要,按照技术文档自行编写,并非一定要使用该代码。
 * 该代码仅供学习和研究创蓝接口使用,只是提供一个参考。
 */

class ChuanglanSmsApi {

 	//参数的配置 请登录zz.253.com 获取以下API信息 ↓↓↓↓↓↓↓
 	const API_SEND_URL='http://intapi.253.com/send/json'; //创蓝发送短信接口URL

 	const API_VARIABLE_URL = 'http://XXX/msg/send/json';//创蓝变量短信接口URL

 	const API_BALANCE_QUERY_URL= 'http://XXX/msg/balance/json';//创蓝短信余额查询接口URL

 	const API_ACCOUNT= 'xxxxxx'; // 创蓝API账号

 	const API_PASSWORD= 'xxxxxxxxxxx';// 创蓝API密码
	//参数的配置 请登录zz.253.com 获取以下API信息 ↓↓↓↓↓↓↓


	/**
	 * 发送短信
	 *
	 * @param string $mobile 		手机号码
	 * @param string $msg 			短信内容
	 * @param string $needstatus 	是否需要状态报告
	 */
	public function sendSMS( $mobile, $msg, $needstatus = 'true') {
		
		//创蓝接口参数
		$postArr = array (
			'account'  =>  self::API_ACCOUNT,
			'password' => self::API_PASSWORD,
			'msg' => urlencode($msg),
			'mobile' => $mobile,
			'report' => $needstatus,
       		);
		$result = $this->curlPost( self::API_SEND_URL, $postArr);
		return $result;
	}
	
	/**
	 * 发送变量短信
	 *
	 * @param string $msg 			短信内容
	 * @param string $params 	最多不能超过1000个参数组
	 */
	public function sendVariableSMS( $msg, $params) {
		
		//创蓝接口参数
		$postArr = array (
			'account'  =>  self::API_ACCOUNT,
			'password' => self::API_PASSWORD,
			'msg' => $msg,
			'params' => $params,
			'report' => 'true'
        	);
		
		$result = $this->curlPost( self::API_VARIABLE_URL, $postArr);
		return $result;
	}
	
	/**
	 * 查询额度
	 *
	 *  查询地址
	 */
	public function queryBalance() {
		
		//查询参数
		$postArr = array ( 
		        'account'  =>  self::API_ACCOUNT,
			'password' => self::API_PASSWORD,
		);
		$result = $this->curlPost(self::API_BALANCE_QUERY_URL, $postArr);
		return $result;
	}

	/**
	 * 通过CURL发送HTTP请求
	 * @param string $url  //请求URL
	 * @param array $postFields //请求参数 
	 * @return mixed
	 *  
	 */
	private function curlPost($url,$postFields){
		$postFields = json_encode($postFields);
		$ch = curl_init ();
		curl_setopt( $ch, CURLOPT_URL, $url ); 
		curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
			'Content-Type: application/json; charset=utf-8'   //json版本需要填写  Content-Type: application/json;
			)
		);
		curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
		curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt( $ch, CURLOPT_POST, 1 );
         	curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields);
       		curl_setopt( $ch, CURLOPT_TIMEOUT,60); 
        	curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
        	curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
		$ret = curl_exec ( $ch );
        if (false == $ret) {
            $result = curl_error(  $ch);
        } else {
            $rsp = curl_getinfo( $ch, CURLINFO_HTTP_CODE);
            if (200 != $rsp) {
                $result = "请求状态 ". $rsp . " " . curl_error($ch);
            } else {
                $result = $ret;
            }
        }
		curl_close ( $ch );
		return $result;
	}
	
}

把创蓝短信 sdk 放到 App下的 Lib 目录下。

<?php
namespace App\Http\Controllers;
use App\Lib\ChuanglanSmsApi;


class UserController extends BaseController
{
    /**
     * @desc 获取手机号验证码 interface
     * @date 2020/5/18
     * */
    public function sms_code(Request $request)
    {
        $phone = $request->post('phone');

        /*创蓝短信start*/
        //发送创蓝短信
         $clapi  = new ChuanglanSmsApi();
         $area_code= $request->post('area_code') ? $request->post('area_code') : '86'; //区号
         $code = randomkeys();
         $expireTime = 5*60;
         $to=$phone;

         //设置您要发送的内容:其中“【】”中括号为运营商签名符号,多签名内容前置添加提交
         $result = $clapi->sendSMS($area_code.$to,'【创蓝】您好!验证码是:'.$code);
         if(!is_null(json_decode($result))){
             $output=json_decode($result,true);
             if(isset($output['code'])  && $output['code']=='0'){
                 //TODO 添加成功处理逻辑
                 OutJson::outJsonResult(Hint::SUCCESS, []);
             }else{
                 OutJson::outJsonResult(Hint::ERROR, []);
             }
         }else{
             //TODO 添加错误处理逻辑
             OutJson::outJsonResult(Hint::ERROR, []);
         }
        /*创蓝短信start*/
    }
}

?>


【腾讯云】 爆款2核2G3M云服务器首年 61元,2核2G4M云服务器新老同享 99元/年,续费同价

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://www.tpxhm.com/adetail/330.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×