云片使用模板发送短信
2020年6月11日1.下载云片官方sdk
1 |
composer require yunpian/yunpian-php-sdk |
2.废话不多说直接上代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
use Yunpian\Sdk\YunpianClient; /** * 云片根据模板发送短信 */ function zfYunPianSdkTplSendMessage($iphone,$tplId, $data) { $clnt = YunpianClient::create('你的apikey'); #定义apikey $tpl = $clnt->tpl()->get(['tpl_id' => $tplId])->data()['tpl_content']; #获取模板数据 $zfMoreStrReplace = zfMoreStrReplace($data, $tpl); #将模板的内容根据需求替换 $response = $clnt->sms()->single_send([YunpianClient::MOBILE => $iphone,YunpianClient::TEXT => $zfMoreStrReplace]);#开始发送短信 if($response->isSucc()){ return true; } } /** * 多次替换内容的方法 * @param $data 数组 * @param $text 文本 * @return string|string[] */ function zfMoreStrReplace($data , $text){ $i = 1; $str =''; foreach ($data as $key => $value) { if ($i == 1) { $i++; $str = str_replace("$key", $value, $text); } else { $i++; $str = str_replace("$key", $value, $str); } } return $str; } |
3.如何调用方法
1 |
zfYunPianSdkTplSendMessage('手机号','模板号',['#name#'=>值1,'#date#'=>值2); |