1、新建aliyunsms.php,放在D:\MYOA\webroot\task目录下
<?php
include_once "task/auth.php";
include_once 'inc/utility_all.php';
$query = "SELECT * from OFFICE_TASK where TASK_CODE='aliyunsms'";
$cursor = exequery(TD::conn(), $query);
if (!$cursor) {
echo "-ERR " . get_err_msg($query, $TASK_ID);
exit();
}
if ($ROW = mysql_fetch_array($cursor)) {
$INTERVAL = $ROW["INTERVAL"];
}
$INTERVAL += 1;
$SMS_ID_STR = "";
$query = "SELECT SMS_ID,PHONE,CONTENT from SMS2 where SEND_FLAG='0' group by SMS_ID";
$cursor = exequery(TD::conn(), $query);
if (!$cursor) {
echo "-ERR " . get_err_msg($query, $TASK_ID);
exit();
}
while ($ROW = mysql_fetch_array($cursor)) {
$SMS_ID_STR = $ROW["SMS_ID"];
$SEND_TIME = gmdate ("Y-m-d\TH:i:s\Z",$ROW["SEND_TIME"]);
$PHONE_STR = strToUtf8($ROW['PHONE']);
$CONTENT_STR = strToUtf8($ROW['CONTENT']);
$CONTENT_ARR = mbStrSplit($CONTENT_STR, 20);//阿里短信字符限制,个人认证 20,企业认证:联系客服取消字符限制
//个人认证
//$j=count($CONTENT_ARR);
//for($i=0;$i<$j;$i++){
//$result=SendSms($PHONE_STR,$CONTENT_ARR[i]);
//}
//企业认证
$result=SendSms($PHONE_STR,$CONTENT_STR);
if($result['SendSmsResponse']['Code'] == 'OK'){
$query = "UPDATE SMS2 SET SEND_FLAG='1' WHERE SMS_ID=".$SMS_ID_STR;
$cursor = exequery(TD::conn(), $query);
}
}
update_office_task($TASK_ID, "1", date("Y-m-d H:i:s", time()));
echo "+OK";
function mbStrSplit ($string, $len) {
$start = 0;
$strlen = mb_strlen($string);
while ($strlen) {
$array[] = mb_substr($string,$start,$len,"utf8");
$string = mb_substr($string, $len, $strlen,"utf8");
$strlen = mb_strlen($string);
}
return $array;
}
function strToUtf8($str){
$encode = mb_detect_encoding($str, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5'));
if($encode == 'UTF-8'){
return $str;
}else{
return mb_convert_encoding($str, 'UTF-8', $encode);
}
}
function sendSms($PHONE,$CONTENT) {
// 注意使用GMT时间
date_default_timezone_set("GMT");
$target = "https://dysmsapi.aliyuncs.com/?";
$dateTimeFormat = 'Y-m-d\TH:i:s\Z'; // ISO8601规范
$accessKeyId = 'your Access Key ID'; // 这里填写您的Access Key ID
$accessKeySecret = 'your Access Key Secret'; // 这里填写您的Access Key Secret
$ParamString='{"content":"'.$CONTENT.'"}'; //短信模板变量
$data = array(
'AccessKeyId' => $accessKeyId,
'Action' => 'SendSms',
'Format' => 'XML',
'OutId' => '123',
'PhoneNumbers' => $PHONE,
'RegionId' => 'cn-hangzhou',
'SignName'=>'XXXX科技有限公司',
'SignatureMethod' => 'HMAC-SHA1',
'SignatureNonce'=> uniqid(),
'SignatureVersion' => '1.0',
'TemplateCode' => 'SMS_XXXXXX',
'TemplateParam' => $ParamString,
'Timestamp' => date($dateTimeFormat),
'Version' => '2017-05-25'
);
// 计算签名并把签名结果加入请求参数
$data['Signature'] = computeSignature($data, $accessKeySecret);
// 发送请求
$result = xml_to_array(https_request($target.http_build_query($data)));
//header("Content-Type: text/plain; charset=utf-8"); // 输出为utf-8的文本格式,仅用于测
//print_r($result);
//echo "<br><br>".$target . http_build_query($data);
return $result;
}
function https_request($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
if (curl_errno($curl)) {return 'ERROR '.curl_error($curl);}
curl_close($curl);
return $data;
}
function xml_to_array($xml){
$reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
if(preg_match_all($reg, $xml, $matches)){
$count = count($matches[0]);
for($i = 0; $i < $count; $i++){
$subxml= $matches[2][$i];
$key = $matches[1][$i];
if(preg_match( $reg, $subxml )){
$arr[$key] = xml_to_array( $subxml );
}else{
$arr[$key] = $subxml;
}
}
}
return @$arr;
}
function percentEncode($str)
{
// 使用urlencode编码后,将"+","*","%7E"做替换即满足ECS API规定的编码规范
$res = urlencode($str);
$res = preg_replace('/\+/', '%20', $res);
$res = preg_replace('/\*/', '%2A', $res);
$res = preg_replace('/%7E/', '~', $res);
return $res;
}
function computeSignature($parameters, $accessKeySecret)
{
// 将参数Key按字典顺序排序
ksort($parameters);
// 生成规范化请求字符串
$canonicalizedQueryString = '';
foreach($parameters as $key => $value)
{
$canonicalizedQueryString .= '&' . percentEncode($key)
. '=' . percentEncode($value);
}
// 生成用于计算签名的字符串 stringToSign
$stringToSign = 'GET&%2F&' . percentencode(substr($canonicalizedQueryString, 1));
//echo "<br>".$stringToSign."<br>";
// 计算签名,注意accessKeySecret后面要加上字符'&'
$signature = base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret . '&', true));
return $signature;
}
?>
2、通达OA定时任务添加定时发送手机短信任务,
INSERT INTO `office_task` VALUES ( '0', 1, '00:00:00', '2021-3-3 13:58:53', 1, '2021-03-03 13:58:53', '/task/aliyunsms.php', '发送阿里短信', '定时发送短信到手机', 'aliyunsms', '1', '1', '', 0);
最终效果