function appid(){
$appid="xxxxxxxxxxx";
$wechat_appsecret="xxxxxxxxx";
//通过网页授权获取code码
if (isset($_GET['code']))
{
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$wechat_appsecret&code=".$_GET['code']."&grant_type=authorization_code";
//$url = 'http://www.mkv8.com';
$output=curlGet($url);
$json=json_decode($output,true);
//得到openid
$openid = strval($json['openid']);
$_SESSION['openid']=$openid;
$access_token = $json['access_token'];
//获取用户个人信息
$return1='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
//echo $return1;die();
$content1 = https_request($return1);;
$_SESSION['weixinuserinfo']= json_decode($content1,true);
print_r($_SESSION['weixinuserinfo']);
}
else
{
//$customeUrl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$customeUrl="http://vqe7sq3hou.proxy.qqbrowser.cc/index.php?g=Turntable&m=Turntable&a=index&id=1";
$_SESSION['weixin']['state'] = md5(uniqid());
//网页授权
$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=".urlencode($customeUrl)."&response_type=code&scope=snsapi_userinfo&state=123123123#wechat_redirec";
echo "<script>location.href='$url';</script>";
exit();
}
}
function curlGet($url){
$ch = curl_init();
$header = "Accept-Charset: utf-8";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$temp = curl_exec($ch);
return $temp;
}
//https请求
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;
}
微信获取用户信息实例
最新推荐文章于 2024-03-01 16:43:18 发布