凌的博客

您现在的位置是:首页  > 学无止境 > PHP

PHP

微信登录接口

2018-09-28PHP0
<?php
error_reporting(E_ALL & ~E_NOTICE);
if(isset($_GET['echostr'])){
	exit($_GET['echostr']);	
}
$app_id = 'xxxxxxxxxxxx';
$app_key = 'xxxxxxxxxxxxxxxxx';
$code = isset($_REQUEST['code']) ? $_REQUEST['code'] : '';
$url = 'http://www.yetyun.com/wx/index.php';

$data = wx_gettoken($app_id, $app_key, $code); //token openid
$ret = wx_getinfo($data['token'], $data['openid']); //用户名昵称  用户头像
$openid = $data['openid']; //openid

function weixin_login($app_id,$url){
	$url = urlencode($url);
	return"https://open.weixin.qq.com/connect/oauth2/authorize?appid={$app_id}&redirect_uri={$url}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirec";	
}

function wx_gettoken($app_id, $app_key, $code){
    $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$app_id}&secret={$app_key}&code={$code}&grant_type=authorization_code";
    $html = file_get_contents($url);
    $json = json_decode($html, 1);
    $access_token = $json['access_token'];
    $openid = $json['openid'];
    return array("token" => $access_token, 'openid' => $openid);
}
function wx_getinfo($token, $openid){
    $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$token}&openid={$openid}";
    $html = file_get_contents($url);
    $json = json_decode($html, 1);
    $nickname = $json['nickname'];
    $headhtml = $json['headimgurl'];
    return array("nickname" => $nickname, 'headimg' => $headhtml);
}

如果本文对您有所帮助,请支持下本站哦!!!^_^

文章评论

0条评论