PHP code example of teemao / wechat-jssdk
1. Go to this page and download the library: Download teemao/wechat-jssdk 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/ */
teemao / wechat-jssdk example snippets
namespace App\Http\Controllers;
use Teemao\WechatJssdk\Wechat;
class WechatController
{
protected $config = [
'appId' => 'xxxxxxxxxxxxx',
'appSecret' => 'xxxxxxxxxxxxxxxxxxxxx',
];
// 获取用户信息
public function getUserInfo(string $code)
{
$wechat = new Wechat($this->config);
$refreshToken = $wechat->getRefreshToken($code);
$openId = $wechat->getOpenid($refreshToken);
$accessResult = $wechat->getAccessToken();
$userInfo = $wechat->getUserInfo($accessResult['access_token'], $openId);
return $userInfo;
}
// 获取JSAPI配置
public function getJsapiConfig()
{
$wechat = new Wechat($this->config);
$accessResult = $wechat->getAccessToken();
$ticketResult = $wechat->getTicket($accessResult['access_token']);
$result = $wechat->getJsapiConfig($ticketResult['ticket']);
return $result;
}
}