当前位置:Gxlcms > php框架 > 阿里云PHP SMS短信服务验证码发送方法

阿里云PHP SMS短信服务验证码发送方法

时间:2021-07-01 10:21:17 帮助过:69人阅读

开通SMS服务

首先去这个网站开通阿里云的SMS短信服务:https://www.aliyun.com/product/sms?spm=5176.8142029.388261.295.vU5T5g

创建签名、模板

要使用短信服务器需要先创建签名和模板,并提交给阿里云审核通过才可以正常使用短信服务。

这里写图片描述

创建签名

创建签名的时候注意一下签名名称,其他的话就不累赘了。

这里写图片描述

记住签名名称

现在请记住你创建的签名名称,一会在代码中需要使用。

创建模板

创建模板也很简单,阿里云已经把要如何填写写的很清楚了。

这里写图片描述

查看并记住模板CODE

返回你的控制台,当你的模板审核通过时这就会出现大于0的数。

点击这个数,会进入模板管理面板就能看到你的模板CODE了,请记住他。

这里写图片描述

这里写图片描述

创建并记住KeyId和KeySecret

到控制台,把鼠标放到右上角你的用户名的位置会出现一个accessKeySecret点进去就可以创建KeyId和KeySecret了,如果他提醒你用RAM安全什么的,你看你要不要给你的员工分配权限,如果要的话就用RAM,否则就直接点击继续使用就行了。

这里写图片描述

这里写图片描述

下载阿里云短信服务器PHP-SDK

官方下载地址:https://help.aliyun.com/document_detail/55359.html?spm=5176.8195934.507901.12.b1ngGK
本教程使用SDK下载地址:http://pan.baidu.com/s/1bpF5B8z

密匙:pult

这里写图片描述

创建PHP-SMS项目

创建代码文件

创建你的代码文件,并把这个文件放在刚才下载的SDK文件夹中的api_sdk的aliyun-php-sdk-core目录下,并把一下代码写入代码文件。

aliyun-php-sdk-core目录里包含了SMS短信服务的各种模块,所以必须得放在这里面才能使用服务

  1. <?php
  2. include 'Config.php';
  3. include_once 'Request/V20170525/SendSmsRequest.php';
  4. include_once 'Request/V20170525/QuerySendDetailsRequest.php';
  5. $accessKeyId = "LTAIvAaNs61JeBiN";
  6. //阿里云KeyId
  7. $accessKeySecret = "Y3H7durYJ6GIqmJJrsdbJwPi6E8O8M";
  8. //阿里云KeySecret
  9. //短信API产品名
  10. $product = "Dysmsapi";
  11. //照写就行了
  12. //短信API产品域名
  13. $domain = "dysmsapi.aliyuncs.com";
  14. //照着写就行了
  15. //暂时不支持多Region
  16. $region = "cn-hangzhou";
  17. //照着写就行了
  18. //初始化访问的acsCleint
  19. $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
  20. DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", $product, $domain);
  21. $acsClient= new DefaultAcsClient($profile);
  22. $request = new SendSmsRequest;
  23. //必填-短信接收号码。支持以逗号分隔的形式进行批量调用,批量上限为20个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
  24. $request->setPhoneNumbers("123456789");
  25. //这里填你要发送的电话号码
  26. //必填-短信签名
  27. $request->setSignName("xx项目");
  28. //这里就是刚才让你记住的项目签名
  29. //必填-短信模板Code
  30. $request->setTemplateCode("SMS_123456");
  31. //这里就是模板CODE
  32. //选填-假如模板中存在变量需要替换则为必填(JSON格式)
  33. $request->setTemplateParam("{\"name\":\"郭涛\",\"number\":\"316\"}");
  34. //选填-发送短信流水号
  35. $request->setOutId("1234");//照填就行了
  36. //发起访问请求
  37. $acsResponse = $acsClient->getAcsResponse($request);
  38. var_dump($acsResponse);//返回结果

移入Requset

还是在下载的SDK文件夹中的api_sdk目录下,有一个交Dysmsapi的文件夹,打开这个文件夹就会看到一个叫Request的文件夹,把这个Reques。的件夹复制粘贴到aliyun-php-sdk-core里面。说实在的我搞不清阿里云这个为什么要这样分开装SDK,可能是我使用的姿势不对吧,如果有大神搞得清,还劳烦赐教小弟,好人一生平安。
移入后,打开Request\V20170525目录里有一个SendSmsRequest.php的源文件。请屏蔽第一行的空间命名。 也就是这一行namespace Dysmsapi\Reqest\V20170525;最后效果如下

  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. //namespace Dysmsapi\Request\V20170525;//就是屏蔽这一行代码!!!!
  21. class SendSmsRequest extends \RpcAcsRequest
  22. {
  23. function __construct()
  24. {
  25. parent::__construct("Dysmsapi", "2017-05-25", "SendSms");
  26. }
  27. private $outId;
  28. private $signName;
  29. private $ownerId;
  30. private $resourceOwnerId;
  31. private $templateCode;
  32. private $phoneNumbers;
  33. private $resourceOwnerAccount;
  34. private $templateParam;
  35. public function getOutId() {
  36. return $this->outId;
  37. }
  38. public function setOutId($outId) {
  39. $this->outId = $outId;
  40. $this->queryParameters["OutId"]=$outId;
  41. }
  42. public function getSignName() {
  43. return $this->signName;
  44. }
  45. public function setSignName($signName) {
  46. $this->signName = $signName;
  47. $this->queryParameters["SignName"]=$signName;
  48. }
  49. public function getOwnerId() {
  50. return $this->ownerId;
  51. }
  52. public function setOwnerId($ownerId) {
  53. $this->ownerId = $ownerId;
  54. $this->queryParameters["OwnerId"]=$ownerId;
  55. }
  56. public function getResourceOwnerId() {
  57. return $this->resourceOwnerId;
  58. }
  59. public function setResourceOwnerId($resourceOwnerId) {
  60. $this->resourceOwnerId = $resourceOwnerId;
  61. $this->queryParameters["ResourceOwnerId"]=$resourceOwnerId;
  62. }
  63. public function getTemplateCode() {
  64. return $this->templateCode;
  65. }
  66. public function setTemplateCode($templateCode) {
  67. $this->templateCode = $templateCode;
  68. $this->queryParameters["TemplateCode"]=$templateCode;
  69. }
  70. public function getPhoneNumbers() {
  71. return $this->phoneNumbers;
  72. }
  73. public function setPhoneNumbers($phoneNumbers) {
  74. $this->phoneNumbers = $phoneNumbers;
  75. $this->queryParameters["PhoneNumbers"]=$phoneNumbers;
  76. }
  77. public function getResourceOwnerAccount() {
  78. return $this->resourceOwnerAccount;
  79. }
  80. public function setResourceOwnerAccount($resourceOwnerAccount) {
  81. $this->resourceOwnerAccount = $resourceOwnerAccount;
  82. $this->queryParameters["ResourceOwnerAccount"]=$resourceOwnerAccount;
  83. }
  84. public function getTemplateParam() {
  85. return $this->templateParam;
  86. }
  87. public function setTemplateParam($templateParam) {
  88. $this->templateParam = $templateParam;
  89. $this->queryParameters["TemplateParam"]=$templateParam;
  90. }
  91. }

完成

运行试试吧

这里写图片描述 
这里写图片描述

以上所述是小编给大家介绍的阿里云PHP SMS短信服务验证码发送方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

人气教程排行