<?php
class
Wechat
{
const
MSGTYPE_TEXT =
'text'
;
const
MSGTYPE_IMAGE =
'image'
;
const
MSGTYPE_LOCATION =
'location'
;
const
MSGTYPE_LINK =
'link'
;
const
MSGTYPE_EVENT =
'event'
;
const
MSGTYPE_MUSIC =
'music'
;
const
MSGTYPE_NEWS =
'news'
;
const
MSGTYPE_VOICE =
'voice'
;
const
MSGTYPE_VIDEO =
'video'
;
const
API_URL_PREFIX =
'https://api.weixin.qq.com/cgi-bin'
;
const
AUTH_URL =
'/token?grant_type=client_credential&'
;
const
MENU_CREATE_URL =
'/menu/create?'
;
const
MENU_GET_URL =
'/menu/get?'
;
const
MENU_DELETE_URL =
'/menu/delete?'
;
const
MEDIA_GET_URL =
'/media/get?'
;
const
QRCODE_CREATE_URL=
'/qrcode/create?'
;
const
QR_SCENE = 0;
const
QR_LIMIT_SCENE = 1;
const
QRCODE_IMG_URL=
'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='
;
const
USER_GET_URL=
'/user/get?'
;
const
USER_INFO_URL=
'/user/info?'
;
const
GROUP_GET_URL=
'/groups/get?'
;
const
GROUP_CREATE_URL=
'/groups/create?'
;
const
GROUP_UPDATE_URL=
'/groups/update?'
;
const
GROUP_MEMBER_UPDATE_URL=
'/groups/members/update?'
;
const
CUSTOM_SEND_URL=
'/message/custom/send?'
;
const
OAUTH_PREFIX =
'https://open.weixin.qq.com/connect/oauth2'
;
const
OAUTH_AUTHORIZE_URL =
'/authorize?'
;
const
OAUTH_TOKEN_PREFIX =
'https://api.weixin.qq.com/sns/oauth2'
;
const
OAUTH_TOKEN_URL =
'/access_token?'
;
const
OAUTH_REFRESH_URL =
'/refresh_token?'
;
const
OAUTH_USERINFO_URL =
'https://api.weixin.qq.com/sns/userinfo?'
;
private
$token
;
private
$appid
;
private
$appsecret
;
private
$access_token
;
private
$user_token
;
private
$_msg
;
private
$_funcflag
= false;
private
$_receive
;
public
$debug
= false;
public
$errCode
= 40001;
public
$errMsg
=
"no access"
;
private
$_logcallback
;
public
function
__construct(
$options
)
{
$this
->token = isset(
$options
[
'token'
])?
$options
[
'token'
]:
''
;
$this
->appid = isset(
$options
[
'appid'
])?
$options
[
'appid'
]:
''
;
$this
->appsecret = isset(
$options
[
'appsecret'
])?
$options
[
'appsecret'
]:
''
;
$this
->debug = isset(
$options
[
'debug'
])?
$options
[
'debug'
]:false;
$this
->_logcallback = isset(
$options
[
'logcallback'
])?
$options
[
'logcallback'
]:false;
}
private
function
checkSignature()
{
$signature
= isset(
$_GET
[
"signature"
])?
$_GET
[
"signature"
]:
''
;
$timestamp
= isset(
$_GET
[
"timestamp"
])?
$_GET
[
"timestamp"
]:
''
;
$nonce
= isset(
$_GET
[
"nonce"
])?
$_GET
[
"nonce"
]:
''
;
$token
=
$this
->token;
$tmpArr
=
array
(
$token
,
$timestamp
,
$nonce
);
sort(
$tmpArr
, SORT_STRING);
$tmpStr
= implode(
$tmpArr
);
$tmpStr
= sha1(
$tmpStr
);
if
(
$tmpStr
==
$signature
){
return
true;
}
else
{
return
false;
}
}
public
function
valid(
$return
=false)
{
$echoStr
= isset(
$_GET
[
"echostr"
]) ?
$_GET
[
"echostr"
]:
''
;
if
(
$return
) {
if
(
$echoStr
) {
if
(
$this
->checkSignature())
return
$echoStr
;
else
return
false;
}
else
return
$this
->checkSignature();
}
else
{
if
(
$echoStr
) {
if
(
$this
->checkSignature())
die
(
$echoStr
);
else
die
(
'no access'
);
}
else
{
if
(
$this
->checkSignature())
return
true;
else
die
(
'no access'
);
}
}
return
false;
}
public
function
Message(
$msg
=
''
,
$append
= false){
if
(
is_null
(
$msg
)) {
$this
->_msg =
array
();
}
elseif
(
is_array
(
$msg
)) {
if
(
$append
)
$this
->_msg =
array_merge
(
$this
->_msg,
$msg
);
else
$this
->_msg =
$msg
;
return
$this
->_msg;
}
else
{
return
$this
->_msg;
}
}
public
function
setFuncFlag(
$flag
) {
$this
->_funcflag =
$flag
;
return
$this
;
}
private
function
log(
$log
){
if
(
$this
->debug && function_exists(
$this
->_logcallback)) {
if
(
is_array
(
$log
))
$log
= print_r(
$log
,true);
return
call_user_func(
$this
->_logcallback,
$log
);
}
}
public
function
getRev()
{
if
(
$this
->_receive)
return
$this
;
$postStr
=
file_get_contents
(
"php://input"
);
$this
->log(
$postStr
);
if
(!
empty
(
$postStr
)) {
$this
->_receive = (
array
)simplexml_load_string(
$postStr
,
'SimpleXMLElement'
, LIBXML_NOCDATA);
}
return
$this
;
}
public
function
getRevData()
{
return
$this
->_receive;
}
public
function
getRevFrom() {
if
(isset(
$this
->_receive[
'FromUserName'
]))
return
$this
->_receive[
'FromUserName'
];
else
return
false;
}
public
function
getRevTo() {
if
(isset(
$this
->_receive[
'ToUserName'
]))
return
$this
->_receive[
'ToUserName'
];
else
return
false;
}
public
function
getRevType() {
if
(isset(
$this
->_receive[
'MsgType'
]))
return
$this
->_receive[
'MsgType'
];
else
return
false;
}
public
function
getRevID() {
if
(isset(
$this
->_receive[
'MsgId'
]))
return
$this
->_receive[
'MsgId'
];
else
return
false;
}
public
function
getRevCtime() {
if
(isset(
$this
->_receive[
'CreateTime'
]))
return
$this
->_receive[
'CreateTime'
];
else
return
false;
}
public
function
getRevContent(){
if
(isset(
$this
->_receive[
'Content'
]))
return
$this
->_receive[
'Content'
];
else
if
(isset(
$this
->_receive[
'Recognition'
]))
return
$this
->_receive[
'Recognition'
];
else
return
false;
}
public
function
getRevPic(){
if
(isset(
$this
->_receive[
'PicUrl'
]))
return
$this
->_receive[
'PicUrl'
];
else
return
false;
}
public
function
getRevLink(){
if
(isset(
$this
->_receive[
'Url'
])){
return
array
(
'url'
=>
$this
->_receive[
'Url'
],
'title'
=>
$this
->_receive[
'Title'
],
'description'
=>
$this
->_receive[
'Description'
]
);
}
else
return
false;
}
public
function
getRevGeo(){
if
(isset(
$this
->_receive[
'Location_X'
])){
return
array
(
'x'
=>
$this
->_receive[
'Location_X'
],
'y'
=>
$this
->_receive[
'Location_Y'
],
'scale'
=>
$this
->_receive[
'Scale'
],
'label'
=>
$this
->_receive[
'Label'
]
);
}
else
return
false;
}
public
function
getRevEvent(){
if
(isset(
$this
->_receive[
'Event'
])){
return
array
(
'event'
=>
$this
->_receive[
'Event'
],
'key'
=>
$this
->_receive[
'EventKey'
],
);
}
else
return
false;
}
public
function
getRevVoice(){
if
(isset(
$this
->_receive[
'MediaId'
])){
return
array
(
'mediaid'
=>
$this
->_receive[
'MediaId'
],
'format'
=>
$this
->_receive[
'Format'
],
);
}
else
return
false;
}
public
function
getRevVideo(){
if
(isset(
$this
->_receive[
'MediaId'
])){
return
array
(
'mediaid'
=>
$this
->_receive[
'MediaId'
],
'thumbmediaid'
=>
$this
->_receive[
'ThumbMediaId'
]
);
}
else
return
false;
}
public
function
getRevTicket(){
if
(isset(
$this
->_receive[
'Ticket'
])){
return
$this
->_receive[
'Ticket'
];
}
else
return
false;
}
public
function
getRevSceneId (){
if
(isset(
$this
->_receive[
'EventKey'
])){
return
str_replace
(
'qrscene_'
,
''
,
$this
->_receive[
'EventKey'
]);
}
else
{
return
false;
}
}
public
static
function
xmlSafeStr(
$str
)
{
return
'<![CDATA['
.preg_replace(
"/[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]/"
,
''
,
$str
).
']]>'
;
}
public
static
function
data_to_xml(
$data
) {
$xml
=
''
;
foreach
(
$data
as
$key
=>
$val
) {
is_numeric
(
$key
) &&
$key
=
"item id=\"$key\""
;
$xml
.=
"<$key>"
;
$xml
.= (
is_array
(
$val
) ||
is_object
(
$val
)) ? self::data_to_xml(
$val
) : self::xmlSafeStr(
$val
);
list(
$key
, ) =
explode
(
' '
,
$key
);
$xml
.=
"</$key>"
;
}
return
$xml
;
}
public
function
xml_encode(
$data
,
$root
=
'xml'
,
$item
=
'item'
,
$attr
=
''
,
$id
=
'id'
,
$encoding
=
'utf-8'
) {
if
(
is_array
(
$attr
)){
$_attr
=
array
();
foreach
(
$attr
as
$key
=>
$value
) {
$_attr
[] =
"{$key}=\"{$value}\""
;
}
$attr
= implode(
' '
,
$_attr
);
}
$attr
= trim(
$attr
);
$attr
=
empty
(
$attr
) ?
''
:
" {$attr}"
;
$xml
=
"<{$root}{$attr}>"
;
$xml
.= self::data_to_xml(
$data
,
$item
,
$id
);
$xml
.=
"</{$root}>"
;
return
$xml
;
}
public
function
text(
$text
=
''
)
{
$FuncFlag
=
$this
->_funcflag ? 1 : 0;
$msg
=
array
(
'ToUserName'
=>
$this
->getRevFrom(),
'FromUserName'
=>
$this
->getRevTo(),
'MsgType'
=>self::MSGTYPE_TEXT,
'Content'
=>
$text
,
'CreateTime'
=>time(),
'FuncFlag'
=>
$FuncFlag
);
$this
->Message(
$msg
);
return
$this
;
}
public
function
music(
$title
,
$desc
,
$musicurl
,
$hgmusicurl
=
''
) {
$FuncFlag
=
$this
->_funcflag ? 1 : 0;
$msg
=
array
(
'ToUserName'
=>
$this
->getRevFrom(),
'FromUserName'
=>
$this
->getRevTo(),
'CreateTime'
=>time(),
'MsgType'
=>self::MSGTYPE_MUSIC,
'Music'
=>
array
(
'Title'
=>
$title
,
'Description'
=>
$desc
,
'MusicUrl'
=>
$musicurl
,
'HQMusicUrl'
=>
$hgmusicurl
),
'FuncFlag'
=>
$FuncFlag
);
$this
->Message(
$msg
);
return
$this
;
}
public
function
news(
$newsData
=
array
())
{
$FuncFlag
=
$this
->_funcflag ? 1 : 0;
$count
=
count
(
$newsData
);
$msg
=
array
(
'ToUserName'
=>
$this
->getRevFrom(),
'FromUserName'
=>
$this
->getRevTo(),
'MsgType'
=>self::MSGTYPE_NEWS,
'CreateTime'
=>time(),
'ArticleCount'
=>
$count
,
'Articles'
=>
$newsData
,
'FuncFlag'
=>
$FuncFlag
);
$this
->Message(
$msg
);
return
$this
;
}
public
function
reply(
$msg
=
array
(),
$return
= false)
{
if
(
empty
(
$msg
))
$msg
=
$this
->_msg;
$xmldata
=
$this
->xml_encode(
$msg
);
$this
->log(
$xmldata
);
if
(
$return
)
return
$xmldata
;
else
echo
$xmldata
;
}
private
function
http_get(
$url
){
$oCurl
= curl_init();
if
(
stripos
(
$url
,
"https://"
)!==FALSE){
curl_setopt(
$oCurl
, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt(
$oCurl
, CURLOPT_SSL_VERIFYHOST, FALSE);
}
curl_setopt(
$oCurl
, CURLOPT_URL,
$url
);
curl_setopt(
$oCurl
, CURLOPT_RETURNTRANSFER, 1 );
$sContent
= curl_exec(
$oCurl
);
$aStatus
= curl_getinfo(
$oCurl
);
curl_close(
$oCurl
);
if
(
intval
(
$aStatus
[
"http_code"
])==200){
return
$sContent
;
}
else
{
return
false;
}
}
private
function
http_post(
$url
,
$param
){
$oCurl
= curl_init();
if
(
stripos
(
$url
,
"https://"
)!==FALSE){
curl_setopt(
$oCurl
, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt(
$oCurl
, CURLOPT_SSL_VERIFYHOST, false);
}
if
(
is_string
(
$param
)) {
$strPOST
=
$param
;
}
else
{
$aPOST
=
array
();
foreach
(
$param
as
$key
=>
$val
){
$aPOST
[] =
$key
.
"="
.urlencode(
$val
);
}
$strPOST
= join(
"&"
,
$aPOST
);
}
curl_setopt(
$oCurl
, CURLOPT_URL,
$url
);
curl_setopt(
$oCurl
, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt(
$oCurl
, CURLOPT_POST,true);
curl_setopt(
$oCurl
, CURLOPT_POSTFIELDS,
$strPOST
);
$sContent
= curl_exec(
$oCurl
);
$aStatus
= curl_getinfo(
$oCurl
);
curl_close(
$oCurl
);
if
(
intval
(
$aStatus
[
"http_code"
])==200){
return
$sContent
;
}
else
{
return
false;
}
}
public
function
checkAuth(
$appid
=
''
,
$appsecret
=
''
){
if
(!
$appid
|| !
$appsecret
) {
$appid
=
$this
->appid;
$appsecret
=
$this
->appsecret;
}
$result
=
$this
->http_get(self::API_URL_PREFIX.self::AUTH_URL.
'appid='
.
$appid
.
'&secret='
.
$appsecret
);
if
(
$result
)
{
$json
= json_decode(
$result
,true);
if
(!
$json
|| isset(
$json
[
'errcode'
])) {
$this
->errCode =
$json
[
'errcode'
];
$this
->errMsg =
$json
[
'errmsg'
];
return
false;
}
$this
->access_token =
$json
[
'access_token'
];
$expire
=
$json
[
'expires_in'
] ?
intval
(
$json
[
'expires_in'
])-100 : 3600;
return
$this
->access_token;
}
return
false;
}
public
function
resetAuth(
$appid
=
''
){
$this
->access_token =
''
;
return
true;
}
static
function
json_encode(
$arr
) {
$parts
=
array
();
$is_list
= false;
$keys
=
array_keys
(
$arr
);
$max_length
=
count
(
$arr
) - 1;
if
((
$keys
[0] === 0) && (
$keys
[
$max_length
] ===
$max_length
)) {
$is_list
= true;
for
(
$i
= 0;
$i
<
count
(
$keys
);
$i
++) {
if
(
$i
!=
$keys
[
$i
]) {
$is_list
= false;
break
;
}
}
}
foreach
(
$arr
as
$key
=>
$value
) {
if
(
is_array
(
$value
)) {
if
(
$is_list
)
$parts
[] = self::json_encode (
$value
);
else
$parts
[] =
'"'
.
$key
.
'":'
. self::json_encode (
$value
);
}
else
{
$str
=
''
;
if
(!
$is_list
)
$str
=
'"'
.
$key
.
'":'
;
if
(
is_numeric
(
$value
) &&
$value
<2000000000)
$str
.=
$value
;
elseif
(
$value
=== false)
$str
.=
'false'
;
elseif
(
$value
=== true)
$str
.=
'true'
;
else
$str
.=
'"'
.
addslashes
(
$value
) .
'"'
;
$parts
[] =
$str
;
}
}
$json
= implode (
','
,
$parts
);
if
(
$is_list
)
return
'['
.
$json
.
']'
;
return
'{'
.
$json
.
'}'
;
}
public
function
createMenu(
$data
){
if
(!
$this
->access_token && !
$this
->checkAuth())
return
false;
$result
=
$this
->http_post(self::API_URL_PREFIX.self::MENU_CREATE_URL.
'access_token='
.
$this
->access_token,self::json_encode(
$data
));
if
(
$result
)
{
$json
= json_decode(
$result
,true);
if
(!
$json
|| !
empty
(
$json
[
'errcode'
])) {
$this
->errCode =
$json
[
'errcode'
];
$this
->errMsg =
$json
[
'errmsg'
];
return
false;
}
return
true;
}
return
false;
}
public
function
getMenu(){
if
(!
$this
->access_token && !
$this
->checkAuth())
return
false;
$result
=
$this
->http_get(self::API_URL_PREFIX.self::MENU_GET_URL.
'access_token='
.
$this
->access_token);
if
(
$result
)
{
$json
= json_decode(
$result
,true);
if
(!
$json
|| isset(
$json
[
'errcode'
])) {
$this
->errCode =
$json
[
'errcode'
];
$this
->errMsg =
$json
[
'errmsg'
];
return
false;
}
return
$json
;
}
return
false;
}
public
function
deleteMenu(){
if
(!
$this
->access_token && !
$this
->checkAuth())
return
false;
$result
=
$this
->http_get(self::API_URL_PREFIX.self::MENU_DELETE_URL.
'access_token='
.
$this
->access_token);
if
(
$result
)
{
$json
= json_decode(
$result
,true);
if
(!
$json
|| !
empty
(
$json
[
'errcode'
])) {
$this
->errCode =
$json
[
'errcode'
];
$this
->errMsg =
$json
[
'errmsg'
];
return
false;
}
return
true;
}
return
false;
}
public
function
getMedia(
$media_id
){
if
(!
$this
->access_token && !
$this
->checkAuth())
return
false;
$result
=
$this
->http_get(self::API_URL_PREFIX.self::MEDIA_GET_URL.
'access_token='
.
$this
->access_token.
'&media_id='
.
$media_id
);
if
(
$result
)
{
$json
= json_decode(
$result
,true);
if
(isset(
$json
[
'errcode'
])) {
$this
->errCode =
$json
[
'errcode'
];
$this
->errMsg =
$json
[
'errmsg'
];
return
false;
}
return
$json
;
}
return
false;
}
public
function
getQRCode(
$scene_id
,
$type
=0,
$expire
=1800){
if
(!
$this
->access_token && !
$this
->checkAuth())
return
false;
$data
=
array
(
'action_name'
=>
$type
?
"QR_LIMIT_SCENE"
:
"QR_SCENE"
,
'expire_seconds'
=>
$expire
,
'action_info'
=>
array
(
'scene'
=>
array
(
'scene_id'
=>
$scene_id
))
);
if
(
$type
== 1) {
unset(
$data
[
'expire_seconds'
]);
}
$result
=
$this
->http_post(self::API_URL_PREFIX.self::QRCODE_CREATE_URL.
'access_token='
.
$this
->access_token,self::json_encode(
$data
));
if
(
$result
)
{
$json
= json_decode(
$result
,true);
if
(!
$json
|| !
empty
(
$json
[
'errcode'
])) {
$this
->errCode =
$json
[
'errcode'
];
$this
->errMsg =
$json
[
'errmsg'
];
return
false;
}
return
$json
;
}
return
false;
}
public
function
getQRUrl(
$ticket
) {
return
self::QRCODE_IMG_URL.
$ticket
;
}
public
function
getUserList(
$next_openid
=
''
){
if
(!
$this
->access_token && !
$this
->checkAuth())
return
false;
$result
=
$this
->http_get(self::API_URL_PREFIX.self::USER_GET_URL.
'access_token='
.
$this
->access_token.
'&next_openid='
.
$next_openid
);
if
(
$result
)
{
$json
= json_decode(
$result
,true);
if
(isset(
$json
[
'errcode'
])) {
$this
->errCode =
$json
[
'errcode'
];
$this
->errMsg =
$json
[
'errmsg'
];
return
false;
}
return
$json
;
}
return
false;
}
public
function
getUserInfo(
$openid
){
if
(!
$this
->access_token && !
$this
->checkAuth())
return
false;
$result
=
$this
->http_get(self::API_URL_PREFIX.self::USER_INFO_URL.
'access_token='
.
$this
->access_token.
'&openid='
.
$openid
);
if
(
$result
)
{
$json
= json_decode(
$result
,true);
if
(isset(
$json
[
'errcode'
])) {
$this
->errCode =
$json
[
'errcode'
];
$this
->errMsg =
$json
[
'errmsg'
];
return
false;
}
return
$json
;
}
return
false;
}
public
function
getGroup(){
if
(!
$this
->access_token && !
$this
->checkAuth())
return
false;
$result
=
$this
->http_get(self::API_URL_PREFIX.self::GROUP_GET_URL.
'access_token='
.
$this
->access_token);
if
(
$result
)
{
$json
= json_decode(
$result
,true);
if
(isset(
$json
[
'errcode'
])) {
$this
->errCode =
$json
[
'errcode'
];
$this
->errMsg =
$json
[
'errmsg'
];
return
false;
}
return
$json
;
}
return
false;
}
public
function
createGroup(
$name
){
if
(!
$this
->access_token && !
$this
->checkAuth())
return
false;
$data
=
array
(
'group'
=>
array
(
'name'
=>
$name
)
);
$result
=
$this
->http_post(self::API_URL_PREFIX.self::GROUP_CREATE_URL.
'access_token='
.
$this
->access_token,self::json_encode(
$data
));
if
(
$result
)
{
$json
= json_decode(
$result
,true);
if
(!
$json
|| !
empty
(
$json
[
'errcode'
])) {
$this
->errCode =
$json
[
'errcode'
];
$this
->errMsg =
$json
[
'errmsg'
];
return
false;
}
return
$json
;
}
return
false;
}
public
function
updateGroup(
$groupid
,
$name
){
if
(!
$this
->access_token && !
$this
->checkAuth())
return
false;
$data
=
array
(
'group'
=>
array
(
'id'
=>
$groupid
,
'name'
=>
$name
)
);
$result
=
$this
->http_post(self::API_URL_PREFIX.self::GROUP_UPDATE_URL.
'access_token='
.
$this
->access_token,self::json_encode(
$data
));
if
(
$result
)
{
$json
= json_decode(
$result
,true);
if
(!
$json
|| !
empty
(
$json
[
'errcode'
])) {
$this
->errCode =
$json
[
'errcode'
];
$this
->errMsg =
$json
[
'errmsg'
];
return
false;
}
return
$json
;
}
return
false;
}
public
function
updateGroupMembers(
$groupid
,
$openid
){
if
(!
$this
->access_token && !
$this
->checkAuth())
return
false;
$data
=
array
(
'openid'
=>
$openid
,
'to_groupid'
=>
$groupid
);
$result
=
$this
->http_post(self::API_URL_PREFIX.self::GROUP_MEMBER_UPDATE_URL.
'access_token='
.
$this
->access_token,self::json_encode(
$data
));
if
(
$result
)
{
$json
= json_decode(
$result
,true);
if
(!
$json
|| !
empty
(
$json
[
'errcode'
])) {
$this
->errCode =
$json
[
'errcode'
];
$this
->errMsg =
$json
[
'errmsg'
];
return
false;
}
return
$json
;
}
return
false;
}
public
function
sendCustomMessage(
$data
){
if
(!
$this
->access_token && !
$this
->checkAuth())
return
false;
$result
=
$this
->http_post(self::API_URL_PREFIX.self::CUSTOM_SEND_URL.
'access_token='
.
$this
->access_token,self::json_encode(
$data
));
if
(
$result
)
{
$json
= json_decode(
$result
,true);
if
(!
$json
|| !
empty
(
$json
[
'errcode'
])) {
$this
->errCode =
$json
[
'errcode'
];
$this
->errMsg =
$json
[
'errmsg'
];
return
false;
}
return
$json
;
}
return
false;
}
public
function
getOauthRedirect(
$callback
,
$state
=
''
,
$scope
=
'snsapi_userinfo'
){
return
self::OAUTH_PREFIX.self::OAUTH_AUTHORIZE_URL.
'appid='
.
$this
->appid.
'&redirect_uri='
.urlencode(
$callback
).
'&response_type=code&scope='
.
$scope
.
'&state='
.
$state
.
'#wechat_redirect'
;
}
public
function
getOauthAccessToken(){
$code
= isset(
$_GET
[
'code'
])?
$_GET
[
'code'
]:
''
;
if
(!
$code
)
return
false;
$result
=
$this
->http_get(self::OAUTH_TOKEN_PREFIX.self::OAUTH_TOKEN_URL.
'appid='
.
$this
->appid.
'&secret='
.
$this
->appsecret.
'&code='
.
$code
.
'&grant_type=authorization_code'
);
if
(
$result
)
{
$json
= json_decode(
$result
,true);
if
(!
$json
|| !
empty
(
$json
[
'errcode'
])) {
$this
->errCode =
$json
[
'errcode'
];
$this
->errMsg =
$json
[
'errmsg'
];
return
false;
}
$this
->user_token =
$json
[
'access_token'
];
return
$json
;
}
return
false;
}
public
function
getOauthRefreshToken(
$refresh_token
){
$result
=
$this
->http_get(self::OAUTH_TOKEN_PREFIX.self::OAUTH_REFRESH_URL.
'appid='
.
$this
->appid.
'&grant_type=refresh_token&refresh_token='
.
$refresh_token
);
if
(
$result
)
{
$json
= json_decode(
$result
,true);
if
(!
$json
|| !
empty
(
$json
[
'errcode'
])) {
$this
->errCode =
$json
[
'errcode'
];
$this
->errMsg =
$json
[
'errmsg'
];
return
false;
}
$this
->user_token =
$json
[
'access_token'
];
return
$json
;
}
return
false;
}
public
function
getOauthUserinfo(
$access_token
,
$openid
){
$result
=
$this
->http_get(self::OAUTH_USERINFO_URL.
'access_token='
.
$access_token
.
'&openid='
.
$openid
);
if
(
$result
)
{
$json
= json_decode(
$result
,true);
if
(!
$json
|| !
empty
(
$json
[
'errcode'
])) {
$this
->errCode =
$json
[
'errcode'
];
$this
->errMsg =
$json
[
'errmsg'
];
return
false;
}
return
$json
;
}
return
false;
}
}