| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Toplan\PhpSms; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Class SendCloudAgent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @property string $smsUser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @property string $smsKey | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class SendCloudAgent extends Agent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     public function sendSms($to, $content, $tempId, array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         $this->sendTemplateSms($to, $tempId, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public function sendTemplateSms($to, $tempId, array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $params = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             'msgType'    => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             'vars'       => $this->getTempDataString($data), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             'phone'      => $to, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             'templateId' => $tempId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $this->request('http://sendcloud.sohu.com/smsapi/send', $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function voiceVerify($to, $code, $tempId, array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $params = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             'phone' => $to, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             'code'  => $code, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->request('http://sendcloud.sohu.com/smsapi/sendVoice', $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     protected function request($sendUrl, array $params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $params = $this->createParams($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $result = $this->curl($sendUrl, $params, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->setResult($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     protected function createParams(array $params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $params = array_merge([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             'smsUser' => $this->smsUser, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ], $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $params['signature'] = $this->genSign($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return $params; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     protected function genSign($params) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         ksort($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $stringToBeSigned = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         foreach ($params as $k => $v) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $stringToBeSigned .= $k . '=' . $v . '&'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $stringToBeSigned = trim($stringToBeSigned, '&'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $stringToBeSigned = $this->smsKey . '&' . $stringToBeSigned . '&' . $this->smsKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         return md5($stringToBeSigned); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     protected function setResult($result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         var_dump($result); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if ($result) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             if (isset($result['result'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 $this->result(Agent::SUCCESS, (bool) $result['result']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 $this->result(Agent::INFO, $error['message']); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 $this->result(Agent::CODE, $result['statusCode']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $this->result(Agent::INFO, '请求失败'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     protected function getTempDataString(array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         return json_encode(array_map('strval', $data)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |     public function sendContentSms($to, $content) | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 90 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  |