1、短信接口申请
官网:https://mas.10086.cn,找客户经理或者400电话(需要公司才可以申请,预计申请时间1周)
2、PHP接口代码
url地址:如果本事例中的不可用,需要和云mas技术人员要。(部门接口url不对可能有报错提示,之前使用http://112.35.1.155:1992/sms/tmpsubmit一直报InvalidUsrOrPwd)
<?php function sendMasSms($mobiles,$params) { // 配置参数(需替换为实际值) $config = [ 'url' => 'http://112.33.46.17:37891/sms/tmpsubmit', 'ecName' => 'xxxx公司', 'apId' => 'username', 'secretKey' => 'password', 'sign' => '签名', 'tmpid' => '模版id', 'addSerial' => '', ]; // 生成MAC签名 $macStr = $config['ecName'] . $config['apId'] . $config['secretKey'] . $config['tmpid'] . $mobiles . json_encode($params) . $config['sign'] . $config['addSerial']; $mac = md5($macStr); // 构建请求数据 $postData = [ 'ecName' => $config['ecName'], 'apId' => $config['apId'], 'templateId'=> $config['tmpid'], 'mobiles' => is_array($mobiles) ? implode(',', $mobiles) : $mobiles, 'params' => json_encode($params), 'sign' => $config['sign'], 'addSerial' => $config['addSerial'], 'mac' => $mac ]; // 发送HTTP请求 $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_URL => $config['url'], CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_HTTPHEADER => ['Content-Type: application/json'], CURLOPT_POSTFIELDS => base64_encode(json_encode($postData,JSON_UNESCAPED_UNICODE)) ]); $response = curl_exec($ch); curl_close($ch); return json_decode($response, true); } /* 使用示例 */ $result = sendMasSms('手机号码',["验证码"]); var_dump($result); //返回:array(3) { ["msgGroup"]=> string(22) "0603141940000007647628" ["rspcod"]=> string(7) "success" ["success"]=> bool(true) }
目前有 0 条评论