国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home 類庫下載 PHP類庫 Use the parallel method of PHP reloading and curl to elegantly encapsulate Xiaomi push SDK

Use the parallel method of PHP reloading and curl to elegantly encapsulate Xiaomi push SDK

Oct 15, 2016 pm 04:27 PM

Some time ago, we migrated some Xiaomi push code, which was previously the responsibility of others. After reading the code, I discovered two points:

The implementation of all interfaces is basically the same except for the url and parameters.

Android and ios devices need to be pushed once respectively.

I just learned about the concept of PHP reloading during this time. [Dynamic creation of types and methods is different from the concept of java and other parameters with different method names being the same. The specific concept can be RTFM] and the concurrency method of curl, so

For the first point above: Can we use the reuse of php? Load concept, use the magic method __call() to dynamically change the url parameters and request parameters of the interface call to implement calls to different interfaces pushed by Xiaomi. In other words, all our calling logic is implemented in the __call method. , but provides external calling methods for Xiaomi push to different interfaces, which will greatly simplify the code

For the second point above: Can we use the parallel mode of curl to push to android and ios devices at once, so that theoretically we can reduce The time of a push call, because we no longer need to wait for the push to one type of device to be pushed to another type of device

Next, we start to write code. First, construct a mipush entity. The member attributes of the entity include: the running environment, some initialized configuration parameters, and the information of the Xiaomi interface implemented (interface uri and parameters, etc.)

    /**
     * 運(yùn)行環(huán)境 develop:開發(fā)環(huán)境 product:生產(chǎn)環(huán)境
     * @var string
     */
    private $_environment = 'develop';

    /**
     * 設(shè)備系統(tǒng)類型 android ios
     * @var string
     */
    private $_osType      = '';

    /**
     * 小米推送域名
     * @var string
     */
    private $_host        = '';

    /**
     * 請求頭
     * @var string
     */
    private $_headers     = '';

    /**
     * 接口url
     * @var string
     */
    private $_url         = '';

    /**
     * 調(diào)用的接口方法名稱
     * @var array
     */
    private $_function    = [];

    /**
     * 請求參數(shù)
     * @var array
     */
    private $_data        = [];

    /**
     * 小米推送設(shè)置
     * @var array
     */
    private $_options = [
        'title'                   => '消息通知自定義title',
        'restricted_package_name' => '',
        'pass_through'            => 0, // 0 通知欄消息 1 透傳
        'notify_type'             => -1, // -1:默認(rèn)所有,1:使用默認(rèn)提示音提示,2:使用默認(rèn)震動提示,4:使用默認(rèn)led燈光提示
        'time_to_send'             => 0, // 定時推送 單位毫秒 默認(rèn)0
    ];

    /**
     * 運(yùn)行環(huán)境配置
     * @var array
     */
    private static $_environmentConfig = [
        'domain' => [
            'product'  => 'https://api.xmpush.xiaomi.com/',
            'develop'  => 'https://sandbox.xmpush.xiaomi.com/'
        ],
    ];

    /**
     * 小米推送接口信息定義
     * 
     * url/請求參數(shù)
     * @var array
     */
    private $_functionDefine = [
        'regid' => [
            'uri' => 'v3/message/regid',
            'arguments' => [
                'registration_id' => [
                    'type' => 'array',
                    'must' => 'y'
                ],
                'description' => [
                    'type' => 'string',
                    'must' => 'y'
                ],
                'params' => [//自定義參數(shù)
                    'type' => 'array',
                    'must' => 'n'
                ],
            ]
        ],
        'userAccount' => [
            'uri' => 'v2/message/user_account',
            'arguments' => [
                'user_account' => [
                    'type' => 'array',
                    'must' => 'y'
                ],
                'description' => [
                    'type' => 'string',
                    'must' => 'y'
                ],
                'params' => [//自定義參數(shù)
                    'type' => 'array',
                    'must' => 'n'
                ],
            ]
        ],
        'alias' => [
            'uri' => 'v3/message/alias',
            'arguments' => [
                'alias' => [
                    'type' => 'array',
                    'must' => 'y'
                ],
                'description' => [
                    'type' => 'string',
                    'must' => 'y'
                ],
                'params' => [//自定義參數(shù)
                    'type' => 'array',
                    'must' => 'n'
                ],
            ]
        ],
        'topic' => [
            'uri' => 'v3/message/topic',
            'arguments' => [
                'topics' => [
                    'type' => 'array',
                    'must' => 'y'
                ],
                'description' => [
                    'type' => 'string',
                    'must' => 'y'
                ],
                'params' => [//自定義參數(shù)
                    'type' => 'array',
                    'must' => 'n'
                ],
            ]
        ],
        'multiTopic' => [
            'uri' => 'v3/message/multi_topic',
            'arguments' => [
                'topics' => [
                    'type' => 'array',
                    'must' => 'y'
                ],
                'topics_op' => [// UNION并集,INTERSECTION交集,EXCEPT差集
                    'type' => 'string',
                    'must' => 'y'
                ],
                'description' => [
                    'type' => 'string',
                    'must' => 'y'
                ],
                'params' => [//自定義參數(shù)
                    'type' => 'array',
                    'must' => 'n'
                ],
            ]
        ],
        'all' => [
            'uri' => 'v3/message/all',
            'arguments' => [
                'description' => [
                    'type' => 'string',
                    'must' => 'y'
                ],
                'params' => [//自定義參數(shù)
                    'type' => 'array',
                    'must' => 'n'
                ],
            ]
        ],
    ];

The constructor of the mipush entity: implements a series of Configuration initialization

/**
     * 初始化配置
     * 
     * @param $string $os_type 系統(tǒng)類型
     * @param $string $config  配置
     * @param array   $options 設(shè)置 [
     *                        'title'        => 'string,標(biāo)題', 
     *                        'pass_through' => 'tinyint,0通知欄消息,1透傳,默認(rèn)0'
     *                        'notify_type'  => 'tinyint,-1,1,2,3,4',
     *                        'time_to_send' => 'int, 定時推送, 毫秒'
     *                        ]
     * @param string  $environment 環(huán)境
     */
    public function __construct($os_type='', $config=array(), $options=array(), $environment='')
    {
        /* init environment */
        if ($environment) {
            $this->_environment = $environment;
        }
        if ($os_type === 'ios') {
            $this->_host     = self::$_environmentConfig['domain'][$this->_environment];// ios
        }else{
            $this->_host     = self::$_environmentConfig['domain']['product'];// android
        }
        
        /* init option */
        $this->_headers   = [];
        $this->_headers[] = 'Authorization: key=' . $config['secret'];
        if ($os_type === 'android') {
            $this->_options['restricted_package_name'] = $config['package_name'];
        }
        foreach ($this->_options as $k => &$v) {
            if (in_array($k, $options)) {
                $v = $options[$k];
            }
        }
    }

Mipush entity’s magic method __call: dynamically implement parameter verification and calling of the Xiaomi push interface, so that we can implement a new Xiaomi push interface and implement configuration in the future.

/**
     * 魔術(shù)方法
     * 
     * 重載對象方法
     * @param  string $name      小米推送方法名稱
     * @param  array  $arguments 請求參數(shù)
     * @return mixed             void||object
     */
    public function __call($name,$arguments)
    {
        $arguments = $arguments[0];
        $this->_function = $this->_functionDefine[$name];
        $this->_url = $this->_host . $this->_function['uri'];
        $this->dataCheck($arguments);

        switch ($name) {
            case 'regid':
                $this->_data['registration_id'] = $arguments['registration_id'];
                break;
            case 'userAccount':
                $this->_data['user_account'] = implode(',', $arguments['user_account']);
                break;
            case 'alias':
                $this->_data['alias']        = implode(',', $arguments['alias']);
                break;
            case 'topic':
                $this->_data['topic']        = $arguments['topic'];
                break;
            case 'multiTopic':
                $this->_data['topics']       = implode(";$;", $arguments['topics']);
                $this->_data['topic_op']     = $arguments['topic_op'];
                break;
            case 'all':
                $this->_data['topics']       = implode(";$;", $topics);
                $this->_data['topic_op']     = 'UNION';
                break;
                
                default:
                throw new \Exception('Sorry, This function is useless in this version', 404);
                break;
        }

        $this->_data['description']  = $arguments['description'];
        if($arguments['params']) {
            foreach ($arguments['params'] as $k => $v) {
                $this->_data['extra.'.$k] = $v;// 自定義參數(shù)
            }
        }
        if ($this->_osType === 'android') {
            $this->_data = array_merge($this->_data, $this->_options);
        }
    }

After defining the Xiaomi push entity, we only need to use the mipush entity to instantiate and produce objects of different device types, and then use curl to initiate push in parallel.

/**
     * 并行推送
     * 
     * @param  Mipush $mipush_ios     ios端實(shí)體
     * @param  Mipush $mipush_android android端實(shí)體
     * @return array                  推送結(jié)果
     */
    private static function curlRequest(Mipush $mipush_ios, Mipush $mipush_android) 
    {
        $ch_ios     = curl_init();
        $ch_android = curl_init();
        curl_setopt($ch_ios, CURLOPT_URL, $mipush_ios->_url);
        curl_setopt($ch_ios, CURLOPT_POST, 1);
        curl_setopt($ch_ios, CURLOPT_POSTFIELDS, $mipush_ios->_data);
        curl_setopt($ch_ios, CURLOPT_HTTPHEADER, $mipush_ios->_headers);
        curl_setopt($ch_ios, CURLOPT_RETURNTRANSFER, 1); 
        
        curl_setopt($ch_android, CURLOPT_URL, $mipush_android->_url);
        curl_setopt($ch_android, CURLOPT_POST, 1);
        curl_setopt($ch_android, CURLOPT_POSTFIELDS, $mipush_android->_data);
        curl_setopt($ch_android, CURLOPT_HTTPHEADER, $mipush_android->_headers);
        curl_setopt($ch_android, CURLOPT_RETURNTRANSFER, 1); 
        
        $mh = curl_multi_init();
        curl_multi_add_handle($mh, $ch_ios);
        curl_multi_add_handle($mh, $ch_android);

        $running=null;
        do {
           curl_multi_exec($mh,$running);
        } while($running > 0);

        $result['ios']        = json_decode(curl_multi_getcontent($ch_ios), true);
        $result['android'] = json_decode(curl_multi_getcontent($ch_android), true);

        curl_multi_remove_handle($mh, $ch_ios);
        curl_multi_remove_handle($mh, $ch_android);
        curl_multi_close($mh);
        return $result;
    }

That’s it. Through the above method, we have encapsulated a Xiaomi SDK with very little code. Currently, we only implement regid (registration id), alias (alias), user_account (user account), topic (label) ), multi_topic (multi-label), all (all) push.

How to use?

I haven’t published this on packagist yet. I published this on packagist. Interested students can google it by themselves, haha~

composer require tigerb/easy-mipush

使用格式:
try {
    Push::init(
        ['secret' => 'string,必傳,ios密鑰'], 
        ['secret' => 'string,必傳,android密鑰', 'package_name' => 'string,必傳,android包名']
        [   
          'title'        => 'string,非必傳,消息通知自定義title',
          'pass_through' => 'int,非必傳,0通知欄消息,1透傳,默認(rèn)0',
          'notify_type'  => 'int,非必傳,-1:默認(rèn)所有,1:使用默認(rèn)提示音提示,2:使用默認(rèn)震動提示,4:使用默認(rèn)led燈光提示',
          'time_to_send' => 'int,非必傳,定時推送,單位毫秒,默認(rèn)0',
        ],
        'string,develop開發(fā)環(huán)境,product生產(chǎn)環(huán)境, 默認(rèn)develop'
        );  
    $res = Push::toUse('string,小米push方法名', 'array, 該方法對應(yīng)的參數(shù)');
    echo json_encode($res, JSON_UNESCAPED_UNICODE);
} catch (Exception $e) {
    echo $e->getMessage();
}

使用示例:
use Mipush\Push;

require './vendor/autoload.php';

try {
    Push::init(
        ['secret' => 'test'], 
        ['secret' => 'test', 'package_name' => 'com.test'],
        [   
          'title'        => 'test',
          'pass_through' => 0,
          'notify_type'  => -1,
          'time_to_send' => 0,
        ],
        'develop'
        );  
    $res = Push::toUse('userAccount', [
            'user_account' => [1],
            'description'  => 'test'
        ]);
    echo json_encode($res, JSON_UNESCAPED_UNICODE);
} catch (Exception $e) {
    echo $e->getMessage();
}


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276