The client sends a request to the server and informs the user by short message. This is a very common way of pushing messages. This time, we need to do the same.
The mobile short message platform we choose is 10086 cloud MAS platform, and the interface is the simplest HTTP interface.
<?php
/**
* ==================================================================
* created by YYXOCHEN on 2018.09.27
* Copyright (c) 2017-2027 YYXOCHEN All Rights Reserved
* ==================================================================
* Class of http Request Sending Common Short Message Function for Mobile Cloud MAS System
* You need to configure the relevant account of the interface requested by http, and the data can be maintained in config/config.ini
* ------------------------------------------------------------------
* Interface parameter description:
* @param string $ecName
* @param string $apId
* @param string $mobiles Comma-separated cell phone numbers
* @param string $content
* @param string $sign Downloaded signatures include
* @param string $addSerial Extension codes are filled in according to the channel applied to the mobile company. If the application matches the channel accurately, the blank string (") is filled in. Otherwise, the extension codes allowed by the mobile company are added.
* @param string $mac API Input parameter signature results, signature algorithm: ecName, apId, secretKey, mobiles, content, sign, addSerial are spliced in sequence, and then calculated by md5(32-bit lowercase).
* The above data requires character set utf8
* POST Request path: http://112.35.1.155:1992/sms/norsubmit
* ------------------------------------------------------------------
* Use examples:
* require_once "util/mas.10086.class.php";
* $mas = new Mas10086();
* $response = $mas->sendSms('138 XXXX XXXX', 'Hello World!');
* var_dump($response);
* ------------------------------------------------------------------
* version 1.0
* sms SMS can be sent through http interface, which adapts to http 2.1 version of mas.
*/
class Mas10086
{
/**
* Project Constant Configuration, which is not required if config exists, is configured here as the default value
* A more general approach would be to configure [MAS_10086] in config.ini
*/
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 = '';
/**
* Initialization: Read config and set configuration parameters
* If no configuration is available, the default is used
*/
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');
}
}
/**
* According to the interface specification of MAS, we need to assemble a set of mac strings for verification.
* @param string $mobiles Comma-separated telephone number string
* @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) );
}
/**
* Built-in a curl customized function for sending requests, specially configured
* @param string $data Data sent
* @return string Request result information
*/
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
* Send Short Message Function
* @param array $phoneNumberList Telephone number array
* @param string $content SMS text
* @return object Mas10086 Standard interface*
* @param string $rspcod, Response code (based on the following return value)
* @param string $msgGroup, Message batch number, generated by cloud MAS platform, is used to verify the consistency of short message submission report and status report (value msgGroup). Note: If data validation is not empty through msgGroup
* @param boolean $success
*/
public function sendSms($phoneList, $content='Come from AsieMatrix Information')
{
if (is_array($phoneList)) {
if (count($phoneList) == 0) {
return false;
}
$mobiles = implode(',', $phoneList);
} else {
$mobiles = $phoneList;
}
$content .= "\n\r".'[System SMS, please do not reply]';
$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;
}
}
?>