步驟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);