├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml.dist ├── src └── Alidayu.php └── tests ├── AxbBindSecondTest.php ├── AxbBindTest.php ├── AxbUnbindTest.php ├── BaseTest.php ├── SmsQueryTest.php ├── SmsSendTest.php ├── TtsSingleCallTest.php └── bootstrap.php /.gitignore: -------------------------------------------------------------------------------- 1 | composer.lock 2 | vendor/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 chocoboxxf 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 | # yii2-alidayu-sdk 2 | 基于Yii2实现的阿里大鱼API SDK(目前开发中) 3 | 4 | 环境条件 5 | -------- 6 | - >= PHP 5.4 7 | - >= Yii 2.0 8 | - >= GuzzleHttp 6.0 9 | 10 | 安装 11 | ---- 12 | 13 | 添加下列代码在``composer.json``文件中并执行``composer update --no-dev``操作 14 | 15 | ```json 16 | { 17 | "require": { 18 | "chocoboxxf/yii2-alidayu-sdk": "dev-master" 19 | } 20 | } 21 | ``` 22 | 23 | 设置方法 24 | -------- 25 | 26 | ```php 27 | // 全局使用 28 | // 在config/main.php配置文件中定义component配置信息 29 | 'components' => [ 30 | ..... 31 | 'alidayu' => [ 32 | 'class' => 'chocoboxxf\Alidayu\Alidayu', 33 | 'appKey' => '1234', // 淘宝开发平台App Key 34 | 'appSecret' => '12345678', // 淘宝开发平台App Secret 35 | 'partnerKey' => 'PARTNER_NAME_AXN', // 阿里大鱼提供的第三方合作伙伴使用的KEY 36 | 'env' => 'sandbox', // 使用环境,分sandbox(测试环境), production(生产环境) 37 | ] 38 | .... 39 | ] 40 | // 代码中调用(调用短信发送接口示例) 41 | $result = Yii::$app->alidayu->smsSend('13000000000', 'SMS_100000', '测试签名', 'normal', ['code' => '111111'], '100000'); 42 | .... 43 | ``` 44 | 45 | ```php 46 | // 局部调用 47 | $alidayu = Yii::createObject([ 48 | 'class' => 'chocoboxxf\Alidayu\Alidayu', 49 | 'appKey' => '1234', // 淘宝开发平台App Key 50 | 'appSecret' => '12345678', // 淘宝开发平台App Secret 51 | 'partnerKey' => 'PARTNER_NAME_AXN', // 阿里大鱼提供的第三方合作伙伴使用的KEY 52 | 'env' => 'sandbox', // 使用环境,分sandbox(测试环境), production(生产环境) 53 | ]); 54 | // 调用短信发送接口示例 55 | $result = $alidayu->smsSend('13000000000', 'SMS_100000', '测试签名', 'normal', ['code' => '111111'], '100000'); 56 | .... 57 | ``` 58 | 59 | 使用示例 60 | -------- 61 | 62 | 短信发送接口 63 | 64 | ```php 65 | $result = Yii::$app->alidayu->smsSend('13000000000', 'SMS_100000', '测试签名', 'normal', ['code' => '111111'], '100000'); 66 | if (isset($result['alibaba_aliqin_fc_sms_num_send_response'])) { 67 | // 正常情况 68 | // 返回数据格式 69 | // { 70 | // "alibaba_aliqin_fc_sms_num_send_response":{ 71 | // "result":{ 72 | // "err_code":"0", 73 | // "model":"100000000000^1000000000000", 74 | // "success":true 75 | // }, 76 | // "request_id":"zt9pqaglr3tk" 77 | // } 78 | // } 79 | .... 80 | } elseif (isset($result['error_response'])) { 81 | // 出错情况 82 | // 返回数据格式 83 | // { 84 | // "error_response":{ 85 | // "code":15, 86 | // "msg":"Remote service error", 87 | // "sub_code":"isv.ACCOUNT_NOT_EXISTS", 88 | // "sub_msg":"阿里大于账户不存在", 89 | // "request_id":"zt9pqaglr3tk" 90 | // } 91 | // } 92 | .... 93 | } else { 94 | // 异常情况 95 | .... 96 | } 97 | .... 98 | ``` 99 | 100 | 短信发送记录查询接口 101 | 102 | ```php 103 | $result = Yii::$app->alidayu->smsQuery('13000000000', '20160909', 1, 50, '100000000000^1000000000000'); 104 | if (isset($result['alibaba_aliqin_fc_sms_num_query_response'])) { 105 | // 正常情况 106 | // 返回数据格式 107 | // { 108 | //     "alibaba_aliqin_fc_sms_num_query_response":{ 109 | //         "current_page":1, 110 | //         "page_size":50, 111 | //         "total_count":1, 112 | //         "total_page":1, 113 | //         "values":{ 114 | //             "fc_partner_sms_detail_dto":[ 115 | //                 { 116 | //                     "extend":"100000", 117 | //                     "rec_num":"13000000000", 118 | //                     "result_code":"DELIVRD", 119 | //                     "sms_code":"SMS_100000", 120 | //                     "sms_content":"【测试*】您的验证码是******", 121 | //                     "sms_receiver_time":"2016-09-09 15:27:46", 122 | //                     "sms_send_time":"2016-09-09 15:27:42", 123 | //                     "sms_status":3 124 | //                 } 125 | //             ] 126 | //         } 127 | //     } 128 | // } 129 | .... 130 | } elseif (isset($result['error_response'])) { 131 | // 出错情况 132 | // 返回数据格式 133 | // { 134 | // "error_response":{ 135 | // "code":15, 136 | // "msg":"Remote service error", 137 | // "sub_code":"isv.ACCOUNT_NOT_EXISTS", 138 | // "sub_msg":"阿里大于账户不存在", 139 | // "request_id":"zt9pqaglr3tk" 140 | // } 141 | // } 142 | .... 143 | } else { 144 | // 异常情况 145 | .... 146 | } 147 | .... 148 | ``` 149 | 150 | 文本转语音通知接口 151 | 152 | ```php 153 | $result = Yii::$app->alidayu->ttsSingleCall('13000000000', 'TTS_14730399', '051482043260', ['code' => '111111'], '100000')); 154 | if (isset($result['alibaba_aliqin_fc_tts_num_singlecall_response'])) { 155 | // 正常情况 156 | // 返回数据格式 157 | // { 158 | //     "alibaba_aliqin_fc_tts_num_singlecall_response":{ 159 | //         "result":{ 160 | //             "err_code":"0", 161 | //             "model":"100000000000^1000000000000", 162 | //             "success":true 163 | //         }, 164 | // "request_id":"zt9pqaglr3tk" 165 | //     } 166 | // } 167 | .... 168 | } elseif (isset($result['error_response'])) { 169 | // 出错情况 170 | // 返回数据格式 171 | // { 172 | // "error_response":{ 173 | // "code":15, 174 | // "msg":"Remote service error", 175 | // "sub_code":"isv.TTS_TEMPLATE_ILLEGAL", 176 | // "sub_msg":"未找到审核通过的文本转语音模板,ttsCode=TTS_100000,partnerId=10000000000", 177 | // "request_id":"zt9pqaglr3tk" 178 | // } 179 | .... 180 | } else { 181 | // 异常情况 182 | .... 183 | } 184 | .... 185 | ``` 186 | 187 | AXB(AXN)一次绑定接口 188 | 189 | ```php 190 | $result = Yii::$app->alidayu->AxbBind('13000000001', '13000000000', '2017-01-01 00:00:00'); 191 | if (isset($result['alibaba_aliqin_secret_axb_bind_response'])) { 192 | // 正常情况 193 | // 返回数据格式 194 | // { 195 | // "alibaba_aliqin_secret_axb_bind_response":{ 196 | // "subs_id":12345, 197 | // "secret_no_x":"13300000000" 198 | // } 199 | // } 200 | .... 201 | } elseif (isset($result['error_response'])) { 202 | // 出错情况 203 | // 返回数据格式 204 | // { 205 | // "error_response":{ 206 | // "code":40, 207 | // "msg":"Missing required arguments:phone_no_b", 208 | // "request_id":"zt9pqaglr3tk" 209 | // } 210 | // } 211 | .... 212 | } else { 213 | // 异常情况 214 | .... 215 | } 216 | .... 217 | ``` 218 | 219 | AXN二次绑定接口 220 | 221 | ```php 222 | $result = Yii::$app->alidayu->AxbBindSecond('123', '13000000000', '2017-01-01 00:00:00'); 223 | if (isset($result['alibaba_aliqin_secret_axb_bind_second_response'])) { 224 | // 正常情况 225 | // 返回数据格式 226 | // { 227 | // "alibaba_aliqin_secret_axb_bind_second_response":{ 228 | // "bind_success":true 229 | // } 230 | // } 231 | .... 232 | } elseif (isset($result['error_response'])) { 233 | // 出错情况 234 | // 返回数据格式 235 | // { 236 | // "error_response":{ 237 | // "code":40, 238 | // "msg":"Missing required arguments:phone_no_b", 239 | // "request_id":"zt9pqaglr3tk" 240 | // } 241 | // } 242 | .... 243 | } else { 244 | // 异常情况 245 | .... 246 | } 247 | .... 248 | ``` 249 | 250 | AXB(AXN)关系解绑接口 251 | 252 | ```php 253 | $result = Yii::$app->alidayu->AxbUnbind('123'); 254 | if (isset($result['alibaba_aliqin_secret_axb_unbind_response'])) { 255 | // 正常情况 256 | // 返回数据格式 257 | // { 258 | // "alibaba_aliqin_secret_axb_unbind_response":{ 259 | // "unbind_success":true, 260 | // "request_id":"zt9pqaglr3tk" 261 | // } 262 | // } 263 | .... 264 | } elseif (isset($result['error_response'])) { 265 | // 出错情况 266 | // 返回数据格式 267 | // { 268 | // "error_response":{ 269 | // "code":40, 270 | // "msg":"Missing required arguments:subs_id", 271 | // "request_id":"zt9pqaglr3tk" 272 | // } 273 | // } 274 | .... 275 | } else { 276 | // 异常情况 277 | .... 278 | } 279 | .... 280 | ``` -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "chocoboxxf/yii2-alidayu-sdk", 3 | "type": "yii2-extension", 4 | "description": "Alidayu API for Yii 2.0", 5 | "keywords": ["yii2", "alidayu", "sms", "telephone", "sdk"], 6 | "homepage": "https://github.com/chocoboxxf/yii2-alidayu-sdk", 7 | "license": "MIT", 8 | "support": { 9 | "issues": "https://github.com/chocoboxxf/yii2-alidayu-sdk/issues", 10 | "wiki": "https://github.com/chocoboxxf/yii2-alidayu-sdk/wiki", 11 | "source": "https://github.com/chocoboxxf/yii2-alidayu-sdk" 12 | }, 13 | "authors": [ 14 | { 15 | "name": "chocoboxxf", 16 | "email": "neil.xuxf@gmail.com", 17 | "homepage": "https://github.com/chocoboxxf", 18 | "role": "Developer" 19 | } 20 | ], 21 | "require": { 22 | "php": ">=5.4.0", 23 | "yiisoft/yii2": "*", 24 | "guzzlehttp/guzzle": "~6.0" 25 | }, 26 | "require-dev": { 27 | "phpunit/phpunit": ">=4.0" 28 | }, 29 | "autoload": { 30 | "psr-4": { 31 | "chocoboxxf\\Alidayu\\": "src/" 32 | } 33 | }, 34 | "autoload-dev": { 35 | "psr-4": { 36 | "chocoboxxf\\Alidayu\\Tests\\": "tests/" 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 14 | 15 | 16 | ./tests/ 17 | 18 | 19 | 20 | 21 | 22 | ./src/ 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/Alidayu.php: -------------------------------------------------------------------------------- 1 | [ 54 | Alidayu::ENV_SANDBOX => 'http://gw.api.tbsandbox.com/router/rest', 55 | Alidayu::ENV_PRODUCTION => 'http://gw.api.taobao.com/router/rest', 56 | Alidayu::ENV_OVERSEA => 'http://api.taobao.com/router/rest', 57 | ], 58 | 'https' => [ 59 | Alidayu::ENV_SANDBOX => 'https://gw.api.tbsandbox.com/router/rest', 60 | Alidayu::ENV_PRODUCTION => 'https://eco.taobao.com/router/rest', 61 | Alidayu::ENV_OVERSEA => 'https://api.taobao.com/router/rest', 62 | ] 63 | ]; 64 | 65 | /** 66 | * 淘宝开发平台App Key 67 | * @var string 68 | */ 69 | public $appKey; 70 | /** 71 | * 淘宝开发平台App Secret 72 | * @var string 73 | */ 74 | public $appSecret; 75 | /** 76 | * 阿里大鱼提供的第三方合作伙伴使用的KEY 77 | * @var string 78 | */ 79 | public $partnerKey; 80 | /** 81 | * 返回结果格式 82 | * @var string 83 | */ 84 | public $format = Alidayu::FORMAT_JSON; 85 | /** 86 | * API版本 87 | * @var string 88 | */ 89 | public $appVersion = '2.0'; 90 | /** 91 | * API是否使用SSL连接 92 | * @var bool 93 | */ 94 | public $isSecure = false; 95 | /** 96 | * API环境 97 | * @var string 98 | */ 99 | public $env = Alidayu::ENV_SANDBOX; 100 | /** 101 | * 使用签名算法 102 | * @var string 103 | */ 104 | public $signMethod = Alidayu::SIGN_METHOD_MD5; 105 | /** 106 | * API Client 107 | * @var \GuzzleHttp\Client 108 | */ 109 | public $apiClient; 110 | 111 | public function init() 112 | { 113 | parent::init(); 114 | if (!isset($this->appKey)) { 115 | throw new InvalidConfigException('请先配置App Key'); 116 | } 117 | if (!isset($this->appSecret)) { 118 | throw new InvalidConfigException('请先配置App Secret'); 119 | } 120 | $this->apiClient = new Client([ 121 | 'base_uri' => $this->isSecure ? $this->apiUrls['https'][$this->env] : $this->apiUrls['http'][$this->env], 122 | ]); 123 | } 124 | 125 | /** 126 | * AXB(AXN)一次绑定接口 127 | * 绑定接口用于实现真实号码和虚拟号码之间的绑定关系,只有在完成绑定以后用户才可以使用虚拟号码的基本通信能力。 128 | * @param string $phoneA 号码A 129 | * @param string $phoneB 号码B(AXB模式下必填,AXN模式下任意填写) 130 | * @param string $endDate 到期自动解绑时间(YYYY-mm-dd HH:MM:SS) 131 | * @param bool|false $otherCall 第三方是否可拨打X转接到号码A 132 | * @param bool|false $needRecord 是否需要录音 133 | * @return mixed 134 | * @throws GuzzleException 135 | */ 136 | public function AxbBind($phoneA, $phoneB, $endDate, $otherCall = false, $needRecord = false) 137 | { 138 | // 公共参数 139 | $data = [ 140 | 'app_key' => $this->appKey, 141 | 'timestamp' => $this->getTimestamp(), 142 | 'format' => $this->format, 143 | 'v' => $this->appVersion, 144 | 'sign_method' => $this->signMethod, 145 | 'method' => Alidayu::API_ALIBABA_ALIQIN_SECRET_AXB_BIND, 146 | ]; 147 | 148 | // 入参 149 | $data['partner_key'] = $this->partnerKey; 150 | $data['phone_no_a'] = $phoneA; 151 | $data['end_date'] = $endDate; 152 | $data['enable_other_call'] = $otherCall ? 'true' : 'false'; 153 | $data['need_record'] = $needRecord ? 'true' : 'false'; 154 | $data['phone_no_b'] = $phoneB; 155 | 156 | // 签名 157 | $signature = $this->getSignature($data, $this->signMethod); 158 | $data['sign'] = $signature; 159 | // 请求 160 | return $this->post('', $data); 161 | } 162 | 163 | /** 164 | * AXN二次绑定接口 165 | * 二次绑定接口用户确认,AX与B之间的关系,在一次绑定接口调用的基础上,再通过调用二次绑定接口,最终形成A与B之间的关系。 166 | * @param string $subsId 绑定关系ID 167 | * @param string $phoneB 号码B 168 | * @param string $endDate 到期自动解绑时间(YYYY-mm-dd HH:MM:SS) 169 | * @param bool|false $otherCall 第三方是否可拨打X转接到号码A 170 | * @param bool|false $needRecord 是否需要录音 171 | * @return mixed 172 | * @throws GuzzleException 173 | */ 174 | public function AxbBindSecond($subsId, $phoneB, $endDate, $otherCall = false, $needRecord = false) 175 | { 176 | // 公共参数 177 | $data = [ 178 | 'app_key' => $this->appKey, 179 | 'timestamp' => $this->getTimestamp(), 180 | 'format' => $this->format, 181 | 'v' => $this->appVersion, 182 | 'sign_method' => $this->signMethod, 183 | 'method' => Alidayu::API_ALIBABA_ALIQIN_SECRET_AXB_BIND_SECOND, 184 | ]; 185 | 186 | // 入参 187 | $data['partner_key'] = $this->partnerKey; 188 | $data['subs_id'] = $subsId; 189 | $data['phone_no_b'] = $phoneB; 190 | $data['end_date'] = $endDate; 191 | $data['enable_other_call'] = $otherCall ? 'true' : 'false'; 192 | $data['need_record'] = $needRecord ? 'true' : 'false'; 193 | 194 | // 签名 195 | $signature = $this->getSignature($data, $this->signMethod); 196 | $data['sign'] = $signature; 197 | // 请求 198 | return $this->post('', $data); 199 | } 200 | 201 | /** 202 | * AXB(AXN)关系解绑接口 203 | * 通过该接口,实现AXB三元关系的解绑,解绑过后,再通过呼叫X,将不能找到A或者B。 204 | * @param string $subsId 绑定关系ID 205 | * @return mixed 206 | * @throws GuzzleException 207 | */ 208 | public function AxbUnbind($subsId) 209 | { 210 | // 公共参数 211 | $data = [ 212 | 'app_key' => $this->appKey, 213 | 'timestamp' => $this->getTimestamp(), 214 | 'format' => $this->format, 215 | 'v' => $this->appVersion, 216 | 'sign_method' => $this->signMethod, 217 | 'method' => Alidayu::API_ALIBABA_ALIQIN_SECRET_AXB_UNBIND, 218 | ]; 219 | 220 | // 入参 221 | $data['partner_key'] = $this->partnerKey; 222 | $data['subs_id'] = $subsId; 223 | 224 | // 签名 225 | $signature = $this->getSignature($data, $this->signMethod); 226 | $data['sign'] = $signature; 227 | // 请求 228 | return $this->post('', $data); 229 | } 230 | 231 | /** 232 | * 短信发送 233 | * @param string $recNum 短信接收号码。支持单个或多个手机号码,群发短信需传入多个号码,以英文逗号分隔,一次调用最多传入200个号码。 234 | * @param string $templateCode 短信模板ID,传入的模板必须是在阿里大于“管理中心-短信模板管理”中的可用模板。 235 | * @param string $signName 短信签名,传入的短信签名必须是在阿里大于“管理中心-短信签名管理”中的可用签名。 236 | * @param string $smsType 短信类型,传入值请填写normal 237 | * @param array $param 短信模板变量,传参规则{"key":"value"},key的名字须和申请模板中的变量名一致,多个变量之间以逗号隔开。 238 | * @param string $extend 公共回传参数,在“消息返回”中会透传回该参数 239 | * @return mixed 240 | * @throws GuzzleException 241 | */ 242 | public function smsSend($recNum, $templateCode, $signName, $smsType = 'normal', $param = [], $extend = '') 243 | { 244 | // 公共参数 245 | $data = [ 246 | 'app_key' => $this->appKey, 247 | 'timestamp' => $this->getTimestamp(), 248 | 'format' => $this->format, 249 | 'v' => $this->appVersion, 250 | 'sign_method' => $this->signMethod, 251 | 'method' => Alidayu::API_ALIBABA_ALIQIN_FC_SMS_NUM_SEND, 252 | ]; 253 | 254 | // 入参 255 | $data['rec_num'] = $recNum; 256 | $data['sms_template_code'] = $templateCode; 257 | $data['sms_free_sign_name'] = $signName; 258 | $data['sms_type'] = $smsType; 259 | if (count($param) > 0) { 260 | $data['sms_param'] = json_encode($param); 261 | } 262 | if ($extend !== '') { 263 | $data['extend'] = $extend; 264 | } 265 | 266 | // 签名 267 | $signature = $this->getSignature($data, $this->signMethod); 268 | $data['sign'] = $signature; 269 | // 请求 270 | return $this->post('', $data); 271 | } 272 | 273 | /** 274 | * 短信发送记录查询 275 | * @param string $recNum 短信接收号码 276 | * @param string $queryDate 短信发送日期,支持近30天记录查询,格式yyyyMMdd 277 | * @param int $currentPage 分页参数,页码 278 | * @param int $pageSize 分页参数,每页数量。最大值50 279 | * @param string $bizId 短信发送流水,同发送结果中model值 280 | * @return mixed 281 | * @throws GuzzleException 282 | */ 283 | public function smsQuery($recNum, $queryDate, $currentPage = 1, $pageSize = 50, $bizId = '') 284 | { 285 | // 公共参数 286 | $data = [ 287 | 'app_key' => $this->appKey, 288 | 'timestamp' => $this->getTimestamp(), 289 | 'format' => $this->format, 290 | 'v' => $this->appVersion, 291 | 'sign_method' => $this->signMethod, 292 | 'method' => Alidayu::API_ALIBABA_ALIQIN_FC_SMS_NUM_QUERY, 293 | ]; 294 | 295 | // 入参 296 | $data['rec_num'] = $recNum; 297 | $data['query_date'] = $queryDate; 298 | $data['current_page'] = $currentPage; 299 | $data['page_size'] = $pageSize; 300 | if ($bizId !== '') { 301 | $data['biz_id'] = $bizId; 302 | } 303 | 304 | // 签名 305 | $signature = $this->getSignature($data, $this->signMethod); 306 | $data['sign'] = $signature; 307 | // 请求 308 | return $this->post('', $data); 309 | } 310 | 311 | /** 312 | * 文本转语音通知 313 | * @param string $calledNum 被叫号码,支持国内手机号与固话号码 314 | * @param string $ttsCode TTS模板ID,传入的模板必须是在阿里大于“管理中心-语音TTS模板管理”中的可用模板 315 | * @param string $calledShowNum 被叫号显,传入的显示号码必须是阿里大于“管理中心-号码管理”中申请或购买的号码 316 | * @param array $param 文本转语音(TTS)模板变量,传参规则{"key":"value"},key的名字须和TTS模板中的变量名一致 317 | * @param string $extend 公共回传参数,在“消息返回”中会透传回该参数 318 | * @return mixed 319 | * @throws GuzzleException 320 | */ 321 | public function ttsSingleCall($calledNum, $ttsCode, $calledShowNum, $param = [], $extend = '') 322 | { 323 | // 公共参数 324 | $data = [ 325 | 'app_key' => $this->appKey, 326 | 'timestamp' => $this->getTimestamp(), 327 | 'format' => $this->format, 328 | 'v' => $this->appVersion, 329 | 'sign_method' => $this->signMethod, 330 | 'method' => Alidayu::API_ALIBABA_ALIQIN_FC_TTS_NUM_SINGLECALL, 331 | ]; 332 | 333 | // 入参 334 | $data['called_num'] = $calledNum; 335 | $data['tts_code'] = $ttsCode; 336 | $data['called_show_num'] = $calledShowNum; 337 | if (count($param) > 0) { 338 | $data['tts_param'] = json_encode($param); 339 | } 340 | if ($extend !== '') { 341 | $data['extend'] = $extend; 342 | } 343 | 344 | // 签名 345 | $signature = $this->getSignature($data, $this->signMethod); 346 | $data['sign'] = $signature; 347 | // 请求 348 | return $this->post('', $data); 349 | } 350 | 351 | /** 352 | * 生成sign签名 353 | * @param array $params 入参 354 | * @param string $method 签名方式:md5或hmac-md5 355 | * @return string 356 | */ 357 | public function getSignature($params = [], $method = Alidayu::SIGN_METHOD_HMAC) 358 | { 359 | $rawQuery = []; 360 | ksort($params); 361 | foreach ($params as $k => $v) { 362 | $rawQuery[] = sprintf('%s%s', $k, $v); 363 | } 364 | $rawText = implode('', $rawQuery); 365 | $signature = ''; 366 | if ($method === Alidayu::SIGN_METHOD_MD5) { 367 | $signature = md5($this->appSecret . $rawText . $this->appSecret); 368 | } elseif ($method === Alidayu::SIGN_METHOD_HMAC) { 369 | $signature = hash_hmac('md5', $rawText, $this->appSecret); 370 | } 371 | return strtoupper($signature); 372 | } 373 | 374 | /** 375 | * 获取当前时间 376 | * @return string 377 | */ 378 | public function getTimestamp() 379 | { 380 | return date('Y-m-d H:i:s'); 381 | } 382 | 383 | /** 384 | * post请求 385 | * @param string $url 接口相对路径 386 | * @param array $data 接口传参 387 | * @param array $headers HTTP Header 388 | * @return mixed 389 | * @throws GuzzleException 390 | */ 391 | public function post($url, $data, $headers = []) 392 | { 393 | $request = new Request('POST', $url, $headers); 394 | $response = $this->apiClient->send( 395 | $request, 396 | [ 397 | 'form_params' => $data, 398 | ] 399 | ); 400 | $result = json_decode($response->getBody(), true); 401 | return $result; 402 | } 403 | 404 | /** 405 | * get请求 406 | * @param string $url 接口相对路径 407 | * @param array $data 接口传参 408 | * @param array $headers HTTP Header 409 | * @return mixed 410 | * @throws GuzzleException 411 | */ 412 | public function get($url, $data, $headers = []) 413 | { 414 | $request = new Request('GET', $url, $headers); 415 | $response = $this->apiClient->send( 416 | $request, 417 | [ 418 | 'query' => $data, 419 | ] 420 | ); 421 | $result = json_decode($response->getBody(), true); 422 | return $result; 423 | } 424 | } -------------------------------------------------------------------------------- /tests/AxbBindSecondTest.php: -------------------------------------------------------------------------------- 1 | client->AxbBindSecond('123', '13000000000', '2017-01-01 00:00:00', true, true)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/AxbBindTest.php: -------------------------------------------------------------------------------- 1 | client->AxbBind('13000000001', '13000000000', '2017-01-01 00:00:00', true, true)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/AxbUnbindTest.php: -------------------------------------------------------------------------------- 1 | client->AxbUnbind('123')); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/BaseTest.php: -------------------------------------------------------------------------------- 1 | client = Yii::createObject([ 23 | 'class' => 'chocoboxxf\Alidayu\Alidayu', 24 | 'appKey' => isset($_ENV['APP_KEY']) ? $_ENV['APP_KEY'] : 'APP_KEY', 25 | 'appSecret' => isset($_ENV['APP_SECRET']) ? $_ENV['APP_SECRET'] : 'APP_SECRET', 26 | 'partnerKey' => isset($_ENV['PARTNER_KEY']) ? $_ENV['PARTNER_KEY'] : 'PARTNER_KEY', 27 | 'env' => isset($_ENV['APP_ENV']) ? $_ENV['APP_ENV'] : 'sandbox', 28 | ]); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tests/SmsQueryTest.php: -------------------------------------------------------------------------------- 1 | client->smsQuery('13000000000', '20160909', 1, 50, '')); 15 | } 16 | 17 | public function testQueryOne() 18 | { 19 | var_dump($this->client->smsQuery('13000000000', '20160909', 1, 50, '100000000000^1000000000000')); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/SmsSendTest.php: -------------------------------------------------------------------------------- 1 | client->smsSend('13000000000', 'SMS_100000', '测试签名', 'normal', ['code' => '111111'], '100000')); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/TtsSingleCallTest.php: -------------------------------------------------------------------------------- 1 | client->ttsSingleCall('13000000000', 'TTS_10000', '02100000000', ['code' => ',1,2,3,4,5,6'], '100000')); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 |