//***方法一

获取code

https://open.weixin.qq.com/connect/oauth2/authorize?appid=这里是你的公众号的APPID&redirect_uri=http://www.xx.com/getcode&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect

用户点击确认登录,自动跳转下面地址得到code

http://www.xx.com/getcode 这个是你自己的跳转地址

http://www.xx.com/getcode?code=0064f7afef7af7b395147bfe8b51f7bf&state=123

后面的这个 ?code=……123   是微信自动跳转添加的,不是你自己加的

下面是PHP语言,写在getcode这个页面里

1
2
3
4
5
$code = $_GET['code'];//获取code
$weixin =  file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=这里是你的APPID&secret=这里是你的SECRET&code=".$code."&grant_type=authorization_code");//通过code换取网页授权access_token
$jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
$array = get_object_vars($jsondecode);//转换成数组
$openid = $array['openid'];//输出openid
 
 
//***方法二
$appid = "公众号在微信的appid";
$secret = "公众号在微信的app secret";
$code = $_GET["code"];
$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$get_token_url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$res = curl_exec($ch);
curl_close($ch);
$json_obj = json_decode($res,true);
//根据openid和access_token查询用户信息
$access_token = $json_obj['access_token'];
$openid = $json_obj['openid'];
$get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
 
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$get_user_info_url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$res = curl_exec($ch);
curl_close($ch);
 
//解析json
$user_obj = json_decode($res,true);
$_SESSION['user'] = $user_obj;
print_r($user_obj); 

PHP获取微信openid 简单教程的更多相关文章

  1. 获取微信openID 的步骤

    获取微信openid的步骤:1.进入-->判断openID是否为空: 空-->$url=urlencode("http://xxx/xxx.php");//回调链接 $ ...

  2. 微信公众号开发系列-获取微信OpenID

    在微信开发时候在做消息接口交互的时候须要使用带微信加密ID(OpenId),下面讲讲述2中类型方式获取微信OpenID.接收事件推送方式和网页授权获取用户基本信息方式获取. 1.通过接收被动消息方式获 ...

  3. php获取微信openid

    使用微信接口,无论是自动登录还是微信支付我们首先需要获取的就是openid,获取openid的方式有两种,一种是在关注的时候进行获取,这种订阅号就可以获取的到,第二种是通过网页授权获取,这种获取需要的 ...

  4. PHP获取用户是否关注公众号。获取微信openid和用户信息

    <?php /* * 首先填写授权地址为当前网址 * 将$appid和$secret参数替换成自己公众号对应参数,需要外网可以访问服务器环境测试 */ header("Content- ...

  5. 微信开发获取用户OpenID

    第一次开发微信版网页,对最重要的获取微信OpenId,特此记录下来 1.首先得有appid和appsecret . public class WeiXin { public static string ...

  6. MVC 微信开发获取用户OpenID

    第一次开发微信版网页,对最重要的获取微信OpenId,特此记录下来 1.首先得有appid和appsecret . public class WeiXin { public static string ...

  7. asp.net core 微信获取用户openid

    获取openid流程为首先根据微信开发参数构造AuthorizeUrl认证链接,用户跳转到该链接进行授权,授权完成将跳转到回调页(首次认证需要授权,后面将直接再跳转至回调页),此时回调页中带上一个GE ...

  8. 微信公众平台开发—利用OAuth2.0获取微信用户基本信息

    在借鉴前两篇获取微信用户基本信息的基础下,本人也总结整理了一些个人笔记:如何通过OAuth2.0获取微信用户信息 1.首先在某微信平台下配置OAuth2.0授权回调页面: 2.通过appid构造url ...

  9. 基于注解的实现获取微信openId1

    最近在弄微信支付,网站有好几种不同类型的"商品",去每个支付的页面都需要获取用户的OpenId,而且获取openid要在微信的浏览器去发送请求,如果有三个不同类型的付款页面就需要写 ...

随机推荐

  1. unity, 弹出panel一定要放在UI Hierarchy的底端

    如上图,buyPanel放在最底端,为的是它弹出时屏蔽所有其它UI的消息.

  2. Android应用APP脱壳笔记

    [TOC] 天下游 模拟定位技术点简析 通过代码分析初步猜测模拟定位用到的几处技术点: 获取了Root权限 通过反射获取 android.os.ServiceManager 对应的函数 getServ ...

  3. [JavaSecurity] - AES Encryption

    1. AES Algorithm The Advanced Encryption Standard (AES), also as known as Rijndael (its original nam ...

  4. JAVA热部署原理

    1.热部署是什么? 对于Java应用程序来说,热部署就是在运行时更新Java类文件. 2.热部署有什么用? 可以不重启应用的情况下,更新应用.举个例子,就像电脑可以在不重启的情况下,更换U盘. OSG ...

  5. 26. Remove Duplicates from Sorted Array【easy】

    26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...

  6. 75. Find Peak Element 【medium】

    75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...

  7. Spring Boot全日志设置

    说在前面 这里日志分两种.一种是tomcat的输出(系统)日志,一种是自己定义的日志. 系统日志设置 目标 当springboot接收到请求时记录日志到文件中 实现 你只需要在你的绿叶applicat ...

  8. java中Calendar.getInstance()和new Date()的差别是什么?

    java中Calendar.getInstance()和new Date()的差别如下: Calendar.getInstance()是获取一个Calendar对象并可以进行时间的计算,时区的指定ne ...

  9. PyCharm创建文件时自动添加头注释

    进入设置 File->settings->Editor->File and Code Templates->Python Script 添加以下代码: #!/usr/bin/e ...

  10. C#中基本类型占用字节数

    bool -> System.Boolean (布尔型,其值为 true 或者 false) byte -> System.Byte (字节型,占 1 字节,表示 8 位正整数,范围 0 ...