PHP code example of cyd622 / wechat-app

1. Go to this page and download the library: Download cyd622/wechat-app 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/ */

    

cyd622 / wechat-app example snippets


WechatApp\WechatAppServiceProvider::class,

...

use WechatApp\WechatAppAuth;

class WechatAppController extends Controller
{
    protected $WechatApp;

    function __construct(WechatAppAuth $WechatApp)
    {
        $this->WechatApp = $WechatApp;
    }

    
    public function getWxUserInfo()
    {
        //code 在小程序端使用 wx.login 获取
        $code = request('code', '');
        //encryptedData 和 iv 在小程序端使用 wx.getUserInfo 获取
        $encryptedData = request('encryptedData', '');
        $iv = request('iv', '');

        //根据 code 获取用户 session_key 等信息, 返回用户openid 和 session_key
        $userInfo = $this->WechatApp->getLoginInfo($code);

        //获取解密后的用户信息
        return $this->WechatApp->getUserInfo($encryptedData, $iv);
    }
}
bash
php artisan vendor:publish --tag=wechatApp