PHP code example of henter / wechat-oauth

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

    

henter / wechat-oauth example snippets




use Henter\WeChat\OAuth

$oauth = new \Henter\WeChat\OAuth($appid, $secret);

$oauth = new \Henter\WeChat\OAuth($appid, $secret);
$callback_url = 'http://your_site.com/your_callback_url';
$url = $oauth->getAuthorizeURL($callback_url);

$url = $oauth->getWeChatAuthorizeURL($callback_url);

//获取code参数
$code = $_GET['code'];

$oauth = new \Henter\WeChat\OAuth($appid, $secret);
if($access_token = $oauth->getAccessToken('code', $code)){
	$refresh_token = $oauth->getRefreshToken();
	$expires_in = $oauth->getExpiresIn();
	$openid = $oauth->getOpenid();
}else{
	echo $oauth->error();
}


$oauth = new \Henter\WeChat\OAuth($appid, $secret, $access_token);

$oauth = new \Henter\WeChat\OAuth($appid, $secret);
$oauth->setAccessToken($access_token);

$userinfo = $oauth->api('sns/userinfo', array('openid'=>$openid));


$oauth = new \Henter\WeChat\OAuth($appid, $secret);

//以下两种方式一样
$access_token = $oauth->getAccessToken('token', $refresh_token);
或
$access_token = $oauth->refreshAccessToken($refresh_token);