1 回复
JS部分
wx.login({ success: function (code){ wx.request({ url: 'test.php' , data:{ code:code }, method: 'POST' , header:{ 'content-type' : 'application/x-www-form-urlencoded;charset=UTF-8' }, success: function (res){ console.log(res) } }) } }) |
PHP部分(我只会PHP,要是其它语言的,你自己看着改吧)
$code = $_POST [ 'code' ]; $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=your_appid&secret=your_secret&grant_type=authorization_code&js_code=' . $code ; $curl = curl_init(); curl_setopt( $curl , CURLOPT_URL, $url ); curl_setopt( $curl , CURLOPT_HEADER, 1); curl_setopt( $curl , CURLOPT_RETURNTRANSFER, 1); curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER, false); $data = curl_exec( $curl ); curl_close( $curl ); $data = explode ( "{" , $data ); $data = explode ( "}" , $data [1]); echo '{' . $data [0]. '}' ; |