当前位置:海纳阁—我的个人经验分享 » WEB技术分享 » 微信认证服务号获取用户头像等信息

微信认证服务号获取用户头像等信息

Posted by 杨志欣 on 8月 20th, 2015
5893 Views

事先说明:这个功能的实现必须是认证后的服务号才可以。

以下为链接:红色部分需要在公众号开发者中心网页授权获取用户基本信息中设置然后通过这个链接获取授权,紫色部分是回调的具体页面

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx744df80ba339e18f&redirect_uri=http://wx.tuopuxuanwei.com/huluwa/long.php&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect

以下为核心代码(将该代码复制到上述红色加紫色部分的路径中即可测试,以下代码文件的名字就是long.php):

<?php
//echo $_GET[‘code’];
$code = $_GET[‘code’];//前端传来的code值
$appid = “wx744df80ba339e18f”;
$appsecret = “9bc5f64b335c00879c6f845936ce006f”;//获取openid
$url = “https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code”;
$result = https_request($url);
$jsoninfo = json_decode($result, true);
$openid = $jsoninfo[“openid”];//从返回json结果中读出openid
$access_token = $jsoninfo[“access_token”];//从返回json结果中读出openid
//echo $jsoninfo[“access_token”];
//$callback=$_GET[‘callback’];? // echo $callback.”({result:'”.$openid.”‘})”;
$url1 = “https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN”;
$result1 = https_request($url1);
$jsoninfo1 = json_decode($result1, true);
$nickname=$jsoninfo1[“nickname”];
$headimgurl0=$jsoninfo1[“headimgurl”];
$city=$jsoninfo1[“city”];
$sex=$jsoninfo1[“sex”];
$openid=$jsoninfo1[“openid”];

//echo $nickname; //把openid 送回前端
//echo $headimgurl0; //把openid 送回前端
//echo $city; //把openid 送回前端
//echo $sex; //把openid 送回前端
//echo $openid; //把openid 送回前端

function https_request($url,$data = null){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″>
<title></title>
</head>
<body>
<div>
<img src=”<?php echo $headimgurl0?>”/>
</div>

</body>
</html>

扫描二维码体验一下:

103a76d72312f619634f21879c7aa779

 

你可能喜欢: