123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- /**
- * 短信服务
- */
- namespace Common\Server;
- require_once 'Application/Common/Common/dysms_sdk/vendor/autoload.php';
- use Aliyun\Core\Config;
- use Aliyun\Core\Profile\DefaultProfile;
- use Aliyun\Core\DefaultAcsClient;
- use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;
- use Aliyun\Api\Sms\Request\V20170525\SendBatchSmsRequest;
- use Aliyun\Api\Sms\Request\V20170525\QuerySendDetailsRequest;
- // 加载区域结点配置
- Config::load();
- //use Sms\Request\V20160927 as Sms;
- class SmsServer {
- static $acsClient = null;
- /**
- * 取得AcsClient
- *
- * @return DefaultAcsClient
- */
- public function getAcsClient() {
- //产品名称:云通信流量服务API产品,开发者无需替换
- $product = "Dysmsapi";
- //产品域名,开发者无需替换
- $domain = "dysmsapi.aliyuncs.com";
- // TODO 此处需要替换成开发者自己的AK (https://ak-console.aliyun.com/)
- $accessKeyId = ACCESS_KEY_ID; // AccessKeyId
- $accessKeySecret = ACCESS_KEY_SECRET; // AccessKeySecret
- // 暂时不支持多Region
- $region = "cn-hangzhou";
- // 服务结点
- $endPointName = "cn-hangzhou";
- if(static::$acsClient == null) {
- //初始化acsClient,暂不支持region化
- $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
- // 增加服务结点
- DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);
- // 初始化AcsClient用于发起请求
- static::$acsClient = new DefaultAcsClient($profile);
- }
- return static::$acsClient;
- }
- /**
- * 发送短信
- * @return stdClass
- */
- public function sendSms($tel,$TemplateCode,$param_arr) {
- // 初始化SendSmsRequest实例用于设置发送短信的参数
- $request = new SendSmsRequest();
- //可选-启用https协议
- //$request->setProtocol("https");
- // 必填,设置短信接收号码
- $request->setPhoneNumbers($tel);
- // 必填,设置签名名称,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
- $request->setSignName(SIGN_NAME);
- // 必填,设置模板CODE,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
- $request->setTemplateCode($TemplateCode);
- // 可选,设置模板参数, 假如模板中存在变量需要替换则为必填项
- $request->setTemplateParam($param_arr);
- //$request->setTemplateParam(json_encode($param_arr, JSON_UNESCAPED_UNICODE));
- // 可选,设置流水号
- //$request->setOutId("yourOutId");
- // 选填,上行短信扩展码(扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段)
- //$request->setSmsUpExtendCode("1234567");
- try {
- // 发起访问请求
- $response = $this->getAcsClient()->getAcsResponse($request);
- cplog('发送结果:'.var_export($response,true),'INFO');
- cplog('[发送成功] 手机号:'.$tel,'INFO');
- if ($response->Message == 'OK') {
- $res = array(
- 'status' => true,
- 'RequestId' => $response->RequestId,
- 'response' => $response
- );
- return $res;
- } else {
- $res = array(
- 'status' => false,
- 'code' => $response->Code,
- 'message' => $response->Message,
- );
- return $res;
- }
- }
- catch (\ClientException $e) {
- cplog('[客户错误码]:'.var_export($e->getErrorCode(),true));
- cplog('[客户错误信息]:'.var_export($e->getErrorMessage(),true));
- $res = array(
- 'status' => false,
- 'code' => $e->getErrorCode(),
- 'message' => $e->getErrorMessage()
- );
- return $res;
- }
- catch (\ServerException $e) {
- cplog('[服务错误码]:'.var_export($e->getErrorCode(),true));
- cplog('[服务错误信息]:'.var_export($e->getErrorMessage(),true));
- $res = array(
- 'status' => false,
- 'code' => $e->getErrorCode(),
- 'message' => $e->getErrorMessage()
- );
- return $res;
- }
- }
- }
|