我在做毕设,项目中想用短信验证功能,就找到了阿里大鱼,价格貌似目前最低,其他什么都好弄,就是导入到项目中,不知道怎么用,一直报错,网上查了,大都是和文档案例一样,没用,tp里还有专栏范例,估计都是旧版本,也没用。最后搞出来了,特在此记录一下。
下载SDK,解压引入Autoload.php、TopSdk.php及aliyun和top两个文件夹。
我引入的位置是 APP_PATH/Common/Lib/Alidayu/,不动tp文件我觉得好些。
新建一个类文件 SendMSM.class.php,这个根据自己需求来。
<?php namespace Lib\Alidayu; include('TopSdk.php'); //就是下面两句把我搞惨了,别嫌弃,我只是入门了而已 use TopClient; use AlibabaAliqinFcSmsNumSendRequest; class SendMSM { public function send($recNum='', $smsParam='', $smsTemplateCode='SMS_8525079', $smsFreeSignName='短信测试'){ $c = new TopClient; $c->format = "json"; $c->appkey = C('AlidayuAppKey'); $c->secretKey = C('AlidayuAppSecret'); $req = new AlibabaAliqinFcSmsNumSendRequest; //$req->setExtend("123456"); $req->setSmsType("normal"); $req->setSmsFreeSignName($smsFreeSignName); $req->setSmsParam($smsParam); $req->setRecNum($recNum); $req->setSmsTemplateCode($smsTemplateCode); $resp = $c->execute($req); return $resp; } }
我把AppKey 和 secret 放到了单独的配置文件,以便后期重置了可以在后台修改。
然后就是使用了,
<?php namespace Admin\Controller; use Think\Controller; use Lib\Alidayu\SendMSM; class IndexController extends Controller { public function index(){ $this->sendMSM(); } protected function sendMSM(){ $alidayu = new SendMSM(); $result = $alidayu->send('13000000000','{"code":"123456"}'); dump($result); } }
测试结果:
object(stdClass)[11] public 'result' => object(stdClass)[12] public 'err_code' => string '0' (length=1) public 'model' => string '101550887105^1102084861016' (length=26) public 'success' => boolean true public 'request_id' => string '12cl9hupqvkgh' (length=13)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft