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

阿里云PHP SMS短信服务验证码发送方法详解,第1张

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

@H_404_8@开通SMS服务

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

@H_404_8@相关学习推荐:php编程(视频)

@H_404_8@创建签名、模板

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

@H_404_8@创建签名

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

@H_404_8@记住签名名称

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

@H_404_8@创建模板

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

@H_404_8@查看并记住模板CODE

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

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

@H_404_8@创建并记住KeyID和KeySecret

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

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

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

@H_404_8@密匙:pult

@H_404_8@创建PHP-SMS项目

@H_404_8@创建代码文件

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

@H_404_8@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);//返回结果
@H_404_8@移入Requset

@H_404_8@还是在下载的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;  }}
@H_404_8@完成

@H_404_8@运行试试吧


@H_404_8@相关学习推荐:编程视频

总结

以上是内存溢出为你收集整理的阿里云PHP SMS短信服务验证码发送方法详解全部内容,希望文章能够帮你解决阿里云PHP SMS短信服务验证码发送方法详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/1159904.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存