开通SMS服务
首先访问阿里云短信服务官网进行开通操作:https://www.aliyun.com/product/sms?spm=5176.8142029.388261.295.vU5T5g
创建签名、模板
使用短信服务前,必须先在控制台创建签名和短信模板,并提交至阿里云审核,审核通过后才能正常调用接口。

创建签名
创建签名时需注意填写规范的签名名称,其他配置项相对简单,此处不再赘述。

记住签名名称
请务必牢记创建的签名名称,后续编写代码时将作为参数使用。
创建模板
模板创建过程较为直观,阿里云控制台已提供详细的填写指引。

查看并记住模板CODE
返回控制台,当模板状态显示审核通过且数值大于0时,表示可用。
点击该数值进入模板管理面板,即可查看并复制唯一的模板CODE(TemplateCode),请妥善保存。


创建并记住KeyId和KeySecret
登录控制台,将鼠标悬停于右上角用户名处,选择AccessKey管理入口以创建AccessKeyId和AccessKeySecret。若系统提示使用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项目
创建代码文件
新建PHP代码文件,将其放置于已下载的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 子文件夹。将该 Request 文件夹整体复制并粘贴到 aliyun-php-sdk-core 目录下。关于阿里云为何采用此种分离式SDK结构,目前尚不明确,可能是使用方式差异所致,若有高手知晓原因,恳请指点。移入完成后,打开 Request\V20260525 目录,找到 SendSmsRequest.php 源文件,需注释掉第一行的命名空间声明,即屏蔽行 namespace Dysmsapi\Reqest\V20260525;,修改后的效果如下所示。
<?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短信服务验证码发送方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对本站网站的支持!