当前位置: 首页 > 后端技术 > PHP

向党伸出援手,thinkphp,新的阿里大鱼短信发送,sdk这么多东西,烦不烦?

时间:2023-03-30 01:18:57 PHP

新版sdk下载下来了,集成了很多东西,自己看很烦,不多说了,我在源码上写了两个类AliSms.class.phpclassAliSms{//网上的地址constAPI_DOAMIN='http://dysmsapi.aliyuncs.com/';protected$env;protected$accesskey;protected$secretKey;protectedstatic$method='POST';protectedstatic$header=array('x-sdk-client'=>'php/2.0.0',);//30secondpublicstatic$connectTimeout=30;//80secondpublicstatic$readTimeout=80;//公共参数受保护$requestParams=array();公共函数__construct($accessKey,$secretKey){$this->accesskey=$accessKey;$this->secretKey=$secretKey;$this->requestParams["AccessKeyId"]=$this->accesskey;$this->requestParams["RegionId"]='cn-杭州';$this->requestParams["格式"]='JSON';$this->requestParams["SignatureMethod"]='HMAC-SHA1';$this->requestParams["SignatureVersion"]='1.0';$this->requestParams["SignatureNonce"]=uniqid();date_default_timezone_set("GMT");$this->requestParams["Timestamp"]=date('Y-m-d\TH:i:s\Z');$this->requestParams["Action"]='SendSms';$this->requestParams["Version"]='2017-05-25';}/***发送短信*@param$phoneNumbers电话号码*@param$signName短信签名*@param$templateCode短信模板代码*@param$tempalteParamSMS模板参数*@returnHttpResponse*@throws\Think\Exception*/publicfunctionexecute($phoneNumbers,$signName,$templateCode,$tempalteParam){$params=array('PhoneNumbers'=>$phoneNumbers,'SignName'=>$signName,'TemplateCode'=>$templateCode,'TemplateParam'=>$tempalteParam,);if(empty($params['PhoneNumbers'])){thrownewException('缺少参数PhoneNumbers');}if(empty($params['SignName'])){thrownewException('缺少参数SignName');}if(empty($params['TemplateCode'])){thrownewException('缺少参数TemplateCode');}if(empty($params['TemplateParam'])){thrownewException('缺少参数TemplateParam');}foreach($paramsas$key=>$value){$apiParams[$key]=$this->prepareValue($value);}$params=array_merge($params,$this->requestParams);$params["Signature"]=$this->computeSignature($params,$this->secretKey);$response=$this->curl(self::API_DOAMIN,self::$method,$params,self::$header);return$response;}//计算签名publicfunctioncomputeSignature($parameters,$accessKeySecret){ksort($parameters);$canonicalizedQueryString='';foreach($parametersas$key=>$value){$canonicalizedQueryString.='&'.$this->percentEncode($key)。'='。$this->percentEncode($value);$stringToSign=self::$method.'&%2F&'。$this->percentencode(substr($canonicalizedQueryString,1));$signature=base64_encode(hash_hmac('sha1',$stringToSign,$accessKeySecret."&",true));返回$signature;}私有函数prepareValue($value){如果(is_bool($value)){如果($value){返回“真”;}else{返回“假”;}}else{返回$值;}}publicfunctionpercentEncode($str){$res=urlencode($str);$res=preg_replace('/\+/','%20',$res);$res=preg_replace('/\*/','%2A',$res);$res=preg_replace('/%7E/','~',$res);return$res;}/***网络请求*@param$url*@paramstring$httpMethod*@paramnull$postFields*@paramnull$headers*@returnmixed*@throws\Think\Exception*/publicfunctioncurl($url,$httpMethod="GET",$postFields=null,$headers=null){$ch=curl_init();curl_setopt($ch,CURLOPT_CUSTOMREQUEST,$httpMethod);curl_setopt($ch,CURLOPT_URL,$url);curl_setopt($ch,CURLOPT_FAILONERROR,false);curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);curl_setopt($ch,CURLOPT_POSTFIELDS,is_array($postFields)?self::getPostHttpBody($postFields):$postFields);如果(自我::$readTimeout){curl_setopt($ch,CURLOPT_TIMEOUT,self::$readTimeout);}if(self::$connectTimeout){curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,self::$connectTimeout);}//https请求if(strlen($url)>5&&strtolower(substr($url,0,5))=="https"){curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);}if(is_array($headers)&&0$apiParamValue){$content.="$apiParamKey=".urlencode($apiParamValue)."&";}returnsubstr($content,0,-1);}publicstaticfunctiongetHttpHeaders($headers){$httpHeader=array();foreach($headersas$key=>$value){array_push($httpHeader,$key.":".$value);}return$httpHeader;}}下面这个类只是我不想调用Sender.class.phpclass写的newAliSmsSender{private$provider;privatestatic$_instance=null;privatefunction__construct(){$this->provider=newAliSms(C('ALIYUN_AK'),C('ALIYUN_SK'));}//单例publicstaticfunctiongetInstance(){if(!self::$_instance){self::$_instance=newSender();}returnself::$_instance;}/***发送短信验证码*@param$mobile电话号码*@param$data验证码数组('code'=>'123456')*@returnbool*/publicfunctionsendCode($mobile,$data){if(!$data['code'])returnfalse;$tempalteParam=json_encode($data);$result=$this->provider->execute($mobile,'SMSsignature','SMStemplatenumber',$tempalteParam);return$th是->parseResult($result,$mobile);}/***处理短信发送返回结果*@param$result*@paramstring$mobile*@returnbool*/privatefunctionparseResult($result,$mobile=''){$result=json_decode($result,1);if($result['Code']=='OK'&&$result['Message']=='OK'){//发送成功$this->onSendSuccess($result,$mobile);返回真;}else{//失败$this->onSendFail($result,$mobile);返回假;}}/***发送成功后*@paramarray$data*@paramstring$mobile*/privatefunctiononSendSuccess(array$data=null,$mobile=''){//todo}/***发送后failed*@paramarray$data*@paramstring$mobile*/privatefunctiononSendFail(array$data=null,$mobile=''){//日志$logPath=RUNTIME_PATH.'sms_'.date('y_m_d')。'。日志';\Think\Log::write('手机号:'.$mobile.'大鱼返回结果:'.serialize($data),'INFO','',$logPath);}使用方法:$result=发件人::getInstance()->sendCode($mobile,array('code'=>$randCode));