php 微信获取openid

来源:互联网 发布:sai软件绘画手机版 编辑:程序博客网 时间:2024/05/10 07:37


header("Content-type: text/html; charset=utf-8");$code = $_GET["code"]; $html = $_GET['state'];if(empty($code)){// 如果CODE为空,取CODE$APPID='xxxx';$REDIRECT_URI='http://zss.xxxx.com/openid/index.php';$scope='snsapi_base';//$scope='snsapi_userinfo';//需要授权$url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$APPID.'&redirect_uri='.urlencode($REDIRECT_URI).'&response_type=code&scope='.$scope.'&state='.$html.'#wechat_redirect';header("Location:".$url);}$appid = "xxxx"; $secret = "xxxx"; $code = $_GET["code"]; $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';$curl = curl_init();curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);curl_setopt($curl, CURLOPT_TIMEOUT, 500);// 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。// 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);curl_setopt($curl, CURLOPT_URL, $url);$res = curl_exec($curl);curl_close($curl);   $json_obj = json_decode($res,true); $openid=$json_obj["openid"];echo 'openid=【'.$openid.'】';


原创粉丝点击