query($sql); $rowset = $result->fetchAll(PDO::FETCH_ASSOC); $errinfo=$pdo->errorInfo(); if (isset($_GET['fromOpenId'])){ $fromOpenId=$_GET['fromOpenId']; }else { $fromOpenId=""; } if($rowset){ $infoUrl="http://".$_SERVER['HTTP_HOST']."/wechat/jssdk/lotteryinfo.php?openId=".$openId; Header("Location: $infoUrl"); exit(); } }else if(!isset($_GET['code'])){ $selfUrl=urlencode("http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); $getCodeUrl="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=".$selfUrl."&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect"; Header("Location: $getCodeUrl"); exit(); }else{ $code=$_GET['code']; $getOpenidUrl="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".APPID."&secret=".APPSECRET."&code=".$code."&grant_type=authorization_code"; $response=http_request($getOpenidUrl); $openIdObj=json_decode($response); $openId=$openIdObj->openid; $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."&openId=".$openId; Header("Location: $url"); exit; } ?>