静默方式获取微信用户openid

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33858250/article/details/82982661

页面A

getopenid.php

<?php


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

猜你喜欢

转载自blog.csdn.net/qq_33858250/article/details/82982661
今日推荐