thinkphp使用阿里大鱼短信接口

时间:2022-09-25 21:10:49

1.首先登陆阿里大鱼注册账号,可获得60元的代金券,然后创建应用,应用的类型为阿里大鱼。创建完后下载sdk,将下载后的sdk导入到ThinkPHP\Org下。

2.创建控制器用来调用短信接口,代码为下:

<code class="hljs php has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;"><?php</span>
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">namespace</span> <span class="hljs-title" style="box-sizing: border-box;">Home</span>\<span class="hljs-title" style="box-sizing: border-box;">Controller</span>;
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">use</span> <span class="hljs-title" style="box-sizing: border-box;">Think</span>\<span class="hljs-title" style="box-sizing: border-box;">Controller</span>;

<span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">class</span> <span class="hljs-title" style="color:#66066;box-sizing: border-box;">DuanXinController</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">extends</span> <span class="hljs-title" style="color:#66066;box-sizing: border-box;">Controller</span> {</span>
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">public</span> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">index</span><span class="hljs-params" style="color:#66066;box-sizing: border-box;">(<span class="hljs-variable" style="color:#66066;box-sizing: border-box;">$value</span>=<span class="hljs-string" style="color:#0880;box-sizing: border-box;">''</span>)</span>
{</span>
<span class="hljs-variable" style="color:#66066;box-sizing: border-box;">$appkey</span> = <span class="hljs-string" style="color:#0880;box-sizing: border-box;">""</span>;<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//你的App key</span>
<span class="hljs-variable" style="color:#66066;box-sizing: border-box;">$secret</span> = <span class="hljs-string" style="color:#0880;box-sizing: border-box;">""</span>;<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//你的App Secret:</span>
import(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">'Org.Taobao.top.TopClient'</span>);
import(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">'Org.Taobao.top.ResultSet'</span>);
import(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">'Org.Taobao.top.RequestCheckUtil'</span>);
import(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">'Org.Taobao.top.TopLogger'</span>);
import(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">'Org.Taobao.top.request.AlibabaAliqinFcSmsNumSendRequest'</span>);
<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//将需要的类引入,并且将文件名改为原文件名.class.php的形式</span>
<span class="hljs-variable" style="color:#66066;box-sizing: border-box;">$c</span> = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> \TopClient;
<span class="hljs-variable" style="color:#66066;box-sizing: border-box;">$c</span>->appkey = <span class="hljs-variable" style="color:#66066;box-sizing: border-box;">$appkey</span>;
<span class="hljs-variable" style="color:#66066;box-sizing: border-box;">$c</span>->secretKey = <span class="hljs-variable" style="color:#66066;box-sizing: border-box;">$secret</span>;
<span class="hljs-variable" style="color:#66066;box-sizing: border-box;">$req</span> = <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">new</span> \AlibabaAliqinFcSmsNumSendRequest;
<span class="hljs-variable" style="color:#66066;box-sizing: border-box;">$req</span>->setExtend(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"123456"</span>);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//确定发给的是哪个用户,参数为用户id</span>
<span class="hljs-variable" style="color:#66066;box-sizing: border-box;">$req</span>->setSmsType(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"normal"</span>);
<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">/*
进入阿里大鱼的管理中心找到短信签名管理,输入已存在签名的名称,这里是身份验证。
*/</span>
<span class="hljs-variable" style="color:#66066;box-sizing: border-box;">$req</span>->setSmsFreeSignName(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"身份验证"</span>);
<span class="hljs-variable" style="color:#66066;box-sizing: border-box;">$req</span>->setSmsParam(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"{'code':'1234','product':'alidayu'}"</span>);
<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//这里设定的是发送的短信内容:验证码${code},您正在进行${product}身份验证,打死不要告诉别人哦!”</span>
<span class="hljs-variable" style="color:#66066;box-sizing: border-box;">$req</span>->setRecNum(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">""</span>);<span class="hljs-comment" style="color:#8800;box-sizing: border-box;">//参数为用户的手机号码</span>
<span class="hljs-variable" style="color:#66066;box-sizing: border-box;">$req</span>->setSmsTemplateCode(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"SMS_2625443"</span>);
<span class="hljs-variable" style="color:#66066;box-sizing: border-box;">$resp</span> = <span class="hljs-variable" style="color:#66066;box-sizing: border-box;">$c</span>->execute(<span class="hljs-variable" style="color:#66066;box-sizing: border-box;">$req</span>);
var_dump(<span class="hljs-variable" style="color:#66066;box-sizing: border-box;">$resp</span>);

}
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right; opacity: 0.0763572;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li></ul>

3.输出如图,此时短信发送成功。 
thinkphp使用阿里大鱼短信接口

4.接口存放目录如图: 
thinkphp使用阿里大鱼短信接口

5.因为阿里大鱼使用沙箱模式是可能报错,所以建议使用正式模式,即将下载SDK解压后top里的TopClient.PHP第8行$gatewayUrl的值改为:http://gw.api.taobao.com/router/rest