背景
客户端发送一个请求到服务器, 将信息通过短信告知用户. 这个是很常见的一种消息推送的方式. 这次也需要完成这样的功能.
我们选择的移动短信平台为 10086 的云 MAS 的平台, 接口选择最简单的 HTTP 接口.
方案
短信平台 | 数据接口 | 服务端 | 客户端 |
---|---|---|---|
中国移动云 MAS | 云 MAS 提供的 HTTP 接口 | PHP 开发的服务 | html+JS |
必要的非技术准备
需要一个中国移动云 MAS 业务平台的集团账号. 通常应该是业主方购买, 如果乙方购买, 自行脑补运维费怎么弄.
PHP 开发通用的 POST 请求程序与云 MAS 对接.
实战
步骤 | Subject | 备忘 | 说明 |
---|---|---|---|
1 | 获得云 MAS 接口账号 | 移动集团账号登录 < a href="http://mas.10086.cn/login" target="_blank" rel="nofollow" ztid="83" ow="179" oh="17"> http://mas.10086.cn/login 开通 | 详细手册在网站有下载 |
2 | 下载配套的签名秘钥 | 根据移动手册 step by step 操作即可,下载的是一个 Excel 文件中包含 | - |
3 | 使用 PHP curl 功能 | 制作配套的 POST 函数,将短信发送出去 | - |
4 | 完成响应客户端请求的 PHP 脚本 | - | - |
封装一个 Mas Class
附送一个代码段, 不同的需求可以不同的改造.
这里使用了读取 INI 获得配置, 这样不修改程序的情况下, 修改 INI 即可完成数据功能切换.
优化余地: 对应短信发送之后的 response 处理可以根据手册更加丰富的处理.
- <?PHP
- /**
- * ==================================================================
- * created by YYXOCHEN on 2018.09.27
- * Copyright (c) 2017-2027 YYXOCHEN All Rights Reserved
- * ==================================================================
- * 适配移动云 MAS 系统的 http 请求发送普通短信功能的类
- * 需要配置 http 请求的接口的相关账号, 数据可以维护到 config/config.INI 中
- * ------------------------------------------------------------------
- * 接口参数说明:
- * @param string $ecName
- * @param string $apId
- * @param string $mobiles 逗号分隔手机号码
- * @param string $content
- * @param string $sign 下载的签名中有
- * @param string $addSerial 扩展码, 根据向移动公司申请的通道填写, 如果申请的精确匹配通道, 则填写空字符串(""), 否则添加移动公司允许的扩展码
- * @param string $Mac API 输入参数签名结果, 签名算法: 将 ecName,apId,secretKey,mobiles,content ,sign,addSerial 按照顺序拼接, 然后通过 md5(32 位小写)计算后得出的值
- * 以上数据需要字符集 utf8
- * POST 请求路径: http://112.35.1.155:1992/sms/norsubmit
- * ------------------------------------------------------------------
- * 使用范例:
- * require_once "util/mas.10086.class.PHP";
- * $mas = new Mas10086();
- * $response = $mas->sendSms('138 XXXX XXXX', 'Hello World!');
- * var_dump($response);
- * ------------------------------------------------------------------
- * version 1.0
- * 可以通过 http 接口, 发送 sms 普通短信, 接口适配 mas 的 HTTP2.1 版本
- */
- class Mas10086
- {
- /**
- * 项目常量配置, 如果有 config 则可以不用, 这里作为默认值配置
- * 更为通用的方式, 应该是用 config.INI 中配置[MAS_10086]
- */
- const AP_ID = 'XXXX';
- const SIGN = 'XXXX';
- const ADD_SERIAL = '';
- const SECRET_KEY = 'XXXX';
- const EC_NAME = 'XXXX';
- const NORMAL_SMS_URL = 'http://112.35.1.155:1992/sms/norsubmit';
- const MAS_VERSION = '2.1';
- private $apId = '';
- private $sign = '';
- private $addSerial = '';
- private $secretKey = '';
- private $ecName = '';
- private $norSmsUrl = '';
- /**
- * 初始化操作: 读取 config, 设定配置参数
- * 如果没有配置, 则使用默认
- */
- function __construct()
- {
- $INI = parse_ini_file("../../Config/config.INI",true);
- $masKey = 'MAS_10086_'.self::MAS_VERSION;
- if (!isset($INI[$masKey])) {
- $this->apId = self::AP_ID;
- $this->sign = self::SIGN;
- $this->addSerial = self::ADD_SERIAL;
- $this->secretKey = self::SECRET_KEY;
- $this->ecName = self::EC_NAME;
- $this->norSmsUrl = self::NORMAL_SMS_URL;
- } else {
- $this->apId = isset( $INI[$masKey]['AP_ID']) ? $INI[$masKey]['AP_ID'] : self::AP_ID;
- $this->sign = isset( $INI[$masKey]['SIGN']) ? $INI[$masKey]['SIGN'] : self::SIGN;
- $this->addSerial = isset( $INI[$masKey]['ADD_SERIAL']) ? $INI[$masKey]['ADD_SERIAL'] : self::ADD_SERIAL;
- $this->secretKey = isset( $INI[$masKey]['SECRET_KEY']) ? $INI[$masKey]['SECRET_KEY'] : self::SECRET_KEY;
- $this->ecName = isset( $INI[$masKey]['EC_NAME']) ? $INI[$masKey]['EC_NAME'] : self::EC_NAME;
- $this->norSmsUrl = isset( $INI[$masKey]['NORMAL_SMS_URL']) ? $INI[$masKey]['NORMAL_SMS_URL'] : self::NORMAL_SMS_URL;
- xlog('I','read mas10086 config from INI file');
- }
- }
- /**
- * 根据 MAS 的接口规范, 需要装配一组 Mac 字符串做验证
- * @param string $mobiles 逗号分隔得电话号码字符串
- * @param string $content
- * @return string
- */
- private function makeMacString($mobiles,$content)
- {
- $macstr = $this->ecName . $this->apId . $this->secretKey . $mobiles . $content . $this->sign . $this->addSerial;
- return strtolower( md5($macstr) );
- }
- /**
- * 内置一个 curl 定制的发送请求的函数, 专门配置
- * @param string $data 发送的数据
- * @return string 请求结果信息
- */
- private function post($url, $data)
- {
- if (!$url) {
- return false;
- }
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_TIMEOUT, 500);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_POST, true);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- $res = curl_exec($curl);
- curl_close($curl);
- return $res;
- }
- /**
- * sendSms
- * 发送短信函数
- * @param array $phoneNumberList 电话号码数组
- * @param string $content 短信文本
- * @return object Mas10086 的标准接口 *
- * @param string $rspcod, 响应码(根据下面返回值判断)
- * @param string $msgGroup, 消息批次号, 由云 MAS 平台生成, 用于验证短信提交报告和状态报告的一致性 (取值 msgGroup) 注: 如果数据验证不通过 msgGroup 为空
- * @param boolean $success
- */
- public function sendSms($phoneList, $content='来自 AsieMatrix 的信息')
- {
- if (is_array($phoneList)) {
- if (count($phoneList) == 0) {
- return false;
- }
- $mobiles = implode(',', $phoneList);
- } else {
- $mobiles = $phoneList;
- }
- $content .= "\n\r".'[系统短信, 请勿回复]';
- $mobiles = ltrim(rtrim($mobiles, ','),',');
- $macstr = $this->makeMacString($mobiles,$content);
- $data = [
- 'addSerial' => $this->addSerial,
- 'apId' => $this->apId,
- 'content' => $content,
- 'ecName' => $this->ecName,
- 'mobiles' => $mobiles,
- 'sign'=>$this->sign,
- 'Mac' => $macstr
- ];
- $dataContent = base64_encode( json_encode($data) );
- $res = json_decode($this->post($this->norSmsUrl, $dataContent));
- return $res;
- }
- }
- ?>
来源: http://www.jianshu.com/p/1f29362e2a59