微信服务号获取微信用户openid - 网络技能论坛 - 经验分享 - 小轻秀场

微信服务号获取微信用户openid

新建文件 text.php

  1. <?php
  2.  
  3.  
  4. $appid = "wx0a75f2ebc13ufygi2c5";//修改为你的服务号开发者ID
  5. $secret = "aeb255579544e561e29f2eytuytrhdf";//修改为你的服务号开发密码
  6. $code = $_GET["code"];
  7.  
  8. $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
  9. $oauth2 = getJson($oauth2Url);
  10.  
  11. // 获得 access_token 和openid
  12. $access_token = $oauth2["access_token"];
  13. $openid = $oauth2['openid'];
  14.  
  15. function getJson($url){
  16. $ch = curl_init();
  17. curl_setopt($ch, CURLOPT_URL, $url);
  18. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  19. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  20. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  21. $output = curl_exec($ch);
  22. curl_close($ch);
  23. return json_decode($output, true);
  24. }
  25.  
  26. $get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
  27. $userinfo = getJson($get_user_info_url);
  28.  
  29. var_dump($userinfo);//输出微信号昵称、头像、openid

再创建一个文件

go.php


  1. $appid = 'wx0a75f2y7ioyg412c5';//你的服务号开发者id
  2. $host='https://wap.xyz/text.php';//你的test.php访问地址
  3. $redirect_uri = urlencode($host);
  4. $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
  5. header("Location:" . $url);


然后微信中打开链接  

https://localhost:443/go.php

请登录后发表评论

    没有回复内容