摘要:公众号页面中 获取访问者OpenID授权 演示 urlencode方法<?php if (!isset($_GET["type"])) { &nbs
公众号页面中 获取访问者OpenID授权 演示 urlencode方法
<?php
if (!isset($_GET["type"]))
{
die("illegal call!");
}
$callpra = $_GET["type"];
if ($callpra != "0" && $callpra != "1" && $callpra != "2")
{
die("illegal call!");
}
$APPID='wxappid';
$REDIRECT_URI='http://appex.couns.com/wxcall/cb.php?type='.$_GET["type"];
//$scope='snsapi_base';
$scope='snsapi_userinfo';//需要授权
$url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$APPID.'&redirect_uri='.urlencode($REDIRECT_URI).'&response_type=code&scope='.$scope.'&state=STATE&connect_redirect=1#wechat_redirect';
header("Location:".$url);
?>获取到授权Code后 js_decode解码调用
//get userinfo by openid and 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); //print_r($res); $user_obj = json_decode($res,true); $openID = $user_obj['openid']; $nickName = $user_obj['nickname']; $msex = $user_obj['sex']; $mcity = $user_obj['city']; $mprovince = $user_obj['province']; $mcountry = $user_obj['country']; $mheadimgurl = $user_obj['headimgurl'];
http_build_query演示
$data = array('foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertext processor');
echo http_build_query($data);
输出:
foo=bar&baz=boom&cow=milk&php=hypertext+processor
批改老师:韦小宝批改时间:2019-02-11 10:20:56
老师总结:这个作业不是写url以及json中常用的函数嘛?你这里用到的url和json函数的地方不明显啊!是代码没复制完整吗!