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


Posted in PHP onJuly 11, 2017

开通SMS服务

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

创建签名、模板

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

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

创建签名

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

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

记住签名名称

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

创建模板

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

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

查看并记住模板CODE

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

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

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

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

创建并记住KeyId和KeySecret

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

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

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

下载阿里云短信服务器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短信服务验证码发送方法

创建PHP-SMS项目

创建代码文件

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

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

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

移入Requset

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

<?php
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
//namespace Dysmsapi\Request\V20170525;//就是屏蔽这一行代码!!!!
class SendSmsRequest extends \RpcAcsRequest
{
  function __construct()
  {
    parent::__construct("Dysmsapi", "2017-05-25", "SendSms");
  }
  private $outId;
  private $signName;
  private $ownerId;
  private $resourceOwnerId;
  private $templateCode;
  private $phoneNumbers;
  private $resourceOwnerAccount;
  private $templateParam;
  public function getOutId() {
    return $this->outId;
  }
  public function setOutId($outId) {
    $this->outId = $outId;
    $this->queryParameters["OutId"]=$outId;
  }
  public function getSignName() {
    return $this->signName;
  }
  public function setSignName($signName) {
    $this->signName = $signName;
    $this->queryParameters["SignName"]=$signName;
  }
  public function getOwnerId() {
    return $this->ownerId;
  }
  public function setOwnerId($ownerId) {
    $this->ownerId = $ownerId;
    $this->queryParameters["OwnerId"]=$ownerId;
  }
  public function getResourceOwnerId() {
    return $this->resourceOwnerId;
  }
  public function setResourceOwnerId($resourceOwnerId) {
    $this->resourceOwnerId = $resourceOwnerId;
    $this->queryParameters["ResourceOwnerId"]=$resourceOwnerId;
  }
  public function getTemplateCode() {
    return $this->templateCode;
  }
  public function setTemplateCode($templateCode) {
    $this->templateCode = $templateCode;
    $this->queryParameters["TemplateCode"]=$templateCode;
  }
  public function getPhoneNumbers() {
    return $this->phoneNumbers;
  }
  public function setPhoneNumbers($phoneNumbers) {
    $this->phoneNumbers = $phoneNumbers;
    $this->queryParameters["PhoneNumbers"]=$phoneNumbers;
  }
  public function getResourceOwnerAccount() {
    return $this->resourceOwnerAccount;
  }
  public function setResourceOwnerAccount($resourceOwnerAccount) {
    $this->resourceOwnerAccount = $resourceOwnerAccount;
    $this->queryParameters["ResourceOwnerAccount"]=$resourceOwnerAccount;
  }
  public function getTemplateParam() {
    return $this->templateParam;
  }
  public function setTemplateParam($templateParam) {
    $this->templateParam = $templateParam;
    $this->queryParameters["TemplateParam"]=$templateParam;
  }
}

完成

运行试试吧

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

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

PHP 相关文章推荐
第十三节--对象串行化
Nov 16 PHP
?生?D片??C字串
Dec 06 PHP
file_get_contents(&quot;php://input&quot;, &quot;r&quot;)实例介绍
Jul 01 PHP
php自动识别文件编码并转换为UTF-8的方法
Jun 12 PHP
Yii框架上传图片用法总结
Mar 28 PHP
PHP实现的下载远程文件类定义与用法示例
Jul 05 PHP
layui框架实现文件上传及TP3.2.3(thinkPHP)对上传文件进行后台处理操作示例
May 12 PHP
PDO::exec讲解
Jan 28 PHP
详解PHP变量传值赋值和引用赋值变量销毁
Mar 23 PHP
php设计模式之装饰模式应用案例详解
Jun 17 PHP
PHP进阶学习之Geo的地图定位算法详解
Jun 19 PHP
php实现分页功能的详细实例方法
Sep 29 PHP
PHP实现阿里大鱼短信验证的实例代码
Jul 10 #PHP
yii2局部关闭(开启)csrf的验证的实例代码
Jul 10 #PHP
PHP实现登陆并抓取微信列表中最新一组微信消息的方法
Jul 10 #PHP
PHP基于socket实现的简单客户端和服务端通讯功能示例
Jul 10 #PHP
PHP正则匹配操作简单示例【preg_match_all应用】
Jul 10 #PHP
php使用flock阻塞写入文件和非阻塞写入文件的实例讲解
Jul 10 #PHP
form自动提交实例讲解
Jul 10 #PHP
You might like
php+lottery.js实现九宫格抽奖功能
2019/07/21 PHP
php post换行的方法
2020/02/03 PHP
整理8个很棒的 jQuery 倒计时插件和教程
2011/12/12 Javascript
js 获取和设置css3 属性值的实现方法
2013/05/06 Javascript
node.js中的buffer.length方法使用说明
2014/12/14 Javascript
Extjs实现下拉菜单效果
2016/04/01 Javascript
在JS中a标签加入单击事件屏蔽href跳转页面
2016/12/16 Javascript
JavaScript实现随机数生成器(去重)
2017/10/13 Javascript
基于vue2实现上拉加载功能
2017/11/28 Javascript
利用jqprint插件打印页面内容的实现方法
2018/01/09 Javascript
Electron中实现大文件上传和断点续传功能
2018/10/28 Javascript
webpack4 SplitChunks实现代码分隔详解
2019/05/23 Javascript
Layui Table js 模拟选中checkbox的例子
2019/09/03 Javascript
解决Vue + Echarts 使用markLine标线(precision精度问题)
2020/07/20 Javascript
Python实现脚本锁功能(同时只能执行一个脚本)
2017/05/10 Python
Python使用matplotlib填充图形指定区域代码示例
2018/01/16 Python
使用Django启动命令行及执行脚本的方法
2018/05/29 Python
让代码变得更易维护的7个Python库
2018/10/09 Python
Python如何批量生成和调用变量
2020/11/21 Python
使用CSS变量实现炫酷惊人的悬浮效果
2019/04/26 HTML / CSS
Bergfreunde丹麦:登山装备网上零售商
2017/02/26 全球购物
Visual-Click葡萄牙:欧洲领先的在线眼镜商
2020/02/17 全球购物
工作自我评价分享
2013/12/01 职场文书
给水工程专业毕业生自荐信
2014/01/28 职场文书
2014国培学习感言
2014/03/05 职场文书
2014年效能监察工作总结
2014/11/21 职场文书
企业财务人员岗位职责
2015/04/14 职场文书
企业廉洁教育心得体会
2016/01/20 职场文书
教师廉政准则心得体会
2016/01/20 职场文书
护理自荐信
2019/05/14 职场文书
职业规划从高考志愿专业选择开始
2019/08/08 职场文书
掌握一个领域知识,高效学习必备方法
2019/08/08 职场文书
CSS 新特性 contain控制页面的重绘与重排问题
2021/04/30 HTML / CSS
教你使用pyinstaller打包Python教程
2021/05/27 Python
MySQL系列之八 MySQL服务器变量
2021/07/02 MySQL
Windows和Linux上部署Golang并运行程序
2022/04/22 Servers