オーロラプッシュメールapiインタフェース

9828 ワード

以下はlaravelバージョンです.tpバージョンはredisを変更し、解析したファイルを正しく呼び出すだけで使用できます.
まずオーロラが提供するSDKファイルを導入する必要がある
プロジェクト内のcomposer.jsonファイルにJSMS依存を追加
"require": {
    "jiguang/jsms": "~1.0"
}
  
 $ php composer.phar install 
  
 $ composer install 
    。

私はjsms/srcの下のJMSSをphpは説明した
アプリの下にLibrariesを作成してJMSSを作成します.phpファイルは次のコードをコピーします.
appKey = $appKey;
        $this->masterSecret = $masterSecret;
        $this->options = array_merge([
            'ssl_verify'  => true,
            'disable_ssl' => false
        ], $options);
    }
    //     
    public function sendCode($mobile, $temp_id) {
        $url = self::URL . 'codes';
        $body = array('mobile' => $mobile, 'temp_id' => $temp_id);
        return $this->request('POST', $url, $body);
    }
    //     
    public function sendVoiceCode($mobile, $options = []) {
        $url = self::URL . 'voice_codes';
        $body = array('mobile' => $mobile);

        if (!empty($options)) {
            if (is_array($options)) {
                $body = array_merge($options, $body);
            } else {
                $body['ttl'] = $options;
            }
        }
        return $this->request('POST', $url, $body);
    }
    //      
    public function checkCode($msg_id, $code) {
        $url = self::URL . 'codes/' . $msg_id . "/valid";
        $body = array('code' => $code);
        return $this->request('POST', $url, $body);
    }

    public function sendMessage($mobile, $temp_id, array $temp_para = [], $time = null) {
        $path = 'messages';
        $body = array(
            'mobile'    => $mobile,
            'temp_id'   => $temp_id,
        );
        if (!empty($temp_para)) {
            $body['temp_para'] = $temp_para;
        }
        if (isset($time)) {
            $path = 'schedule';
            $body['send_time'] = $time;
        }
        $url = self::URL . $path;
        return $this->request('POST', $url, $body);
    }

    public function sendBatchMessage($temp_id, array $recipients, $time = null) {
        $path = 'messages';
        foreach ($recipients as $mobile => $temp_para) {
            $r[] = array(
                'mobile'    => $mobile,
                'temp_para' => $temp_para
            );
        }
        $body = array(
            'temp_id'    => $temp_id,
            'recipients' => $r
        );
        if (isset($time)) {
            $path = 'schedule';
            $body['send_time'] = $time;
        }
        $url = self::URL . $path . '/batch';
        return $this->request('POST', $url, $body);
    }

    public function showSchedule($scheduleId) {
        $url = self::URL . 'schedule/' . $scheduleId;
        return $this->request('GET', $url);
    }

    public function deleteSchedule($scheduleId) {
        $url = self::URL . 'schedule/' . $scheduleId;
        return $this->request('DELETE', $url);
    }

    public function getAppBalance() {
        $url = self::URL . 'accounts/app';
        return $this->request('GET', $url);
    }

    private function request($method, $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_CUSTOMREQUEST => $method,
        );
        if (!$this->options['ssl_verify']
            || (bool) $this->options['disable_ssl']) {
            $options[CURLOPT_SSL_VERIFYPEER] = false;
            $options[CURLOPT_SSL_VERIFYHOST] = 0;
        }
        if (!empty($body)) {
            $options[CURLOPT_POSTFIELDS] = json_encode($body);
        }
        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);
            $headers = array();

            foreach (explode("\r
", $header_text) as $i => $line) { if (!empty($line)) { if ($i === 0) { $headers[0] = $line; } else if (strpos($line, ": ")) { list ($key, $value) = explode(': ', $line); $headers[$key] = $value; } } } $response['headers'] = $headers; $response['body'] = json_decode($body, true); $response['http_code'] = $httpCode; } curl_close($ch); return $response; } }

独自のクラスライブラリまたは関数の導入
プロジェクトディレクトリの下でjsonに加わる
"autoload": {
    "files":[
         "app/Libraries/JSMS.php"
    ]
}

プロジェクトディレクトリで実行
composer dump-autoload
 $data,'msg' => $msg,'code' => $code];
    }
    public function ok($data = [],$msg='  ',$code=200){
        return [ 'data' => $data,'msg' => $msg,'code' => $code ];
    }
    public function index(Request $request){
        //    redis  ip  ,        redis
        $redis=new Redis();
        $redis->connect('127.0.0.1',6379);
        $key =   $this->get_real_ip();//       ip  
        $check = $redis->exists($key);//      
        if($check){//    
            $redis->incr($key);//   1
            $count = $redis->get($key);//   
            if($count > 3){
                return $this->no('','      ,     ');
            }
        }else{
            //     300 
            $redis->set($key,0,300);//  reids  k,  0,    300 
        }

        //-------------------    -------------------//
        //1.   
        $app_key = 'XXXXXXXXXXXXXXXXXXX';
        $master_secret = 'XXXXXXXXXXXXXX';
        $temp_id = '1';//       id
       // $temp_para = ['test' => 'jiguang'];
        $client = new JSMS($app_key, $master_secret);
        //-------------------    -------------------//
         
            $data = $request->all();
            $phone = $data['phone'];
            //$msg_id:       sendCode           msg_id      
            //2.    
            $client = new JSMS($app_key, $master_secret, [ 'disable_ssl' => true ]);
            //3.       
            $data = $client->sendCode($phone, $temp_id);
            $msg_id = $data['body']['msg_id'];//               id
          return $this->ok($msg_id);
    }

    function check($msg_id,$code){
        $app_key = 'XXXXXXXX';
        $master_secret = 'XXXXXXXXXXXXXXX';
        $client = new JSMS($app_key, $master_secret, [ 'disable_ssl' => true ]);
        $check =  $client->checkCode($msg_id, $code);// id            
        $is_ok=$check['http_code'];
       return $is_ok;//     
    }


    //       ip  
    function get_real_ip(){
        static $realip;
        if(isset($_SERVER)){
            if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
                $realip=$_SERVER['HTTP_X_FORWARDED_FOR'];
            }else if(isset($_SERVER['HTTP_CLIENT_IP'])){
                $realip=$_SERVER['HTTP_CLIENT_IP'];
            }else{
                $realip=$_SERVER['REMOTE_ADDR'];
            }
        }else{
            if(getenv('HTTP_X_FORWARDED_FOR')){
                $realip=getenv('HTTP_X_FORWARDED_FOR');
            }else if(getenv('HTTP_CLIENT_IP')){
                $realip=getenv('HTTP_CLIENT_IP');
            }else{
                $realip=getenv('REMOTE_ADDR');
            }
        }
        return $realip;
    }
}

フロントエンドはajax要求送信検証コードを利用してmsg_を取得するid,フォームのコミット時にcheck()メソッドのmsg_への転送を要求するidおよび検証コードで校正可能
初めて文章を書くもし間違いがあったら、皆さん、ご容赦ください.