本文實例講述了php版阿裡大於(阿裡大魚)短信發送實現方法。分享給大家供大家參考,具體如下:
通用函數
// 發送大於短信 更牛逼的 protected function sendDayuSmsPlus($tel,$type,$data) { $dayu_template = 'dayu_template_'.$type; $signname = C($dayu_template.".signname"); $templatecode = C($dayu_template.".templatecode"); // require LIB_PATH . 'ORG/Taobao-sdk-php/TopSdk.php'; include_once LIB_PATH . 'ORG/Taobao-sdk-php/TopSdk.php'; $c = new TopClient; $c->appkey = C('dayu_appkey'); $c->secretKey = C('dayu_secretKey'); $req = new AlibabaAliqinFcSmsNumSendRequest; $req->setSmsType("normal"); $req->setSmsFreeSignName("{$signname}"); if ($type == 'sold') { $req->setSmsParam('{"name":"'. $data['name'] .'"}'); } if ($type == 'buysuccess') { $req->setSmsParam('{"name":"'. $data['name'] .'","product":"'. $data['product'] .'"}'); } if ($type == 'newagent') { $req->setSmsParam('{"name":"'. $data['name'] .'"}'); } $req->setRecNum("{$tel}"); $req->setSmsTemplateCode("{$templatecode}"); $resp = $c->execute($req); return $resp; }
優化
// 發送大於短信 更牛逼的 protected function sendDayuSmsPlus($tel,$type,$data) { $dayu_template = 'dayu_template_'.$type; $signname = C($dayu_template.".signname"); $templatecode = C($dayu_template.".templatecode"); // require LIB_PATH . 'ORG/Taobao-sdk-php/TopSdk.php'; include_once LIB_PATH . 'ORG/Taobao-sdk-php/TopSdk.php'; $c = new TopClient; $c->appkey = C('dayu_appkey'); $c->secretKey = C('dayu_secretKey'); $req = new AlibabaAliqinFcSmsNumSendRequest; $req->setSmsType("normal"); $req->setSmsFreeSignName("{$signname}"); switch($type) { case 'sold': $req->setSmsParam('{"name":"'. $data['name'] .'"}'); break; case 'buysuccess': $req->setSmsParam('{"name":"'. $data['name'] .'","product":"'. $data['product'] .'"}'); break; case 'newagent': $req->setSmsParam('{"name":"'. $data['name'] .'"}'); break; default: $req->setSmsParam('{"code":"'. $data['code'] .'","product":"'. $data['product'] .'"}'); } $req->setRecNum("{$tel}"); $req->setSmsTemplateCode("{$templatecode}"); $resp = $c->execute($req); return $resp; }
這裡的require 和 include_once 還是有區別的。如果用require,重復調用方法,就會報錯。Fatal error: Cannot redeclare class。改成include_once 就可以了。
配置模板
<?php return array ( // 阿裡大魚短信配置 'dayu_appkey'=>'xxxxxx', 'dayu_secretKey'=>'xxxxxxxxxxxxxxxxxxxxx', 'dayu_template_register' => array('signname'=>'注冊驗證','templatecode'=>'SMS_9655457'), 'dayu_template_alteration' => array('signname'=>'變更驗證','templatecode'=>'SMS_9655454'), 'dayu_template_identity' => array('signname'=>'身份驗證','templatecode'=>'SMS_9655461'), 'dayu_template_sold'=> array('signname'=>'點多多','templatecode'=>'SMS_12800188'), 'dayu_template_buysuccess'=> array('signname'=>'點多多','templatecode'=>'SMS_12775103'), 'dayu_template_newagent'=> array('signname'=>'點多多','templatecode'=>'SMS_12815193'), );
關於簽名簽名會顯示在短信中【點多多】,只要是允許的簽名,系統的或者自己審核通過的,可以混用。
原生類
<?php /** * TOP API: alibaba.aliqin.fc.sms.num.send request * * @author auto create * @since 1.0, 2015.12.02 */ class AlibabaAliqinFcSmsNumSendRequest { /** * 公共回傳參數,在“消息返回”中會透傳回該參數;舉例:用戶可以傳入自己下級的會員ID,在消息返回時,該會員ID會包含在內,用戶可以根據該會員ID識別是哪位會員使用了你的應用 **/ private $extend; /** * 短信接收號碼。支持單個或多個手機號碼,傳入號碼為11位手機號碼,不能加0或+86。群發短信需傳入多個號碼,以英文逗號分隔,一次調用最多傳入200個號碼。示例:18600000000,13911111111,13322222222 **/ private $recNum; /** * 短信簽名,傳入的短信簽名必須是在阿裡大魚“管理中心-短信簽名管理”中的可用簽名。如“阿裡大魚”已在短信簽名管理中通過審核,則可傳入”阿裡大魚“(傳參時去掉引號)作為短信簽名。短信效果示例:【阿裡大魚】歡迎使用阿裡大魚服務。 **/ private $smsFreeSignName; /** * 短信模板變量,傳參規則{"key":"value"},key的名字須和申請模板中的變量名一致,多個變量之間以逗號隔開。示例:針對模板“驗證碼$[code],您正在進行${product}身份驗證,打死不要告訴別人哦!”,傳參時需傳入{"code":"1234","product":"alidayu"} **/ private $smsParam; /** * 短信模板ID,傳入的模板必須是在阿裡大魚“管理中心-短信模板管理”中的可用模板。示例:SMS_585014 **/ private $smsTemplateCode; /** * 短信類型,傳入值請填寫normal **/ private $smsType; private $apiParas = array(); public function setExtend($extend) { $this->extend = $extend; $this->apiParas["extend"] = $extend; } public function getExtend() { return $this->extend; } public function setRecNum($recNum) { $this->recNum = $recNum; $this->apiParas["rec_num"] = $recNum; } public function getRecNum() { return $this->recNum; } public function setSmsFreeSignName($smsFreeSignName) { $this->smsFreeSignName = $smsFreeSignName; $this->apiParas["sms_free_sign_name"] = $smsFreeSignName; } public function getSmsFreeSignName() { return $this->smsFreeSignName; } public function setSmsParam($smsParam) { $this->smsParam = $smsParam; $this->apiParas["sms_param"] = $smsParam; } public function getSmsParam() { return $this->smsParam; } public function setSmsTemplateCode($smsTemplateCode) { $this->smsTemplateCode = $smsTemplateCode; $this->apiParas["sms_template_code"] = $smsTemplateCode; } public function getSmsTemplateCode() { return $this->smsTemplateCode; } public function setSmsType($smsType) { $this->smsType = $smsType; $this->apiParas["sms_type"] = $smsType; } public function getSmsType() { return $this->smsType; } public function getApiMethodName() { return "alibaba.aliqin.fc.sms.num.send"; } public function getApiParas() { return $this->apiParas; } public function check() { RequestCheckUtil::checkNotNull($this->recNum,"recNum"); RequestCheckUtil::checkNotNull($this->smsFreeSignName,"smsFreeSignName"); RequestCheckUtil::checkNotNull($this->smsTemplateCode,"smsTemplateCode"); RequestCheckUtil::checkNotNull($this->smsType,"smsType"); } public function putOtherTextParam($key, $value) { $this->apiParas[$key] = $value; $this->$key = $value; } }
更多關於PHP相關內容感興趣的讀者可查看本站專題:《php面向對象程序設計入門教程》、《PHP微信開發技巧匯總》、《PHP編碼與轉碼操作技巧匯總》、《PHP網絡編程技巧總結》、《PHP基本語法入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。