登录  /  注册

阿里云PHP SMS短信服务验证码如何发送

小云云
发布: 2018-02-08 09:18:53
原创
2085人浏览过

本文主要和大家分享阿里云php sms短信服务验证码如何发送,希望能帮助到大家。

开通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短信服务的各种模块,所以必须得放在这里面才能使用服务

<?php   include &#39;Config.php&#39;;
  include_once &#39;Request/V20170525/SendSmsRequest.php&#39;;
  include_once &#39;Request/V20170525/QuerySendDetailsRequest.php&#39;;
  $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-&gt;setSignName("xx项目");
//这里就是刚才让你记住的项目签名
  //必填-短信模板Code
  $request-&gt;setTemplateCode("SMS_123456");
//这里就是模板CODE
  //选填-假如模板中存在变量需要替换则为必填(JSON格式)
  $request-&gt;setTemplateParam("{\"name\":\"郭涛\",\"number\":\"316\"}");
  //选填-发送短信流水号
  $request-&gt;setOutId("1234");//照填就行了
  //发起访问请求
  $acsResponse = $acsClient-&gt;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-&gt;outId = $outId;
    $this-&gt;queryParameters["OutId"]=$outId;
  }
  public function getSignName() {
    return $this-&gt;signName;
  }
  public function setSignName($signName) {
    $this-&gt;signName = $signName;
    $this-&gt;queryParameters["SignName"]=$signName;
  }
  public function getOwnerId() {
    return $this-&gt;ownerId;
  }
  public function setOwnerId($ownerId) {
    $this-&gt;ownerId = $ownerId;
    $this-&gt;queryParameters["OwnerId"]=$ownerId;
  }
  public function getResourceOwnerId() {
    return $this-&gt;resourceOwnerId;
  }
  public function setResourceOwnerId($resourceOwnerId) {
    $this-&gt;resourceOwnerId = $resourceOwnerId;
    $this-&gt;queryParameters["ResourceOwnerId"]=$resourceOwnerId;
  }
  public function getTemplateCode() {
    return $this-&gt;templateCode;
  }
  public function setTemplateCode($templateCode) {
    $this-&gt;templateCode = $templateCode;
    $this-&gt;queryParameters["TemplateCode"]=$templateCode;
  }
  public function getPhoneNumbers() {
    return $this-&gt;phoneNumbers;
  }
  public function setPhoneNumbers($phoneNumbers) {
    $this-&gt;phoneNumbers = $phoneNumbers;
    $this-&gt;queryParameters["PhoneNumbers"]=$phoneNumbers;
  }
  public function getResourceOwnerAccount() {
    return $this-&gt;resourceOwnerAccount;
  }
  public function setResourceOwnerAccount($resourceOwnerAccount) {
    $this-&gt;resourceOwnerAccount = $resourceOwnerAccount;
    $this-&gt;queryParameters["ResourceOwnerAccount"]=$resourceOwnerAccount;
  }
  public function getTemplateParam() {
    return $this-&gt;templateParam;
  }
  public function setTemplateParam($templateParam) {
    $this-&gt;templateParam = $templateParam;
    $this-&gt;queryParameters["TemplateParam"]=$templateParam;
  }
}
登录后复制

完成

运行试试吧

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

相关推荐:

怎么用php实现短信验证码发送

JS实现短信验证码

JS实现用户注册时获取短信验证码和倒计时功能的示例代码分享

以上就是阿里云PHP SMS短信服务验证码如何发送的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号