| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Toplan\PhpSms; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Class AlidayuAgent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @property string $sendUrl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @property string $appKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @property string $secretKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @property string $smsFreeSignName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @property string $calledShowNum | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class AlidayuAgent extends Agent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     public function sendSms($to, $content, $tempId, array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $this->sendTemplateSms($to, $tempId, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function sendTemplateSms($to, $tempId, array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $params = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             'method'             => 'alibaba.aliqin.fc.sms.num.send', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             'sms_type'           => 'normal', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             'sms_free_sign_name' => $this->smsFreeSignName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             'sms_param'          => json_encode($data), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             'rec_num'            => $to, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             'sms_template_code'  => $tempId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->request($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function voiceVerify($to, $code, $tempId, array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $params = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             'called_num'      => $to, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             'called_show_num' => $this->calledShowNum, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         if ($tempId) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             //文本转语音通知 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             $params['method'] = 'alibaba.aliqin.fc.tts.num.singlecall'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $params['tts_code'] = $tempId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $params['tts_param'] = json_encode($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } elseif ($code) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             //语音通知 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             $params['method'] = 'alibaba.aliqin.fc.voice.num.singlecall'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $params['voice_code'] = $code; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->request($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     protected function request(array $params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $sendUrl = $this->sendUrl ?: 'https://eco.taobao.com/router/rest'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $params = $this->createParams($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $result = $this->curl($sendUrl, $params, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->setResult($result, $this->genResponseName($params['method'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     protected function createParams(array $params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $params = array_merge([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             'app_key'            => $this->appKey, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             'v'                  => '2.0', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             'format'             => 'json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             'sign_method'        => 'md5', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             'timestamp'          => date('Y-m-d H:i:s'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         ], $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $params['sign'] = $this->genSign($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return $params; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     protected function genSign($params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         ksort($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $stringToBeSigned = $this->secretKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         foreach ($params as $k => $v) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             if (is_string($v) && '@' !== substr($v, 0, 1)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 $stringToBeSigned .= "$k$v"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         unset($k, $v); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $stringToBeSigned .= $this->secretKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return strtoupper(md5($stringToBeSigned)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     protected function setResult($result, $callbackName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         if ($result['request']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             $result = json_decode($result['response'], true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             if (isset($result[$callbackName]['result'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 $result = $result[$callbackName]['result']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 $this->result(Agent::SUCCESS, (bool) $result['success']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 $this->result(Agent::INFO, $result['msg']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 $this->result(Agent::CODE, $result['err_code']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             } elseif (isset($result['error_response'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 $error = $result['error_response']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 $this->result(Agent::INFO, json_encode($error)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 $this->result(Agent::CODE, 'code:' . $error['code'] . '|sub_code:' . $error['sub_code']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $this->result(Agent::INFO, '请求失败'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     protected function genResponseName($method) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         return str_replace('.', '_', $method) . '_response'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |     public function sendContentSms($to, $content) | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 117 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |  |