<?php
function
getToken(){
$timestamp
=
$_GET
[
"timestamp"
];
$nonce
=
$_GET
[
"nonce"
];
$token
=
"hello hqtoken"
;
$signature
=
$_GET
[
"signature"
];
$array
=
array
(
$timestamp
,
$nonce
,
$token
);
sort(
$array
);
$tmpstr
=implode(
""
,
$array
);
$tmpstr
=sha1(
$tmpstr
);
if
(
$tmpstr
==
$signature
){
echo
$_GET
[
"echostr"
];
exit
;
}
}
function
getAccessToken(){
$appid
=
"你的appid"
;
$appsecret
=
"你的appsecret"
;
$data
=json_decode(get_php_file(
"access_token.php"
));
if
(
$data
->expire_time<time()){
$url
=
"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
.
$appid
.
"&secret="
.
$appsecret
;
$res
=json_decode(httpGet(
$url
));
$access_token
=
$res
->access_token;
if
(
$access_token
){
$data
->expire_time=time()+7000;
$data
->access_token=
$access_token
;
set_php_file(
"access_token.php"
,json_encode(
$data
));
}
}
else
{
$access_token
=
$data
->access_token;
}
return
$access_token
;
}
function
httpGet(
$url
){
$curl
= curl_init();
curl_setopt(
$curl
,CURLOPT_URL,
$url
);
curl_setopt(
$curl
,CURLOPT_RETURNTRANSFER,1);
curl_setopt(
$curl
,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt(
$curl
,CURLOPT_SSL_VERIFYHOST,false);
$res
=curl_exec(
$curl
);
curl_close(
$curl
);
return
$res
;
}
function
get_php_file(
$filename
){
return
trim(
substr
(
file_get_contents
(
$filename
),15));
}
function
set_php_file(
$filename
,
$content
){
$fp
=
fopen
(
$filename
,
"w"
);
fwrite(
$fp
,
"<?php exit();?>"
.
$content
);
fclose(
$fp
);
}
echo
getAccessToken();
?>