PHP code example of blackhive / yii2-easywechat

1. Go to this page and download the library: Download blackhive/yii2-easywechat library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

blackhive / yii2-easywechat example snippets


'components' => [
	// ...
	'wechat' => [
		'class' => 'blackhive\easywechat\Wechat',
		//'openidName' => 'wx.openid',
	],
]

[
    'wechat' => [
        // 微信商户平台
        'pay' => [
            'app_id' => '',
            'mch_id' => '',
            'key' => '',
            'cert_path' => dirname(__FILE__) . '/path/to/apiclient_cert.pem', // 绝对路径!!!!
            'key_path' => dirname(__FILE__) . '/path/to/apiclient_key.pem',  // 绝对路径!!!!
            'notify_url' => '',
        ],
        // 微信公众平台
        'mp' => [
            'app_id' => '',
            'secret' => '',
            'oauth' => [
                'scopes'   => ['snsapi_base'],
                'callback' => '/wechat/oauth-callback',
            ],
        ],
        // 微信开放平台
        'open' => [
            'app_id'   => '',
            'secret'   => '',
            'token'    => '',
            'aes_key'  => ''
        ]
]

// 微信支付客户端
$payment = Yii::$app->wechat->payment;

// 微信公众号客户端
$officialAccount = Yii::$app->wechat->officialAccount;

// 微信公众平台客户端
$openPlatform = Yii::$app->wechat->openPlatform;

// 是否在微信客户端内
if (Yii::$app->wechat->isWechat()){
    // 微信内的操作
}


Yii::$app->wechat->openid = 'jfdjdjfjdaj';

// 从 session 中获取 openid
Yii::$app->wechat->openid;

// 微信网页授权:
if (Yii::$app->wechat->inWechat && !Yii::$app->wechat->openid) {// 从 session 中获取 openid
    Yii::$app->wechat->returnUrl = ['user/order', 'id' => 1];
    return Yii::$app->wechat->officialAccount->oauth->redirect()->send();
}

// 网页授权回调控制器 /wechat/oauth-callback
$oauth  = Yii::$app->wechat->officialAccount->oauth;
Yii::$app->wechat->openid = $oauth->user()->getId();// 向 session 中保存 openid
return $this->redirect(Yii::$app->wechat->returnUrl);