php 调用中国移动第三方短信接口
<?php header("Content-type:text/html; charset=UTF-8"); /** * Class SendApi */ class SendApi { const API_SEND_URL='http://112.35.1.155:1992/sms/norsubmit'; //请求地址 const API_ACCOUNT=''; //账号 const API_PASSWORD=''; //密码 const API_ECNAME=''; //登录企业名称 const API_SIGN=''; //签名 const API_ADDSERIAL=''; //服务代码 /** * @param array $mobiles * @param string $content * @return string * @author: xuxiangang * @Time: 2022/7/12 17:39 * @Desc:短信接口 */ function sendMessage($mobiles = array(),$content=''){ if (!$mobiles && !$content){ return "参数错误"; } $headers = array(header("Content-type: text/html;charset=utf-8")); $url = self::API_SEND_URL; $ecName = self::API_ECNAME; $apId = self::API_ACCOUNT; $secretKey = self::API_PASSWORD; $sign = self::API_SIGN; $addSerial = self::API_ADDSERIAL; $mobile = implode(",",$mobiles); $content = $content; $mac = md5($ecName.$apId.$secretKey.$mobile.$content.$sign.$addSerial); $data = array( "apId" => $apId, "secretKey" => $secretKey, "content" => $content, "ecName" => $ecName, "mac" => $mac, "mobiles" => $mobile, "sign" => $sign, "addSerial" => $addSerial, ); $dataJson = json_encode($data, JSON_UNESCAPED_UNICODE); $database64 = base64_encode($dataJson); $rs = $this->http_request($url,$database64,$headers); if ($rs['msgGroup']){ return array("code" => 200,"msg" => "发送成功"); }else{ return array("code" => 500,"msg" => $rs); } } /** * @param $url * @param null $data * @param array $headers * @return bool|string * @author: xuxiangang * @Time: 2022/7/12 17:39 * @Desc:短信请求 */ function http_request($url, $data = null, $headers = array() ){ $curl = curl_init(); if(count($headers) >= 1){ curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); } curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if(!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); if(curl_errno($curl)) { print curl_error($curl); } $output = curl_exec($curl); curl_close($curl); return $output; } } ?>
调用
public function message(){ $phone = array("15039946305","17596185621"); $con = "技术测试,请忽略!!!"; Vendor('SmsHelper.SendApi'); //这是TP3 调用方法 # 调用 短信接口 $SMS = new SendApi(); $demo = $SMS->sendMessage($phone,$con); }
注:公共方法 tp3路径\ThinkPHP\Extend\Vendor\SmsHelper\SendApi.php(省得以后忘记!)