首页 > 微信 > 详细

静默获取微信用户openid如此简单,只需要一句话

时间:2017-10-08 09:30:34      阅读:424      评论:0      收藏:0      [点我收藏+]

页面A

getopenid.php

<?php
/**
 * Created by PhpStorm.
 * User: sunjianyin
 * Date: 2017/10/7
 * Time: 下午10:22
 */


header(‘Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=OPENID&redirect_uri=b.php&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect‘);

其中的openid和回调的uri需要再自己设置,需要注意的是redirect_uri必须是在设置回调的域名下;

不需要任何其他,初始第一步这样,跳转到页面b.php,其中再获取$code=$_GET[‘code‘],就是code了

主要还是要看 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842

b.php

$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

 

静默获取微信用户openid如此简单,只需要一句话

原文:http://www.cnblogs.com/vipstar/p/7636344.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!