切换风格

默认Lavender Sky Flowers Wizard Snow Beige California City Dragon Black London Sunset glow Pink Cloud

45

主题

1250

积分

4

精华

用户组 

易积分
5443
热心
99
好评
17

热心会员

[PHP]阿里云短信服务接口类[复制链接]
发表于 2018-3-31 22:44:40 | 显示全部楼层 |阅读模式
本帖最后由 幻令 于 2018-3-31 22:46 编辑
最近有项目需要验证手机,于是就找到了阿里云的短信服务,感觉用起来有些麻烦,而且限制挺多的,官方的接口有些受不了…下下来一解压,好几M,不知道里面啥玩意,没看了,于是就自己封装了一个

0x01 申请接口
首先需要去申请接口,申请短信模板啦,申请短信签名啦balalalal
申请地址:https://dysms.console.aliyun.com/dysms.htm#/overview1

0x02 代码
直接贴代码好了,233
因为我只需要发送短信,不需要其他的东西了,就只封装了发送短信,如果有其他需求可以看官方的接口:https://help.aliyun.com/document_detail/56189.html?spm=5176.doc60127.6.576.r4wrf8
需要其他的操作直接使用AliSms中的request方法,传入参数就可以了
效果:

发一条好几分钱,好贵啊,我就不多测试了
OA@LO0P0DZ@S6W6UC-1.jpg
Demo.php
  1. <?php
  2. $akID='xxxx';//$AccessKeyID
  3. $akS='xxxxx';//$AccessKeySecret
  4. $phone="15000000000";//手机号码
  5. $SignName='爱编码的Farmer';//签名
  6. $TemplateCode='SMS_XXXXX';//模板id
  7. $sms=new AliSms($akID,$akS);
  8. echo $sms->sendSms($SignName,$TemplateCode,['code'=>'test'.rand(10,99)],$phone);
复制代码
AliSms.php
  1. <?php
  2. /**
  3. *============================
  4. * author:Farmer
  5. * time:2017/12/28
  6. * blog:blog.icodef.com
  7. * function:阿里云短信发送
  8. *============================
  9. */


  10. namespace icf\lib\info;

  11. class AliSms {
  12.     private $akID;
  13.     private $akSecrt;

  14.     public function __construct($AccessKeyID, $AccessKeySecret) {
  15.         $this->akID = $AccessKeyID;
  16.         $this->akSecrt = $AccessKeySecret;

  17.     }

  18.     public function sendSms($SignName, $TemplateCode, $TemplateParam, $PhoneNumbers) {
  19.         return $this->request([
  20.             'Action' => 'SendSms',
  21.             'Version' => '2017-05-25',
  22.             'RegionId' => 'cn',
  23.             'SignName' => $SignName,
  24.             'TemplateCode' => $TemplateCode,
  25.             'TemplateParam' => json_encode($TemplateParam, JSON_UNESCAPED_UNICODE),
  26.             'PhoneNumbers' => $PhoneNumbers
  27.         ]);
  28.     }

  29.     public function request($param) {
  30.         $req = array_merge([
  31.             'AccessKeyId' => $this->akID,
  32.             'Timestamp' => gmdate("Y-m-d\TH:i:s\Z"),
  33.             'SignatureMethod' => 'HMAC-SHA1',
  34.             'SignatureVersion' => '1.0',
  35.             'SignatureNonce' => uniqid(mt_rand(0, 0xffff), true),
  36.             'Format' => 'JSON'
  37.         ], $param);
  38.         $getParam = $this->getReqString($req);
  39.         $Signature = $this->sign($getParam);
  40.         $http = new http("https://dysmsapi.aliyuncs.com/?Signature={$Signature}{$getParam}");
  41.         $http->https();
  42.         $data = $http->get();
  43.         return $data;
  44.     }

  45.     private function getReqString($req) {
  46.         ksort($req);
  47.         $ret = '';
  48.         foreach ($req as $key => $value) {
  49.             $ret .= '&' . $this->encode($key) . '=' . $this->encode($value);
  50.         }
  51.         return $ret;
  52.     }

  53.     private function sign($param) {
  54.         $stringToSign = "GET&%2F&" . $this->encode(substr($param, 1));
  55.         $sign = base64_encode(hash_hmac("sha1", $stringToSign, $this->akSecrt . "&", true));
  56.         return $this->encode($sign);
  57.     }

  58.     private function encode($str) {
  59.         $res = urlencode($str);
  60.         $res = preg_replace("/\+/", "%20", $res);
  61.         $res = preg_replace("/\*/", "%2A", $res);
  62.         $res = preg_replace("/%7E/", "~", $res);
  63.         return $res;
  64.     }

  65. }

  66. /**
  67. * Class http
  68. * @package icf\lib\info
  69. */
  70. class http {
  71.     private $curl;

  72.     public function __construct($url = '') {
  73.         $this->curl = curl_init($url);
  74.         curl_setopt($this->curl, CURLOPT_HEADER, 0); //不返回header部分
  75.         curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); //返回字符串,而非直接输出
  76.         curl_setopt($this->curl, CURLOPT_TIMEOUT, 10);
  77.     }

  78.     public function setopt($key, $value) {
  79.         curl_setopt($this->curl, $key, $value);
  80.     }

  81.     public function setRedirection($value = 1) {
  82.         curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, $value);
  83.     }

  84.     public function __destruct() {
  85.         // TODO: Implement __destruct() method.
  86.         curl_close($this->curl);
  87.     }

  88.     public function setCookie($cookie) {
  89.         curl_setopt($this->curl, CURLOPT_COOKIE, $cookie);
  90.     }

  91.     public function setHeader($header) {
  92.         curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header);
  93.     }

  94.     public function setUrl($url) {
  95.         curl_setopt($this->curl, CURLOPT_URL, $url);
  96.     }

  97.     public function https() {
  98.         curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
  99.     }

  100.     public function get($url = '') {
  101.         if (!empty($url)) {
  102.             $this->setUrl($url);
  103.         }
  104.         curl_setopt($this->curl, CURLOPT_POST, 0);
  105.         return $this->access();
  106.     }

  107.     public function post($url = '', $data = '') {
  108.         curl_setopt($this->curl, CURLOPT_POST, 1);
  109.         if (!empty($url)) {
  110.             $this->setUrl($url);
  111.             curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);
  112.         } else {
  113.             curl_setopt($this->curl, CURLOPT_POSTFIELDS, $url);
  114.         }
  115.         return $this->access();
  116.     }

  117.     public function access() {
  118.         $response = curl_exec($this->curl);
  119.         if ($response == false) return curl_error($this->curl);
  120.         return $response;
  121.     }
  122. }
复制代码


个人博客:blog.icodef.com
回复

使用道具 举报

4

主题

86

积分

0

精华

用户组 

易积分
8
热心
0
好评
0
发表于 2018-4-3 14:55:15 来自手机 | 显示全部楼层
到宿舍三生三世色额外
易如意中文编程学习交流论坛有你更精彩~
回复

使用道具 举报

5

主题

461

积分

0

精华

用户组 

易积分
30
热心
0
好评
0
QQ
发表于 2018-4-13 21:15:08 | 显示全部楼层

感谢楼主分享
易如意中文编程学习交流论坛有你更精彩~
回复

使用道具 举报

0

主题

6

积分

0

精华

用户组 

易积分
27
热心
0
好评
0
发表于 2018-6-15 15:46:53 | 显示全部楼层
阿里云短信服务接口类怎么封装呀?
易如意中文编程学习交流论坛有你更精彩~
回复

使用道具 举报

QQ|sitemap|免责声明|RGB颜色对照表|手机版|小黑屋| 易如意 - E4A中文编程学习交流论坛

GMT+8, 2024-3-28 18:16 , Processed in 0.050015 second(s), 41 queries .

Powered by Discuz! X3.4

© 2001-2018 eruyi.cn

返回顶部