步骤1:组装url用来获取code (通过开发者工具访问)
https://open.weixin.qq.com/connect/oauth2/authorize?appid=自己的APPID&redirect_uri=http://自己的网址/login.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
< ?php #1.获取code $code = $_GET['code']; #2.获取openid和accessiontoken $apiData = array( 'appid'=>'wx56e3e3d75414b3d0', 'secret'=>'f8ec8178c6dda48d1cb25c07304eff44', 'code'=> $code, 'grant_type'=>'authorization_code' ); $api = "https://api.weixin.qq.com/sns/oauth2/access_token?".http_build_query($apiData); $data = json_decode(file_get_contents($api), true); // echo $data['access_token']; // echo '<hr/>'; // echo $data['openid']; # 步骤3:根据openid和access_token获取用户信息 $apiData = array( 'access_token'=> $data['access_token'], 'openid'=> $data['openid'], 'lang'=>'zh_CN' ); $api = "https://api.weixin.qq.com/sns/userinfo?".http_build_query($apiData); $data = json_decode(file_get_contents($api), true); echo '<pre>'; print_r($data);