thinkphp 5アクセスオーロラプッシュの実現方式

22237 ワード

composerインストール
PHPプロジェクトディレクトリに入り、実行
composer require jpush/jpush

インストール成功後tp 5テスト

namespace app\jpush\controller;

class IndexController
{
     
    static $app_key="";
    static $master_secret="";
    /*
     *     
     */
    public function test() {
     
        $app_key=self::$app_key;
        $master_secret=self::$master_secret;
        $client = new \JPush\Client($app_key,$master_secret);
        $pusher = $client->push();
        $pusher->setPlatform('all');
        $pusher->addAllAudience();
        $pusher->setNotificationAlert('test,JPush---'.date("Y-m-d H:i:s"));
        try {
     
            $res=$pusher->send();
            var_dump($res);
        } catch (\JPush\Exceptions\JPushException $e) {
     
            print $e;
        }
    }
}

git cloneからpathvendorにダウンロード
requireはautoloadを導入した.phpファイル

namespace app\portal\controller;


require_once VENDOR_PATH."jpush/jpush/autoload.php";

class IndexController
{
     
    static $app_key="";
    static $master_secret="";
    
    public function test() {
     
        $app_key=self::$app_key;
        $master_secret=self::$master_secret;
        $client = new \JPush\Client($app_key,$master_secret);
        $pusher = $client->push();
        $pusher->setPlatform('all');
        $pusher->addAllAudience();
        $pusher->setNotificationAlert('test,JPush---'.date("Y-m-d H:i:s"));
        try {
     
            $res=$pusher->send();
            var_dump($res);
        } catch (\JPush\Exceptions\JPushException $e) {
     
            print $e;
        }

    }
}


公式PHPSDKを使用せず、直接REST apiインタフェース呼び出し
public function test(){
     
        $url = "https://bjapi.push.jiguang.cn/v3/push";
        $data['platform'] = 'all';
        $data['audience'] = 'all';
        //message:          App,   App     
        $data['message'] = array(
            'msg_content'=>json_encode(array('id'=>1))
        );
        //           ,               "alert"。
        $extras = json_encode(array('id'=>1));
        $data['notification'] = array(
            'android'=>array('alert'=>"test,JPush---".date("Y-m-d H:i:s"), 'extras'=>json_decode($extras, true)),
            'ios'=>array('alert'=>"test,JPush---".date("Y-m-d H:i:s"), 'extras'=>json_decode($extras, true))
        );
        $authup=self::$app_key.":".self::$master_secret;
        $headers[]="Content-Type:application/json";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);//    1       , 0     
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//    1  , 0  echo
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_USERPWD, $authup);// Authorization     
        curl_exec($curl);
        curl_close($curl);
    }