<?php
define(
"TOKEN"
,
"chenxiang"
);
define(
'APP_ID'
,
''
);
define(
'APP_SECRET'
,
''
);
$wechatObj
=
new
wechatCallbackapiTest(APP_ID,APP_SECRET);
$wechatObj
->Run();
class
wechatCallbackapiTest
{
private
$fromUsername
;
private
$toUsername
;
private
$times
;
private
$keyword
;
private
$app_id
;
private
$app_secret
;
public
function
__construct(
$appid
,
$appsecret
)
{
# code...
$this
->app_id =
$appid
;
$this
->app_secret =
$appsecret
;
}
public
function
valid()
{
$echoStr
=
$_GET
[
"echostr"
];
if
(
$this
->checkSignature()){
echo
$echoStr
;
exit
;
}
}
public
function
Run()
{
$this
->responseMsg();
$arr
[]=
"您好,这是自动回复,我现在不在,有事请留言,我会尽快回复你的^_^"
;
echo
$this
->make_xml(
"text"
,
$arr
);
}
public
function
responseMsg()
{
$postStr
=
$GLOBALS
[
"HTTP_RAW_POST_DATA"
];
if
(!
empty
(
$postStr
)){
$access_token
=
$this
->get_access_token();
$this
->createmenu(
$access_token
);
$postObj
= simplexml_load_string(
$postStr
,
'SimpleXMLElement'
, LIBXML_NOCDATA);
$this
->fromUsername =
$postObj
->FromUserName;
$this
->toUsername =
$postObj
->ToUserName;
$this
->keyword = trim(
$postObj
->Content);
$this
->times = time();
$MsgType
=
$postObj
->MsgType;
if
(
$MsgType
==
'event'
){
$MsgEvent
=
$postObj
->Event;
if
(
$MsgEvent
==
'subscribe'
) {
$arr
[] =
"你好,我是xxx,现在我们是好友咯![愉快][玫瑰]"
;
echo
$this
->make_xml(
"text"
,
$arr
);
exit
;
}
elseif
(
$MsgEvent
==
'CLICK'
) {
$EventKey
=
$postObj
->EventKey;
$arr
[] =
$EventKey
;
echo
$this
->make_xml(
"text"
,
$arr
);
exit
;
}
}
}
else
{
echo
"this a file for weixin API!"
;
exit
;
}
}
private
function
get_access_token()
{
$url
=
"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
.
$this
->app_id.
"&secret="
.
$this
->app_secret;
$data
= json_decode(
file_get_contents
(
$url
),true);
if
(
$data
[
'access_token'
]){
return
$data
[
'access_token'
];
}
else
{
return
"获取access_token错误"
;
}
}
public
function
createmenu(
$access_token
)
{
$url
=
"https://api.weixin.qq.com/cgi-bin/menu/create?access_token="
.
$access_token
;
$arr
=
array
(
'button'
=>
array
(
array
(
'name'
=>urlencode(
"生活查询"
),
'sub_button'
=>
array
(
array
(
'name'
=>urlencode(
"天气查询"
),
'type'
=>
'click'
,
'key'
=>
'VCX_WEATHER'
),
array
(
'name'
=>urlencode(
"身份证查询"
),
'type'
=>
'click'
,
'key'
=>
'VCX_IDENT'
)
)
),
array
(
'name'
=>urlencode(
"轻松娱乐"
),
'sub_button'
=>
array
(
array
(
'name'
=>urlencode(
"刮刮乐"
),
'type'
=>
'click'
,
'key'
=>
'VCX_GUAHAPPY'
),
array
(
'name'
=>urlencode(
"幸运大转盘"
),
'type'
=>
'click'
,
'key'
=>
'VCX_LUCKPAN'
)
)
),
array
(
'name'
=>urlencode(
"我的信息"
),
'sub_button'
=>
array
(
array
(
'name'
=>urlencode(
"关于我"
),
'type'
=>
'click'
,
'key'
=>
'VCX_ABOUTME'
),
array
(
'name'
=>urlencode(
"工作信息"
),
'type'
=>
'click'
,
'key'
=>
'VCX_JOBINFORMATION'
)
)
)
)
);
$jsondata
= urldecode(json_encode(
$arr
));
$ch
= curl_init();
curl_setopt(
$ch
,CURLOPT_URL,
$url
);
curl_setopt(
$ch
,CURLOPT_RETURNTRANSFER,1);
curl_setopt(
$ch
,CURLOPT_POST,1);
curl_setopt(
$ch
,CURLOPT_POSTFIELDS,
$jsondata
);
curl_exec(
$ch
);
curl_close(
$ch
);
}
private
function
getmenu(
$access_token
)
{
# code...
$url
=
"https://api.weixin.qq.com/cgi-bin/menu/get?access_token="
.
$access_token
;
$data
=
file_get_contents
(
$url
);
return
$data
;
}
private
function
delmenu(
$access_token
)
{
# code...
$url
=
"https://api.weixin.qq.com/cgi-bin/menu/delete?access_token="
.
$access_token
;
$data
= json_decode(
file_get_contents
(
$url
),true);
if
(
$data
[
'errcode'
]==0) {
# code...
return
true;
}
else
{
return
false;
}
}
private
function
make_xml(
$type
,
$value_arr
,
$o_arr
=
array
(0)){
$con
="<xml>
<ToUserName><![CDATA[{
$this
->fromUsername}]]></ToUserName>
<FromUserName><![CDATA[{
$this
->toUsername}]]></FromUserName>
<CreateTime>{
$this
->times}</CreateTime>
<MsgType><![CDATA[{
$type
}]]></MsgType>";
switch
(
$type
){
case
"text"
:
$con
.="<Content><![CDATA[{
$value_arr
[0]}]]></Content>
<FuncFlag>{
$o_arr
}</FuncFlag>";
break
;
case
"news"
:
$con
.="<ArticleCount>{
$o_arr
[0]}</ArticleCount>
<Articles>";
foreach
(
$value_arr
as
$id
=>
$v
){
if
(
$id
>=
$o_arr
[0])
break
;
else
null;
$con
.="<item>
<Title><![CDATA[{
$v
[0]}]]></Title>
<Description><![CDATA[{
$v
[1]}]]></Description>
<PicUrl><![CDATA[{
$v
[2]}]]></PicUrl>
<Url><![CDATA[{
$v
[3]}]]></Url>
</item>";
}
$con
.="</Articles>
<FuncFlag>{
$o_arr
[1]}</FuncFlag>";
break
;
}
$con
.=
"</xml>"
;
return
$con
;
}
private
function
checkSignature()
{
$signature
=
$_GET
[
"signature"
];
$timestamp
=
$_GET
[
"timestamp"
];
$nonce
=
$_GET
[
"nonce"
];
$token
= TOKEN;
$tmpArr
=
array
(
$token
,
$timestamp
,
$nonce
);
sort(
$tmpArr
);
$tmpStr
= implode(
$tmpArr
);
$tmpStr
= sha1(
$tmpStr
);
if
(
$tmpStr
==
$signature
){
return
true;
}
else
{
return
false;
}
}
}
?>