您的当前位置:首页正文

微信接口获取用户信息userinfo

2024-11-26 来源:个人技术集锦
待完善
//<!--授权接口-->
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=平台配置的回调地址&res&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect

//<?php

$code = $_GET["code"];//得到code
if($code){
    //获取到access_token的接口地址
    $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=secret&code=$code&grant_type=authorization_code";
}
//得到access_token,openid
$q=data($url);
//获取用户信息的接口
$url2="https://api.weixin.qq.com/sns/userinfo?access_token=$q->access_token&openid=$q->openid&lang=zh_CN";
$c=data($url2);
var_dump($c);

/**
 *
 * @param $url
 * @return mixed
 */
function data($url){
    $postUrl = $url;
    $curlPost = '';
    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
    curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    $data = curl_exec($ch);//运行curl
    curl_close($ch);

    $j=json_decode($data);
    return $j;
}

 

显示全文