├── .gitignore ├── LICENSE ├── README.md ├── _config.yml ├── composer.json ├── src ├── Client.php ├── Helper.php └── Request │ ├── ARequest.php │ ├── IRequest.php │ └── SendSms.php └── tests └── index.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | /composer.lock -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 叶子坑 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 阿里短信接口 2 | 3 | [![Latest Stable Version](https://poser.pugx.org/flc/dysms/v/stable)](https://packagist.org/packages/flc/dysms) 4 | [![Total Downloads](https://poser.pugx.org/flc/dysms/downloads)](https://packagist.org/packages/flc/dysms) 5 | ![php>=5.4](https://img.shields.io/badge/php->%3D5.4-orange.svg?maxAge=2592000) 6 | [![License](https://poser.pugx.org/flc/dysms/license)](https://packagist.org/packages/flc/dysms) 7 | [![996.icu](https://img.shields.io/badge/link-996.icu-red.svg)](https://996.icu) 8 | [![LICENSE](https://img.shields.io/badge/license-Anti%20996-blue.svg)](https://github.com/996icu/996.ICU/blob/master/LICENSE) 9 | 10 | > PS:**阿里大于** [https://github.com/flc1125/alidayu](https://github.com/flc1125/alidayu) 11 | 12 | ## 安装 13 | 14 | ```shell 15 | composer require flc/dysms 16 | ``` 17 | 18 | ## 使用 19 | 20 | ```php 21 | 'LTAIbVA2LRQ1tULr', 27 | 'accessKeySecret' => 'ocS48RUuyBPpQHsfoWokCuz8ZQbGxl', 28 | ]; 29 | 30 | $client = new Client($config); 31 | $sendSms = new SendSms; 32 | $sendSms->setPhoneNumbers('1500000000'); 33 | $sendSms->setSignName('叶子坑'); 34 | $sendSms->setTemplateCode('SMS_77670013'); 35 | $sendSms->setTemplateParam(['code' => rand(100000, 999999)]); 36 | $sendSms->setOutId('demo'); 37 | 38 | print_r($client->execute($sendSms)); 39 | ``` 40 | 41 | ## 支持 42 | 43 | - 官方网址: https://www.aliyun.com/product/sms?spm=5176.8142029.388261.339.WL7atM 44 | - 官方API文档: https://help.aliyun.com/document_detail/55451.html?spm=5176.doc55289.6.556.pMlBIe 45 | - composer: https://getcomposer.org/ 46 | 47 | ## 捐赠 48 | 49 | 如果你觉得本扩展对你有帮助,请捐赠以表支持,谢谢~~ 50 | 51 | 52 | 53 | 54 | 55 | 56 |

微信

支付宝

57 | 58 | ## License 59 | 60 | - MIT 61 | - Anti 996 62 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "flc/dysms", 3 | "description": "阿里短信-适用Laravel、Yii、Thinkphp等任何PHP项目...", 4 | "keywords": ["阿里短信", "flc", "dysms", "alisms", "aliyun"], 5 | "type": "library", 6 | "license": "MIT", 7 | "require": { 8 | "php": ">=5.4.0" 9 | }, 10 | "autoload": { 11 | "psr-4": { 12 | "Flc\\Dysms\\": "src/" 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/Client.php: -------------------------------------------------------------------------------- 1 | 11 | */ 12 | class Client 13 | { 14 | /** 15 | * 接口地址 16 | * @var string 17 | */ 18 | protected $api_uri = 'http://dysmsapi.aliyuncs.com/'; 19 | 20 | /** 21 | * 回传格式 22 | * @var string 23 | */ 24 | protected $format = 'json'; 25 | 26 | /** 27 | * 签名方式 28 | * @var string 29 | */ 30 | protected $signatureMethod = 'HMAC-SHA1'; 31 | 32 | /** 33 | * 接口请求方式[GET/POST] 34 | * @var string 35 | */ 36 | protected $httpMethod = 'POST'; 37 | 38 | /** 39 | * 配置项 40 | * @var array 41 | */ 42 | protected $config = []; 43 | 44 | /** 45 | * 初始化 46 | * @param array $config [description] 47 | */ 48 | public function __construct($config = []) 49 | { 50 | $this->config = $config; 51 | } 52 | 53 | /** 54 | * 请求 55 | * @param IRequest $request [description] 56 | * @return [type] [description] 57 | */ 58 | public function execute(IRequest $request) 59 | { 60 | $action = $request->getAction(); 61 | $reqParams = $request->getParams(); 62 | $pubParams = $this->getPublicParams(); 63 | 64 | $params = array_merge( 65 | $pubParams, 66 | ['Action' => $action], 67 | $reqParams 68 | ); 69 | 70 | 71 | // 签名 72 | $params['Signature'] = $this->generateSign($params); 73 | 74 | // 请求数据 75 | $resp = $this->curl( 76 | $this->api_uri, 77 | $params 78 | ); 79 | 80 | return json_decode($resp); 81 | } 82 | 83 | /** 84 | * 生成签名 85 | * @param array $params 待签参数 86 | * @return string 87 | */ 88 | protected function generateSign($params = []) 89 | { 90 | ksort($params); // 排序 91 | 92 | $arr = []; 93 | foreach ($params as $k => $v) { 94 | $arr[] = $this->percentEncode($k) . '=' . $this->percentEncode($v); 95 | } 96 | 97 | $queryStr = implode('&', $arr); 98 | $strToSign = $this->httpMethod . '&%2F&' . $this->percentEncode($queryStr); 99 | 100 | return base64_encode(hash_hmac('sha1', $strToSign, $this->config['accessKeySecret'] . '&', true)); 101 | } 102 | 103 | /** 104 | * 签名拼接转码 105 | * @param string $str 转码前字符串 106 | * @return string 107 | */ 108 | protected function percentEncode($str) 109 | { 110 | $res = urlencode($str); 111 | $res = preg_replace('/\+/', '%20', $res); 112 | $res = preg_replace('/\*/', '%2A', $res); 113 | $res = preg_replace('/%7E/', '~', $res); 114 | 115 | return $res; 116 | } 117 | 118 | /** 119 | * 公共返回参数 120 | * @return array 121 | */ 122 | protected function getPublicParams() 123 | { 124 | return [ 125 | 'AccessKeyId' => $this->config['accessKeyId'], 126 | 'Timestamp' => $this->getTimestamp(), 127 | 'Format' => $this->format, 128 | 'SignatureMethod' => $this->signatureMethod, 129 | 'SignatureVersion' => '1.0', 130 | 'SignatureNonce' => uniqid(), 131 | 'Version' => '2017-05-25', 132 | 'RegionId' => 'cn-hangzhou', 133 | ]; 134 | } 135 | 136 | /** 137 | * 返回时间格式 138 | * @return string 139 | */ 140 | protected function getTimestamp() 141 | { 142 | $timezone = date_default_timezone_get(); 143 | date_default_timezone_set('GMT'); 144 | $timestamp = date('Y-m-d\TH:i:s\Z'); 145 | date_default_timezone_set($timezone); 146 | 147 | return $timestamp; 148 | } 149 | 150 | /** 151 | * curl请求 152 | * @param string $url string 153 | * @param array|null $postFields 请求参数 154 | * @return [type] [description] 155 | */ 156 | protected function curl($url, $postFields = null) 157 | { 158 | $ch = curl_init(); 159 | curl_setopt($ch, CURLOPT_URL, $url); 160 | curl_setopt($ch, CURLOPT_FAILONERROR, false); 161 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 162 | //https 请求 163 | if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { 164 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 165 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 166 | } 167 | if (is_array($postFields) && 0 < count($postFields)) { 168 | $postBodyString = ""; 169 | foreach ($postFields as $k => $v) { 170 | $postBodyString .= "$k=" . urlencode($v) . "&"; 171 | } 172 | unset($k, $v); 173 | curl_setopt($ch, CURLOPT_POST, true); 174 | $header = array("content-type: application/x-www-form-urlencoded; charset=UTF-8"); 175 | curl_setopt($ch,CURLOPT_HTTPHEADER,$header); 176 | curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1)); 177 | } 178 | $reponse = curl_exec($ch); 179 | return $reponse; 180 | } 181 | } -------------------------------------------------------------------------------- /src/Helper.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | class Helper 10 | { 11 | /** 12 | * 返回唯一标识 13 | * @return string 14 | */ 15 | public static function uuid() 16 | { 17 | if (function_exists('com_create_guid')) { 18 | $uuid = com_create_guid(); 19 | } else { 20 | mt_srand((double) microtime() * 10000); 21 | $charid = strtoupper(md5(uniqid(rand(), true))); 22 | $hyphen = chr(45); 23 | $uuid = chr(123). 24 | substr($charid, 0, 8).$hyphen 25 | .substr($charid, 8, 4).$hyphen 26 | .substr($charid,12, 4).$hyphen 27 | .substr($charid,16, 4).$hyphen 28 | .substr($charid,20,12) 29 | .chr(125);// "}" 30 | } 31 | 32 | return trim(strtoupper($uuid), '{}'); 33 | } 34 | } -------------------------------------------------------------------------------- /src/Request/ARequest.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | abstract class ARequest implements IRequest 10 | { 11 | /** 12 | * API接口名称 13 | * @var string 14 | */ 15 | protected $action; 16 | 17 | /** 18 | * 接口请求参数 19 | * @var array 20 | */ 21 | protected $params = []; 22 | 23 | /** 24 | * 返回API名称 25 | * @return string 26 | */ 27 | public function getAction() 28 | { 29 | return $this->action; 30 | } 31 | 32 | /** 33 | * 接口请求参数 34 | * @return array 35 | */ 36 | public function getParams() 37 | { 38 | return $this->params; 39 | } 40 | } -------------------------------------------------------------------------------- /src/Request/IRequest.php: -------------------------------------------------------------------------------- 1 | 8 | */ 9 | interface IRequest 10 | { 11 | /** 12 | * 返回API接口名称 13 | * @return string 14 | */ 15 | public function getAction(); 16 | 17 | /** 18 | * 返回接口请求参数 19 | * @return array 20 | */ 21 | public function getParams(); 22 | } -------------------------------------------------------------------------------- /src/Request/SendSms.php: -------------------------------------------------------------------------------- 1 | 8 | * @link https://help.aliyun.com/document_detail/55451.html?spm=5176.doc55359.6.555.9caHua 9 | */ 10 | class SendSms extends ARequest implements IRequest 11 | { 12 | /** 13 | * API名称 14 | * @var string 15 | */ 16 | protected $action = 'SendSms'; 17 | 18 | /** 19 | * 设置短信接收号码 20 | * @param string|array $value 短信接收号码。支持以逗号分隔的形式进行批量调用,批量上限为20个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式 21 | */ 22 | public function setPhoneNumbers($value = '') 23 | { 24 | $this->params['PhoneNumbers'] = is_array($value) ? implode(',', $value) : $value; 25 | 26 | return $this; 27 | } 28 | 29 | /** 30 | * 设置短信签名 31 | * @param string $value 短信签名 32 | */ 33 | public function setSignName($value) 34 | { 35 | $this->params['SignName'] = $value; 36 | 37 | return $this; 38 | } 39 | 40 | /** 41 | * 设置短信模板ID 42 | * @param string $value 短信模板ID 43 | */ 44 | public function setTemplateCode($value) 45 | { 46 | $this->params['TemplateCode'] = $value; 47 | 48 | return $this; 49 | } 50 | 51 | /** 52 | * 设置短信模板变量 53 | * @param array $value 短信模板变量 54 | */ 55 | public function setTemplateParam($value = []) 56 | { 57 | $this->params['TemplateParam'] = json_encode($value, JSON_FORCE_OBJECT); 58 | 59 | return $this; 60 | } 61 | 62 | /** 63 | * 设置外部流水扩展字段 64 | * @param string $value 外部流水扩展字段 65 | */ 66 | public function setOutId($value) 67 | { 68 | $this->params['OutId'] = $value; 69 | 70 | return $this; 71 | } 72 | } -------------------------------------------------------------------------------- /tests/index.php: -------------------------------------------------------------------------------- 1 | 'LTAIbVA2LRQ1tULr', 9 | 'accessKeySecret' => 'ocS48RUuyBPpQHsfoWokCuz8ZQbGxl', 10 | ]; 11 | 12 | $client = new Client($config); 13 | $sendSms = new SendSms; 14 | $sendSms->setPhoneNumbers('15000000000'); 15 | $sendSms->setSignName('叶子坑'); 16 | // $sendSms->setTemplateCode('SMS_77665019'); 17 | // $sendSms->setTemplateParam(['username' => 'demo', 'time' => date('Y-m-d')]); 18 | $sendSms->setTemplateCode('SMS_85540016'); 19 | // $sendSms->setTemplateParam(['username' => 'demo', 'time' => date('Y-m-d')]); 20 | $sendSms->setOutId('demo'); 21 | 22 | print_r($client->execute($sendSms)); --------------------------------------------------------------------------------