微信OAuth登入SDK
把下面的配置加入你的composer.json
文件
"henter/wechat-oauth" : " dev-master "
然後使用Composer來安裝SDK
composer install
如果Packagist故障或不可用導致無法安裝SDK的,可以使用Satis或Artifact來進行本機安裝,詳見Composer文件中的Repositories
複製lib/Henter/WeChat
到專案目錄,然後require "/path/to/sdk/OAuth.php"
如果你用Composer來安裝,可以用以下程式碼自動載入
require ' vendor/autoload.php ' ;
SDK位於全域命名空間下。
use Henter WeChat OAuth
實例化OAuth
即可完成初始化
$ oauth = new Henter WeChat OAuth ( $ appid , $ secret );
$appid
和$secret
是微信開放平台的應用的唯一標識和秘鑰AppSecret
$ oauth = new Henter WeChat OAuth ( $ appid , $ secret );
$ callback_url = ' http://your_site.com/your_callback_url ' ;
$ url = $ oauth -> getAuthorizeURL ( $ callback_url );
重定向到$url
,待使用者允許授權後,將會重定向到$callback_url
上,並且帶上code
和state
參數(範例程式碼未傳入state
參數)
預設授權位址是跳到微信掃描二維碼頁面(適用於PC端),如果使用者在微信內造訪網頁點微信登陸,這種方式不太適合。 需要用下面的方法來取得微信內的授權地址:
$ url = $ oauth -> getWeChatAuthorizeURL ( $ callback_url );
註:這個在微信開放平台文件上沒有(只在公眾號平台文件中提到),不過測試發現同樣適用於開放平台應用。
code
參數取得access_token
//获取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 ();
}
如果取得成功,需保存這4個值用於後續介面調用,否則透過$oauth->error()
取得錯誤訊息
access_token
呼叫API $ oauth = new Henter WeChat OAuth ( $ appid , $ secret , $ access_token );
或
$ oauth = new Henter WeChat OAuth ( $ appid , $ secret );
$ oauth -> setAccessToken ( $ access_token );
呼叫用戶訊息,需傳入openid
$ userinfo = $ oauth -> api ( ' sns/userinfo ' , array ( ' openid ' => $ openid ));
其中sns/userinfo
為api類型,具體請參考微信API文檔
refresh_token
刷新或續期access_token
$ oauth = new Henter WeChat OAuth ( $ appid , $ secret );
//以下两种方式一样
$ access_token = $ oauth -> getAccessToken ( ' token ' , $ refresh_token );
或
$ access_token = $ oauth -> refreshAccessToken ( $ refresh_token );
此時可以透過$oauth->getRefreshToken()
取得新的refresh_token
本SDK無任何拋異常部分,如果呼叫$oauth->getAccessToken()
或$oauth->api()
等方法時返回false
則表示未成功,錯誤訊息均透過$oauth->error()
獲取,所以無需使用try {} catch {}
方式處理錯誤
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 , copyge , 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, FIEVTNESS FOR A PPYLAR PURURC IGHT NON FRING] 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