<?php $appid = "wx200ece2d613cc414"; //这里的appid是假的演示用 $redirect_uri = urlencode("http://test.it1000.cc/test.php"); //这里的地址需要http: $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $appid . "&redirect_uri=" . $redirect_uri . "&response_type=code&scope=snsapi_base&state=123#wechat_redirect"; header('location:' . $url);
<?php $appid = "wx200ece2d613cc414"; $secret = "029daa7ef07c36c4e3b97673f5713d76"; $code = $_GET["code"]; $get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $secret . '&code=' . $code . '&grant_type=authorization_code'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $get_token_url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); $res = curl_exec($ch); curl_close($ch); $json_obj = json_decode($res, true); var_dump(json_obj);
这个简易版