/** * 发送接口 * @param $info 发送信息的某些参数 * @return bool */ public function send($info) { $config = target('tools/ToolsSendConfig')->getConfig('ali');//获取ali短信的配置 key 秘钥之类的 if(empty($config)){ return $this->error('配置不存在!'); } $receive = $info['receive'];//手机号 if($info['user_info']) { $receive = $info['user_info']['tel']; } $AccessKeyId=$config['accessKeyId']; $accessKeySecret=$config['accessKeySecret']; $params["PhoneNumbers"] = $receive; $params["SignName"] = $config['SignName']; $params["TemplateCode"] = $info['param']['TemplateCode'];//选择ali短信配置好的模板 $params['TemplateParam'] = Array ( "code" => $info['param']['code']//需要替换的内容 ); if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) { $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE); } $params=array_merge($params, array( "RegionId" => "cn-hangzhou", "Action" => "SendSms", "Version" => "2017-05-25", )); //配置参数 $apiParams = array_merge(array ( "SignatureMethod" => "HMAC-SHA1", "SignatureNonce" => uniqid(mt_rand(0,0xffff), true), "SignatureVersion" => "1.0", "AccessKeyId" => $AccessKeyId, "Timestamp" => gmdate("Y-m-dTH:i:s"), "Format" => "JSON", ), $params); ksort($apiParams); $sortedQueryStringTmp = ""; foreach ($apiParams as $key => $value) { $sortedQueryStringTmp .= "&" . $this->encode($key) . "=" . $this->encode($value); } $stringToSign = "GET&%2F&" . $this->encode(substr($sortedQueryStringTmp, 1)); $sign = base64_encode(hash_hmac("sha1", $stringToSign, $accessKeySecret . "&",true)); $signature = $this->encode($sign); //拼接url $url = ($security ? 'https' : 'http')."://dysmsapi.aliyuncs.com/?Signature={$signature}{$sortedQueryStringTmp}"; //发送curl请求 $content = $this->fetchContent($url); //$return = json_decode($content, true); if($return['code']<>'OK'){ return $this->error($return['Message']); } return $this->success(); }
还是比较简单,拼接好参数后一步到位。