index.php
<?php
//echo $wechat->getUserData();
header('location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx40ebdcf2ab048c9c&redirect_uri=http://tina.love5211314.xin/indexLogin.php&response_type=code&scope=snsapi_userinfo&state=123&connect_redirect=1#wechat_redirect');
indexLogin.php
<?php
$appid = 'wx40ebdcf2ab048c9c';
$secretid = '';
$code = $_GET['code'];
//获取access_token
function get_access_token($appid,$secretid,$code)
{
$json = file_get_contents('https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secretid.'&code='.$code.'&grant_type=authorization_code');
$data = json_decode($json,true);
//echo "<pre>";
//var_dump($data);
if(array_key_exists('errcode',$data) && $data['errcode']!=0){
return $data['errcode']."请检查";
}
$access_token = $data['access_token'];
//设置缓存文件名
$cacheFile = "access_token";
//执行缓存
cacheSet($cacheFile,$access_token,7200);
return $access_token;
}
//echo get_access_token($appid,$secretid,$code);
//设置缓存
function cacheSet($path,$data,$expire = 7200)
{
//缓存时间
$expire = sprintf("%010d",$expire);
$data = "<?php\n//".$expire.serialize($data)."\n?>";
return file_put_contents($path, $data);
}
//获取用户信息
//https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
function get_userinfo($appid,$secretid,$code)
{
$access_token = get_access_token($appid,$secretid,$code);
echo $access_token;
$json = file_get_contents('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$appid.'&lang=zh_CN');
$userInfo = json_decode($json,true);
echo "<pre>";
var_dump($userInfo);
$url = $userInfo['headimgurl'];
$img = file_get_contents($url);
$fileName = $userInfo['openid'];
file_put_contents('/home/wwwroot/default/weixin_login/down/'.$fileName.'.png',$img);
}
get_userinfo($appid,$secretid,$code);