<!doctype html> <html> <head> <meta charset="utf-8"> <title>活动会议签到</title> </head> <body> <?php function curlPost($url,$data=""){ $ch = curl_init(); $opt = array( CURLOPT_URL => $url, CURLOPT_HEADER => 0, CURLOPT_POST => 1, CURLOPT_POSTFIELDS => $data, CURLOPT_RETURNTRANSFER => 1, CURLOPT_TIMEOUT => 20 ); $ssl = substr($url,0,8) == "https://" ? TRUE : FALSE; if ($ssl){ $opt[CURLOPT_SSL_VERIFYHOST] = 2; $opt[CURLOPT_SSL_VERIFYPEER] = FALSE; //FALSE返回对象,TRUE返回数组 } curl_setopt_array($ch,$opt); $data = curl_exec($ch); curl_close($ch); return $data; } function accessToken() { $tokenFile = "./access_token.txt"; $data = json_decode(file_get_contents($tokenFile)); if ($data->expire_time < time() or !$data->expire_time){ $appid = "替换成你的appid"; $appsecret = "替换成你的appid"; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret"; $res = getJson($url); $access_token = $res['access_token']; if($access_token) { $data_new['expire_time'] = time() + 7000; $data_new['access_token'] = $access_token; $fp = fopen($tokenFile, "w"); fwrite($fp, json_encode($data_new)); fclose($fp); } } else { $access_token = $data->access_token; } return $access_token; } function getJson($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return json_decode($output,TRUE); //返回数组式json } $ACCESS_TOKEN=accessToken(); $Url="https://api.weixin.qq.com/shakearound/user/getshakeinfo?access_token=$ACCESS_TOKEN"; $ticket=$_GET["ticket"]; $data='{"ticket":"'.$ticket.'","need_poi":0}'; $res = curlPost($Url,$data); $openid=json_decode($res)->data->openid; $fl=$_GET["fl"]; $url = "替换成你的网址/qd.asp?fl=". $fl ."&wxid=".$openid; echo "<script language='javascript' type='text/javascript'>"; echo "window.location.href='$url'"; echo "</script>"; ?> </body> </html>
微信公众号缓存ACCESS_TOKEN的PHP代码
猜你喜欢
转载自hanbaohong.iteye.com/blog/2292193
今日推荐
周排行