options = array_merge(array( 'ssl_verify'=> false ), $options); $this->appKey = $appKey; $this->masterSecret = $masterSecret; } public function sendCode($mobile, $temp_id = 1) { $url = self::code_url . '/codes'; $body = array('mobile' => $mobile, 'temp_id' => $temp_id); $res = $this->sendPost($url, $body); if(!empty($res['http_code']) && $res['http_code'] == 200){ $body = json_decode($res['body'], true); if(isset($body['error'])){ HelperService::returnJson(['code'=>400,'msg'=>'send fail','data'=>null]); } //1.添加一个短信记录 $mobile手机号 $body['msg_id']返回得内容 $temp_id传得数据 $res['msg_id']=$body['msg_id']; $res['status']=true; return $res; } $res['status'] =false; return $res; } /** * @param $mobile * @param int $temp_id * @param array $temp_para * @return mix */ public function sendMessage($mobile, $temp_id = 1,$temp_para=[]) { $url = self::code_url . '/messages'; $body = array('mobile' => $mobile, 'temp_id' => $temp_id,'temp_para'=>$temp_para); $res = $this->sendPost($url, $body); if(!empty($res['http_code']) && $res['http_code'] == 200){ $body = @json_decode($res['body'], true); if(isset($body['error'])){ HelperService::returnJson(['code'=>400,'msg'=>'send fail','data'=>null]); } //添加一个短信记录 return $res; } HelperService::returnJson(['code'=>400,'msg'=>'send fail','data'=>$res]); } public function checkCode($code,$msg_id) { if(empty($msg_id)){ return false; } $url = self::code_url . '/codes/' . $msg_id . "/valid"; $body = array('code' => $code); $res = $this->sendPost($url, $body); $arrRes = json_decode($res['body'], true); return $arrRes['is_valid']; } private function sendPost($url, $body) { $ch = curl_init(); $options = array( CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_HTTPHEADER => array( 'Content-Type: application/json', 'Connection: Keep-Alive' ), CURLOPT_USERAGENT => 'JSMS-API-PHP-CLIENT', CURLOPT_CONNECTTIMEOUT => 20, CURLOPT_TIMEOUT => 120, CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERPWD => $this->appKey . ":" . $this->masterSecret, CURLOPT_URL => $url, CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($body) ); if (!$this->options['ssl_verify']) { $options[CURLOPT_SSL_VERIFYPEER] = false; $options[CURLOPT_SSL_VERIFYHOST] = 0; } curl_setopt_array($ch, $options); $output = curl_exec($ch); if($output === false) { return "Error Code:" . curl_errno($ch) . ", Error Message:".curl_error($ch); } else { $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $body = substr($output, $header_size); $response['body'] = $body; $response['http_code'] = $httpCode; } curl_close($ch); return $response; } }