123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- namespace app\index\service;
- use think\Config;
- class SmsService extends BaseService
- {
- const code_url = 'https://api.sms.jpush.cn/v1';
- private $appKey = '7ec8cf35b83d1f5fa23b9e4e';
- private $masterSecret = '237e321c3b7067e2a634470c';
- private $options;
- public function __construct(array $options = array()) {
- $this->options = array_merge(array(
- 'ssl_verify'=> false
- ), $options);
- $this->appKey = Config::get('SmsConfig')['user'];
- $this->masterSecret = Config::get('SmsConfig')['password'];
- }
- 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;
- }
-
- public function sendBatchCode($recipients, $temp_id = 1) {
- $url = self::code_url . '/messages/batch';
- $body = array('recipients' => $recipients, 'temp_id' => $temp_id);
- $res = $this->sendPost($url, $body);
- file_put_contents('sendmobile.jg',json_encode($res)."\n",FILE_APPEND);
- return $res;
- }
-
- 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);
- $header_text = substr($output, 0, $header_size);
- $body = substr($output, $header_size);
- $response['body'] = $body;
- $response['http_code'] = $httpCode;
- }
- curl_close($ch);
- return $response;
- }
- }
|