<?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范例的更多相关文章

  1. 微信公众平台开发(99) 自定义菜单获取OpenID

    关键字 微信公众平台 自定义菜单 OpenID作者:方倍工作室原文:http://www.cnblogs.com/txw1958/p/weixin-menu-get-openid.html 在这篇微信 ...

  2. PHP 微信公众号/小程序获取openid,用户信息

    1.获取code (获得openid的前置条件) 地址:https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redi ...

  3. 微信小程序开发之获取openid及用户信息

    1. 获取openid 1.1 获取code 调用接口获取登录凭证(code)进而换取用户登录态信息,包括用户的唯一标识(openid) 及本次登录的会话密钥(session_key).用户数据的加解 ...

  4. 微信多客服插件获取openid

    <!doctype html> <html> <head> <meta http-equiv="Content-Type" content ...

  5. 微信网页授权 通过code获取openid 报错40163 code been used

    使用好好的微信功能,突然安卓无法正常使用了,苹果的正常. 安卓报错内容: 40163,code been used. 题外话:微信的东西,为何报英文错误呢,装什么13. 实测结果:安卓获取用户信息时 ...

  6. PHP微信公共号授权,获取openid、unionid。

    /** * 获取code * @return code code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期 */ publ ...

  7. 微信小程序wx.login()获取openid,附:前端+后端代码

    微信小程序开放了微信登录的api,无论是个人还是企业申请的小程序均可使用. 首先创建一个项目,把这些代码都清空,我们自己写! 然后,开始写了!首先index.wxml,写一个button用于发起登录 ...

  8. 微信公众号开发前端获取openId

    参考 https://blog.csdn.net/qq_35430000/article/details/79299529

  9. 微信自定义菜单view类型获取openid访问网页

    用户点击view类型按钮后,微信客户端将会打开开发者在按钮中填写的url值 (即网页链接),达到打开网页的目的,但是view不能获取用户的openid,需与网页授权获取用户基本信息接口结合使用,获得用 ...

随机推荐

  1. linux 遇见错误Could not get lock /var/lib/dpkg/lock

    通过终端安装程序sudo apt-get install xxx时出错:E: Could not get lock /var/lib/dpkg/lock - open (11: Resource te ...

  2. WebService 之 WSDL详解

    WSDL (Web Services Description Language,Web服务描述语言)是一种XML Application,他将Web服务描述定义为一组服务访问点,客户端可以通过这些服务 ...

  3. tornado ThreadPoolExecutor

    import os import sys import time import tornado.httpserver import tornado.ioloop import tornado.opti ...

  4. 算法笔记_099:蓝桥杯练习 算法提高 排列数(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 0.1.2三个数字的全排列有六种,按照字母序排列如下: 012.021.102.120.201.210 输入一个数n 求0~9十个数的全排 ...

  5. 回调函数callback使用例子

    代码如下: <!DOCTYPE HTML> <html> <head> <meta charset="GBK" /> <tit ...

  6. 乐鑫esp8266基于freeRtos实现私有服务器本地远程OTA升级

    目录 一.前言: 二.回顾下OTA的流程: 三.lwip网络框架的知识的使用: 四.如何处理服务器返回的数据? 五.扇区的擦除和烧写? 六.如何调用? 七.好好享用吧! 八.下载: 九.工程截图: 代 ...

  7. C++实现对数学基本运算表达式的解析

    代码地址如下:http://www.demodashi.com/demo/11078.html 前段时间在LeetCode上刷题,遇到了很多涉及对字符串进行解析的题目.可能是出于这个原因,最近迷恋上了 ...

  8. 使用AsParallel 进行并行化处理数据

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  9. c#, extract number from string

    using System.Text.RegularExpressions; string numberStr = Regex.Match (str, "[0-9]").Value; ...

  10. 点滴积累【C#】---使用log4net组件记录错误日志(以文本形式记录)

    效果: 描述: 利用log4net组件进行错误日志的记录,log4net记录错误的方式我所了解的有4种,No.1 文本形式记录日志,No.2存储到数据库形式记录日志,No.3控制台控制显示日志,No. ...