php获取微信 openid

header ( ‘content-type:text/html’ );header ( ‘content-type:text/html’ ); header ( ‘charset:utf-8’ ); $wxcode = $_GET[‘code’]; $wxstate = $_GET[‘state’]; define ( “APPID”, “aaaaaaaaaaaaaaaaaaaaaaaaa” ); define ( “APPSECRET”, “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx” ); if ($wxcode!=”){ $token_access_url = “https://api.weixin.qq.com/sns/oauth2/access_token?appid=” . APPID . “&secret=” . APPSECRET . “&code=” . $wxcode . “&grant_type=authorization_code”; echo “token_access_url:”.$token_access_url.”<br />”; $res = file_get_contents($token_access_url); // 获取文件内容或获取网络请求的内容 //echo “res:”.$res.”<br />”; $result = json_decode($res,true); // 接受一个 JSON 格式的字符串并且把它转换为 PHP 变量 $access_token = $result[‘access_token’]; //echo “access:”.$access_token; $expires_in = $result[‘expires_in’]; $weixin_id = (string)$result[‘openid’]; $scope = $result[‘scope’]; $unionid = $result[‘unionid’]; //echo “weixin_id:”.$weixin_id.”<br />”;