首页 » phpcode » swoole异步redis使用密码认证登陆的php代码

swoole异步redis使用密码认证登陆的php代码

 
<?php
class Task_card{

    /**
     * @var int
     */
    public $httpCode        = 0;

    /**
     * @var string
     */
    public $error_status    = "";

    /**
     * @author  xiaohuihui  <xzh_tx@163.com>
     * @param array $HttpData
     * @param array $bodyData
     * @param string $parseType
     * @return mixed
     */
    public function curl($HttpData=array(), $bodyData=array() ,$parseType = ""){
        $HttpData=array_merge(array(
            'url'=>"",
            'method'=>"GET",
            'connectTimeOut'=>10,
            'CURLOPT_RETURNTRANSFER'=>1,
            'httpheader'=>[],
        ),$HttpData);

        $curlary = array(
            CURLOPT_RETURNTRANSFER=>$HttpData['CURLOPT_RETURNTRANSFER'],
            CURLOPT_CONNECTTIMEOUT=>10,
            CURLOPT_URL=>$HttpData['url'],
            CURLOPT_HTTPHEADER=>$HttpData['httpheader'],
        );
        /* {{{ 方法参数解析
         */
        if($HttpData['method'] === "POST"){
            $curlary[CURLOPT_POST] = 1;
            if($parseType == 'from'){
                $curlary[CURLOPT_POSTFIELDS] = http_build_query($bodyData);
            } elseif ($parseType == 'json'){
                $curlary[CURLOPT_POSTFIELDS] = json_encode($bodyData);
            } else {
                $curlary[CURLOPT_POSTFIELDS] = $bodyData;
            }
        }elseif ($HttpData['method'] == "GET"){

            $url = $HttpData['url'].(empty($bodyData) ? "" : "?".http_build_query($bodyData));
            $curlary[CURLOPT_URL]=$url;
        }
        /* }}} */

        /* {{{ 开始执行curl
         */
        $curl = curl_init();
        curl_setopt_array($curl, $curlary);
        $response = curl_exec($curl);
        $this->error_status = curl_error($curl);
        $this->httpCode     = curl_getinfo($curl,CURLINFO_HTTP_CODE);
        curl_close($curl);
        /* }}} */

        return $response;
    }


    public function PageExpireCard(){
        $redis_config = SConfig::getConfig(ROOT_CONFIG."/redis.ini","default");
        $client = new \swoole_redis;
        $client->on('message', function (\swoole_redis $client, $result) {

            /**
            Array
            (
            [0] => pmessage
            [1] => __keyevent@0__:expired
            [2] => __keyevent@0__:expired
            [3] => card:expire:remark:3:8
            )
             */
            $common_config = SConfig::getConfig(ROOT_CONFIG."/common.conf","TASK");
            if(!empty($result[3]) && stripos($result[3],$common_config->match_key) !==false){
                //echo $result[3];//card:expire:remark:3:8
                preg_match('/'.$common_config->match_key.'(\d+):(\d+)/',$result[3],$ret);

                $uid = !empty($ret[1]) ? $ret[1]:0;
                $fid = !empty($ret[2]) ? $ret[2]:0;
                if($uid > 0 && $fid > 0){
                    $bodyData = [
                        'uid'=>$uid,
                        'fid'=>$fid,
                        'authkey'=>$common_config->authkey,
                    ];
                    $HttpData = [
                        'url'   =>$common_config->callback_url,
                        'method'=>"GET",
                        'httpheader'=>0,
                    ];
                    $ret = $this->curl($HttpData,$bodyData);

                    $this->debug($ret);
                }

            }
        });
        //核心代码。。。。。。。
        $client->connect($redis_config->host, $redis_config->port, function (\swoole_redis $client, $result) {
            $redis_config = SConfig::getConfig(ROOT_CONFIG."/redis.ini","default");

            if(isset($redis_config->pwd) && $redis_config->pwd!=""){
                $client->auth($redis_config->pwd, function($redis, $result) {

                    $redis->psubscribe('__keyevent@0__:expired');
                });
            }else{
                $client->psubscribe('__keyevent@0__:expired');
            }
            $common_config = SConfig::getConfig(ROOT_CONFIG."/common.conf","TASK");
            $debug = [
                'redis_host'=>$redis_config->host,
                'redis_port'=>$redis_config->port,
                'redis_pwd'=>isset($redis_config->pwd) ? $redis_config->pwd : '',
                'connect'=>"ok",
                'callbackUrl'=>$common_config->callback_url,
                'authkey'=>$common_config->authkey,
            ];

            $this->debug($debug);
        });
    }

    public function debug($data=[]){
        print_r($data);
    }
}

原文链接:swoole异步redis使用密码认证登陆的php代码,转载请注明来源!

0