zoukankan      html  css  js  c++  java
  • php获取微信的openid

     1         header("Content-type: text/html; charset=utf-8");
     2         if(!isset($_GET['code'])){
     3             $APPID='公众号在微信的appid';
     4             $REDIRECT_URI='要请求的url';
     5             $scope='snsapi_base';
     6             $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$APPID.'&redirect_uri='.urlencode($REDIRECT_URI).'&response_type=code&scope='.$scope.'&state=wx'.'#wechat_redirect';
     7             header("Location:".$url);
     8         }else{
     9             $appid = "公众号在微信的appid";
    10             $secret = "公众号在微信的app secret";
    11             $code = $_GET["code"];
    12             $get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
    13             $ch = curl_init();
    14             curl_setopt($ch,CURLOPT_URL,$get_token_url);
    15             curl_setopt($ch,CURLOPT_HEADER,0);
    16             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
    17             curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    18             $res = curl_exec($ch);
    19             curl_close($ch);
    20             $json_obj = json_decode($res,true);
    21             //根据openid和access_token查询用户信息
    22             $access_token = $json_obj['access_token'];
    23             $openid = $json_obj['openid'];
    24             $get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
    25             
    26             $ch = curl_init();
    27             curl_setopt($ch,CURLOPT_URL,$get_user_info_url);
    28             curl_setopt($ch,CURLOPT_HEADER,0);
    29             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
    30             curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    31             $res = curl_exec($ch);
    32             curl_close($ch);
    33             
    34             //解析json
    35             $user_obj = json_decode($res,true);
    36             $_SESSION['user'] = $user_obj;
    37             print_r($user_obj);
    38         }
  • 相关阅读:
    3-05. 寻求倒数第二链线性表K项目(15)(STL list应用 ZJU_PAT)
    springbatch操作CSV文件
    oracle 数据库技术支持生命周期表
    调试经验--硬盘U菜
    hdu149850 years, 50 colors (多个最小顶点覆盖)
    POJ3213(矩阵乘法)
    Cocos2d-x 2.3.3版本 FlappyBird
    POJ 2114 Boatherds 划分树
    jQuery 添加 删除 改动select option
    STL容器存储的内容动态分配情况下的内存管理
  • 原文地址:https://www.cnblogs.com/wxfallstar/p/6677884.html
Copyright © 2011-2022 走看看