鍍金池/ 問答/HTML/ 微信授權(quán)問題

微信授權(quán)問題

web開發(fā)者工具調(diào)試就有
clipboard.png
沒問題
授權(quán)登陸的頁面
但是到了手機(jī)測(cè)試
就沒有這個(gè)頁面但是也可以獲取openid 這是什么原因?

代碼應(yīng)該是沒問題的,因?yàn)榱硪粋€(gè)項(xiàng)目沒有出現(xiàn)這種問題

  //根據(jù)授權(quán)code獲取用戶id 
public static function weixin(){
  $code=!empty($_GET['code'])?trim($_GET['code']):'';
  if(!$code){
    header("location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=http%3a%2f%2fjiaogui.sunaon.com&response_type=code&scope=snsapi_userinfo#wechat_redirect");
    exit;
  }
//獲取access_token跟openid
  $pdata=array(
    'appid'=>APPID,
    'secret'=>APPSECRET,
    'code'=>$code,
    'grant_type'=>'authorization_code'
    );
  $data=self::post("https://api.weixin.qq.com/sns/oauth2/access_token",$pdata);
  $data=json_decode($data);
  $access_token=$data->access_token;
  $openid=$data->openid;
//將用戶的信息錄到數(shù)據(jù)庫去
   self::post_user_info($openid,$access_token);
  //var_dump($openid);exit;
  echo $openid; 
}


public static function post($url,$data){      
  $postdata = http_build_query($data);
  $opts = array('http' =>array('method'=>'POST','header'=>'Content-type: application/x-www-form-urlencoded','content'=>$postdata));
  $context = stream_context_create($opts);         
  $result = file_get_contents($url,true,$context);     
  return $result;
}

public static function post_user_info($openid,$access_token){
    $sql="select * from `user_info` where `openid`='{$openid}'";
    $user=Db::getAll($sql);
  if(!$user){
    $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN";
    $json = file_get_contents($url);
    $std = json_decode($json);
    $nickname=$std->nickname;
    $sex=$std->sex;
    $city=$std->city;
    $country=$std->country;
    $province=$std->province;
    $headimgurl=$std->headimgurl;
    $subscribe_time=$regtime=time();
    $sql="INSERT INTO `user_info` (`openid`,`sex`, `nickname`,`city`,`country`,`province`,`headimgurl`,`subscribe_time`) VALUES ('{$openid}','{$sex}','{$nickname}','{$city}','{$country}','{$province}','{$headimgurl}','{$subscribe_time}')";
    $act1=Db::execute($sql);
    if(!$act1){
    self::error('404','獲取openid失敗','404');    
    }
    session_start();
    $_SESSION["userid"]=$openid;
      }else{
        $_SESSION["userid"]=$openid;
     }
}

我將 獲取用戶的信息的json數(shù)據(jù) 打印出來
在web開發(fā)者工具

clipboard.png

另一個(gè)用戶手機(jī)測(cè)試是

clipboard.png

回答
編輯回答
陪她鬧

微信公眾號(hào)獲取有兩種授權(quán)方式,請(qǐng)認(rèn)真看一下微信公眾號(hào)開發(fā)文檔

2018年3月15日 02:27
編輯回答
怣人

微信網(wǎng)頁授權(quán)有兩種
一種是至獲取openid,靜默授權(quán),不能獲取昵稱 地區(qū)頭像等信息
另一種是 可以獲取 昵稱 地區(qū)頭像等信息 需要像你截圖的彈窗確認(rèn)

2018年2月10日 04:00