微信view类型的菜单获取openid范例
<?php
//启用session
session_start();
//编码
header("Content-type: text/html; charset=utf-8");
//保存微信openid
$weixin_openid = "";
//保存从微信得到的code
$code = "";
//获得菜单中设置的state值
$state = ""; //修改为注册的测试号信息
$appid = "wx834404c1d1dbb5ec";
$secret = "bf03cfa823d80dd537bd18e83ce9a4e4"; if (isset($_GET['code']) && isset($_GET['state']) ){
$weixin_openid = GetOpenid($_GET['code']);
$code = $_GET['code'];
$state = $_GET['state'];
//echo $resultStr;
}else{
echo "NO CODE";
return ;
} // store session data
$_SESSION['weixin_openid']=$weixin_openid; if($state == "s1"){
//我的课堂
header('Location: courseCenter.php');
}else if($state == "s2"){
//订单记录
header('Location: orderList.php');
}else{
$textTpl="由认证接口进入我的课堂,<br/>您的 wx_openid: %s <br/> code: %s <br/> state: %s <br/>
<a href='orderList.php'>订单记录</a>";
$resultStr = sprintf($textTpl, $weixin_openid, $code, $state); echo $resultStr ;
} //获取微信的openid
function GetOpenid($c_code)
{
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid . "&secret=" . $secret . "&code=" . $c_code . "&grant_type=authorization_code";
//$url = "http://www.baidu.com";
$result = getData($url); $jsondecode = json_decode($result);
if($jsondecode != null){
if(property_exists ( $jsondecode, "openid" ) )
{
return $jsondecode->{"openid"};
}else{
return "code is invalid.";
}
} return null;
}
//获取https的get请求结果
function getData($c_url)
{
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $c_url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
// curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
// curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec($curl); // 执行操作
if (curl_errno($curl)) {
echo 'Errno'.curl_error($curl);//捕抓异常
}
curl_close($curl); // 关闭CURL会话
return $tmpInfo; // 返回数据
} ?>
微信view类型的菜单获取openid范例的更多相关文章
- 微信公众平台开发(99) 自定义菜单获取OpenID
关键字 微信公众平台 自定义菜单 OpenID作者:方倍工作室原文:http://www.cnblogs.com/txw1958/p/weixin-menu-get-openid.html 在这篇微信 ...
- PHP 微信公众号/小程序获取openid,用户信息
1.获取code (获得openid的前置条件) 地址:https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redi ...
- 微信小程序开发之获取openid及用户信息
1. 获取openid 1.1 获取code 调用接口获取登录凭证(code)进而换取用户登录态信息,包括用户的唯一标识(openid) 及本次登录的会话密钥(session_key).用户数据的加解 ...
- 微信多客服插件获取openid
<!doctype html> <html> <head> <meta http-equiv="Content-Type" content ...
- 微信网页授权 通过code获取openid 报错40163 code been used
使用好好的微信功能,突然安卓无法正常使用了,苹果的正常. 安卓报错内容: 40163,code been used. 题外话:微信的东西,为何报英文错误呢,装什么13. 实测结果:安卓获取用户信息时 ...
- PHP微信公共号授权,获取openid、unionid。
/** * 获取code * @return code code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期 */ publ ...
- 微信小程序wx.login()获取openid,附:前端+后端代码
微信小程序开放了微信登录的api,无论是个人还是企业申请的小程序均可使用. 首先创建一个项目,把这些代码都清空,我们自己写! 然后,开始写了!首先index.wxml,写一个button用于发起登录 ...
- 微信公众号开发前端获取openId
参考 https://blog.csdn.net/qq_35430000/article/details/79299529
- 微信自定义菜单view类型获取openid访问网页
用户点击view类型按钮后,微信客户端将会打开开发者在按钮中填写的url值 (即网页链接),达到打开网页的目的,但是view不能获取用户的openid,需与网页授权获取用户基本信息接口结合使用,获得用 ...
随机推荐
- Zend Guard Run-time support missing 问题的解决
Zend Guard是目前市面上最成熟的PHP源码加密产品了. 刚好需要对自己的产品进行加密,折腾了一晚上,终于搞定,将碰到的问题及解决方法记录下来,方便日后需要,也可以帮助其他人. 我使用的是Wam ...
- [Erlang]Erlang经常使用工具解说
原创文章,转载请注明出处:服务器非业余研究http://blog.csdn.net/erlib 作者Sunface 联系邮箱:cto@188.com 但凡有图形界面的都须要linux系统安装了wx图形 ...
- GET RESTful With Python
Python调用RESTful:http://blog.akiban.com/get-restful-with-python/ 本文就是参考该英文做了一下试验,后续补充一下翻译. This post ...
- vue - vue-loader.conf
'use strict' // 工具=> build/util.js const utils = require('./utils') // 配置=> build/config/index ...
- Rxjava2.0 链式请求异常处理
使用Rxjava2.0的过程中,难免会遇到链式请求,而链式请求一般都是第一个抛异常,那么后面的请求都是不会走的.现在来讨论一下链式请求的一种异常处理方法.例如: 一个登录-->通过登录返回的to ...
- php求斐波那契数列
<?php function feibonaqi(){ //参数$num表示为第$num个数之前的所有斐波那契数列 $arr = array(); //定义一个空变量用来存放斐波那契数列的数组 ...
- stderr和stdout详细解说(转)
今天又查了一下fprintf,其中对第一个参数stderr特别感兴趣. int fprintf(FILE *stream,char *format,[argument]): 在此之前先区分一下:pri ...
- sharepoint admin svc must be running in order to create deployment timer job 若要创建计时器作业,必须执行SVC
sharepoint admin svc must be running in order to create deployment timer job 若要创建计时器作业.必须执行SVC ...
- DevExpress SpinEdit控件使用实例——删除ASPxSpinEdit右边的上下小箭头SpinButtons-ShowIncrementButtons
使用ASPxSpinEdit来添加数字类型的输入框: 默认情况下, SpinEdit通过点击右边的上下箭头来加减number,下面的代码是去掉上下箭头,内容居中显示,并将边框颜色置为白色: <d ...
- C# 取时间段年、月、日、季度
DateTime dt = DateTime.Now; //当前时间 DateTime startWeek = dt.AddDays(1 - Convert.ToInt32( ...