当前位置:首页 > PHP教程 > PHP常见问题

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

开通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 '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,可能是我使用的姿势不对吧,如果有大神搞得清,还劳烦赐教小弟,好人一生平安。
移入后,打开requestv20170525目录里有一个sendsmsrequest.php的源文件。请屏蔽第一行的空间命名。 也就是这一行namespace dysmsapireqestv20170525;最后效果如下

<?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 dysmsapirequestv20170525;//就是屏蔽这一行代码!!!! 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短信服务验证码发送方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对硕编程网站的支持!



【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐