本文執行個體講述了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 就可以了。
配置模板
<?phpreturn 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版阿里大於(阿里大魚)簡訊發送執行個體詳解 的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!