WeChat OAuth login SDK
Add the following configuration to your composer.json
file
"henter/wechat-oauth" : " dev-master "
Then use Composer to install the SDK
composer install
If Packagist fails or is unavailable and the SDK cannot be installed, you can use Satis or Artifact to install it locally. For details, see Repositories in the Composer documentation.
Copy lib/Henter/WeChat
to the project directory, and then require "/path/to/sdk/OAuth.php"
If you use Composer to install, you can use the following code to automatically load
require ' vendor/autoload.php ' ;
The SDK is located under the global namespace.
use Henter WeChat OAuth
Instantiate OAuth
to complete the initialization
$ oauth = new Henter WeChat OAuth ( $ appid , $ secret );
$appid
and $secret
are the unique identification and secret key AppSecret of the WeChat open platform application
$ oauth = new Henter WeChat OAuth ( $ appid , $ secret );
$ callback_url = ' http://your_site.com/your_callback_url ' ;
$ url = $ oauth -> getAuthorizeURL ( $ callback_url );
Redirect to $url
. After the user allows authorization, it will redirect to $callback_url
with code
and state
parameters (the sample code does not pass in state
parameter)
The default authorization address is to jump to the WeChat scan QR code page (applicable to PC). This method is not suitable if the user accesses the web page within WeChat and clicks WeChat login. You need to use the following method to obtain the authorized address for use in WeChat:
$ url = $ oauth -> getWeChatAuthorizeURL ( $ callback_url );
Note: This is not mentioned in the WeChat open platform documentation (it is only mentioned in the official account platform documentation), but the test found that it is also applicable to open platform applications.
access_token
through code
parameter //获取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 ();
}
If the acquisition is successful, these 4 values need to be saved for subsequent interface calls, otherwise the error information is obtained through $oauth->error()
access_token
$ oauth = new Henter WeChat OAuth ( $ appid , $ secret , $ access_token );
or
$ oauth = new Henter WeChat OAuth ( $ appid , $ secret );
$ oauth -> setAccessToken ( $ access_token );
To call user information, openid
needs to be passed in
$ userinfo = $ oauth -> api ( ' sns/userinfo ' , array ( ' openid ' => $ openid ));
Among them, sns/userinfo
is the api type. For details, please refer to the WeChat API document.
access_token
via refresh_token
$ oauth = new Henter WeChat OAuth ( $ appid , $ secret );
//以下两种方式一样
$ access_token = $ oauth -> getAccessToken ( ' token ' , $ refresh_token );
或
$ access_token = $ oauth -> refreshAccessToken ( $ refresh_token );
At this time, you can obtain the new refresh_token
through $oauth->getRefreshToken()
This SDK does not have any exception-throwing part. If false
is returned when calling methods such as $oauth->getAccessToken()
or $oauth->api()
, it means failure. The error information is obtained through $oauth->error()
, so no need to Use try {} catch {}
to handle errors
The MIT License (MIT) Copyright (c) 2014 Henter <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge , publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.