1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- require_once 'api_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();
- /**
- * Class Aliyunsms
- *
- * 阿里云短信发送
- */
- class Aliyunsms
- {
- private $CI;
- private $setting;
-
- static $acsClient = null;
- function __construct()
- {
- $this->CI = & get_instance();
- $this->CI->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
- $this->setting = $this->CI->cache->get('setting');
- }
- /**
- * 取得AcsClient
- *
- * @return DefaultAcsClient
- */
- private function getAcsClient() {
- //产品名称:云通信流量服务API产品,开发者无需替换
- $product = "Dysmsapi";
- //产品域名,开发者无需替换
- $domain = "dysmsapi.aliyuncs.com";
- $accessKeyId = $this->setting['access_key_id']; // AccessKeyId
- $accessKeySecret = $this->setting['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($phone,$template_code,$content) {
- if(is_array($content)){
- // 初始化SendSmsRequest实例用于设置发送短信的参数
- $request = new SendSmsRequest();
- //可选-启用https协议
- //$request->setProtocol("https");
- // 必填,设置短信接收号码
- $request->setPhoneNumbers($phone);
- // 必填,设置签名名称,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
- $request->setSignName($this->setting['sign_name']);
- // 必填,设置模板CODE,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
- $request->setTemplateCode($template_code);
- // 可选,设置模板参数, 假如模板中存在变量需要替换则为必填项
- $request->setTemplateParam(json_encode($content), JSON_UNESCAPED_UNICODE);
- // 发起访问请求
- try {
- $acsResponse = $this->getAcsClient()->getAcsResponse($request);
- return (array)$acsResponse;
- }catch (Exception $e) {
- return array("Code"=>"FAILED" , "Message"=>$e->getErrorMessage());
- }
- }else{
- return array("Code"=>"FAILED","Message"=>"参数错误");
- }
- }
- }
|