'wxd3ffecaec1034fa4', '__SERECT__' =>'e53844ef2b712bfc68ee1d23fb41ddde', '__CALL_URL__' =>'https://m.kminsw.com/haha.php' //当前页地址 ); //没有传递code的情况下,先登录一下 if(!isset($_GET['code']) || empty($_GET['code'])){ $getCodeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize". "?appid=" . $CONF['__APPID__'] . "&redirect_uri=" . $CONF['__CALL_URL__'] . "&response_type=code". "&scope=snsapi_base". #!!!scope设置为snsapi_base !!! "&state=1"; //跳转微信获取code值,去登陆 header('Location:' . $getCodeUrl); exit; } $code = trim($_GET['code']); /*echo '
'; print_r($code); echo '';*/ //使用code,拼凑链接获取用户openid $getTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token". "?appid={$CONF['__APPID__']}". "&secret={$CONF['__SERECT__']}". "&code={$code}". "&grant_type=authorization_code"; /*echo '
'; print_r($getTokenUrl); echo '';*/ $getTokenUrll = $getTokenUrl; /*$weixin = file_get_contents($getTokenUrll);//通过code换取网页授权access_token echo '
'; print_r($weixin); echo ''; $jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码 $array = get_object_vars($jsondecode);//转换成数组 $openid = $array['openid'];//输出openid*/ $param = array(); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $getTokenUrll); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $ret = curl_exec($ch); $retinfo = curl_getinfo($ch); curl_close($ch); if($retinfo['http_code']==200){ $data = json_decode($ret, true); /*print_r($data);*/ $openid = $data['openid'];//输出openid*/ echo '
'; print_r($openid); echo ''; }else{ echo 'POST Fail'; } function cookie($var, $value='', $time=0, $path='', $domain=''){ $_COOKIE[$var] = $value; if(is_array($value)){ foreach($value as $k=>$v){ setcookie($var.'['.$k.']', $v, $time, $path, $domain, $s); } }else{ setcookie($var, $value, $time, $path, $domain, $s); } } //将openid和accesstoken存入cookie中 cookie('oneStep_switch',$openid,time()+2*7*24*3600,'/'); $getid = $_COOKIE['oneStep_switch'] ; echo '
'; print_r($getid); echo ''; header('Location: ' . $_SERVER['HTTP_REFERER']); } else{ echo "liulanqi"; } $namme = $_COOKIE['UNAME']; echo '
'; print_r($namme); echo ''; ?>