<?php
/**
* 通过$appid、$appsecret获得基础支持的接口唯一凭证access_token,返回值为array类型
*/
function get_access_token_base($appid , $appsecret ) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
$output = https_request( $url );
$jsoninfo = json_decode($output, true);
return $jsoninfo;
} //======================================================================================================================================= /**
* 通过网页授权access_token和用户openid获取用户信息
* 返回值为array类型
*/
public function get_userinfo($access_token , $openid ) {
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
$output = https_request( $url );
$jsoninfo = json_decode($output, true);
return $jsoninfo;
} //======================================================================================================================================= /**
* 获取JS接口中的签名signature
*/
function jssdk( $appid , $appsecret ,$access_token){
//获取缓存的access_token
$access_token = S('access_token');
if( $access_token =="" ) {
$jsoninfo = $this->get_access_token( $appid, $appsecret );
$access_token = $jsoninfo['access_token'];
S('access_token' , $access_token , );
} $jsapi = file_get_contents("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi");
$jsapi = json_decode($jsapi);
$j = get_object_vars($jsapi);
$jsapi = $j['ticket'];//get JSAPI $time = ;
$noncestr= $time;
$jsapi_ticket= $jsapi;
$timestamp=$time;
$url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$and = "jsapi_ticket=".$jsapi_ticket."&noncestr=".$noncestr."×tamp=".$timestamp."&url=".$url."";
$signature = sha1($and);
return $signature;
} //======================================================================================================================================= /**
* 若在TP框架中,可用此方法将微信JS接口所需要的参数传递至模板中
*/
function js_param_to_temp( $appid , $appsecret, $access_token ) {
//获取缓存的access_token
$access_token = S('access_token');
if( $access_token =="" ) {
$jsoninfo = $this->get_access_token( $appid, $appsecret );
$access_token = $jsoninfo['access_token'];
S('access_token' , $access_token , );
}
$this->assign('appid' , $appid);
$jsapi = file_get_contents("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token."&type=jsapi");
$jsapi = json_decode($jsapi);
//将对象转为数组
$j = get_object_vars($jsapi);
$jsapi = $j['ticket'];//get JSAPI $time = time();
$noncestr= $time;
$this->assign('noncestr' , $noncestr);
$jsapi_ticket= $jsapi;
$timestamp=$time;
$this->assign('timestamp' , $timestamp);
$url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$and = "jsapi_ticket=".$jsapi_ticket."&noncestr=".$noncestr."×tamp=".$timestamp."&url=".$url."";
$signature = sha1($and);
$this->assign('signatrue' , $signature); } //======================================================================================================================================= /**
* 构造获取用户信息所需要的code,的链接
* $jump_url:为用户授权后的跳转地址,可用get方式获取code,且只能使用一次
* $scope:为应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),
* snsapi_userinfo: (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息)
*/
function construct_getcode_url( $appid, $jump_url, $scope="snsapi_userinfo" ) {
$jump_u = urlencode( $jump_url );
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $appid .
"&redirect_uri=" . $jump_url . "&response_type=code&scope=" . $scope . "&state=1#wechat_redirect";
return $url;
} //======================================================================================================================================= /**
* 获取网页授权access_token
* 所需要的参数必须有用户授权后返回的code值
* 返回值为array类型
* 包括:access_token、expires_in、refresh_token、scope、unionid
*/
function get_access_token_web( $appid, $appsecret, $code ) {
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=". $appid ."&secret=". $appsecret ."&code=". $code ."CODE&grant_type=authorization_code";
$output = https_request( $url );
$jsoninfo = json_decode($output, true);
return $jsoninfo;
} //======================================================================================================================================= /**
* 检验access_token是否有效
* access_token为网页授权接口凭证
* openid为用户ID
*/
function check_access_token( $access_token , $openid ) {
$url = "https://api.weixin.qq.com/sns/auth?access_token=". $access_token ."&openid=". $openid ;
$output = https_request( $url );
$jsoninfo = json_decode($output, true);
if( $jsoninfo['errcode'] == ) {
return true;
} else {
return false;
}
} //======================================================================================================================================= /**
* 数据请求
* @return string $output 返回的数据,未解码
*/
function https_request($url, $data = null){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, );
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, );
$output = curl_exec($curl);
curl_close($curl);
return $output;
} //======================================================================================================================================= /**
* 此接口唯一凭证access_token为基础凭证
* 返回的信息全面
*/
function get_user_info( $access_token, $openid ) {
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=". $access_token ."&openid=". $openid ."&lang=zh_CN";
$output = https_request( $url );
$jsoninfo = json_decode($output, true);
return jsoninfo;
} //======================================================================================================================================== /**
* 此接口为用公众号接口唯一凭证access_token和用户openid获取用户信息,包括订阅、头像、昵称等详细信息
* @param string $access_token :公众号接口唯一凭证
* @param string $openid :用户openid
* @return array :用户详细信息
*/
function get_user_if( $access_token, $openid ) {
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=". $access_token ."&openid=" . $openid . "&lang=zh_CN";
$output = https_request( $url );
$jsoninfo = json_decode($output, true);
return $jsoninfo;
} //======================================================================================================================================== /**
* 菜单类处理 (***未完善***)
* @param access_token:公众号基础接口
* @param action:操作:0:创建;1:查询;2:删除;3:获取菜单配置接口
* @param data:菜单处理json数据
* @return info:处理结果
*/
function menu_handle( $access_token, $action, $data ) {
switch ($action) {
case :
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $access_token;
//post方式提交
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, );
curl_setopt($ch, CURLOPT_AUTOREFERER, );
//带上数据
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//反馈数据
$info = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Errno'.curl_error($ch);
}
curl_close($ch);
break;
case :
$url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" . $access_token;
$info = https_request( $url );
break;
case :
$url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" . $access_token;
$info = https_request( $url );
break;
case :
$url = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=" . $access_token;
$info = https_request( $url );
break;
}
return $info; }

php获取微信用户信息(没测试过)的更多相关文章

  1. python flask获取微信用户信息报404,nginx问题

    在学习flask与微信公众号时问题,发现测试自动回复/wechat8008时正常,而测试获取微信用户信息/wechat8008/index时出现404.查询资料后收发是nginx配置问题. 在loca ...

  2. SpringBoot中获取微信用户信息从未如此简单!

    前言 不知道你是否参加过拼多多上邀请微信好友砍价功能,这个功能实现首先需要考虑的就是获取微信用户的信息.获取用户信息就是获取公众号下微信用户的信息,今天我就来讲讲如何从公众号下获取微信用户信息. 需要 ...

  3. java、JavaScript获取微信用户信息登录优化方案

    1.获取微信用户信息要调用微信的好几个接口,再加上自己系统的接口就会变的很慢,影响用户体验,之前走过的弯路我就不赘述了,直接说新的方案. 2.第一步都是向微信发起获取用户code请求: 请求接口:ht ...

  4. Magicodes.WeiChat——使用OAuth 2.0获取微信用户信息

    使用Magicodes.WeiChat,可以很方便的获取到微信用户的信息.在使用OAuth 2.0之前,你先需要做以下操作: 1)在开发者中心修改[网页授权获取用户基本信息],在弹出的界面输入自己的根 ...

  5. 小白学react之网页获取微信用户信息

    通过上一篇<小白学react之EJS模版实战>我们学习了怎样通过EJS模版生成我们高定制化的index.html文件. 本篇我们将会继续延续我们的alt-tutorial项目的实战计划.去 ...

  6. python flask获取微信用户信息流程

    需要了解的几个url 用户第一次访问时的url,包含以下几个参数 https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID& ...

  7. Java 微信登录授权后获取微信用户信息昵称乱码问题解决

    String getUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+access_toke ...

  8. Java微信公众平台开发(十二)--微信用户信息的获取

    转自:http://www.cuiyongzhi.com/post/56.html 前面的文章有讲到微信的一系列开发文章,包括token获取.菜单创建等,在这一篇将讲述在微信公众平台开发中如何获取微信 ...

  9. Java微信公众平台开发(十)--微信用户信息的获取

    前面的文章有讲到微信的一系列开发文章,包括token获取.菜单创建等,在这一篇将讲述在微信公众平台开发中如何获取微信用户的信息,在上一篇我们有说道微信用户和微信公众账号之间的联系可以通过Openid关 ...

随机推荐

  1. springMVC学习(6)-包装pojo类型、数组、list、Map类型参数绑定

    一.包装类型pojo参数绑定: 需求:商品查询controller方法中实现商品查询条件传入. 实现方法: 1)在形参中 添加HttpServletRequest request参数,通过reques ...

  2. appium 5-27屏幕旋转、

    1.屏幕切换 注意:应用一定要支持横竖屏切换,否则无效果, public void testBrowser() throws InterruptedException { Thread.sleep(1 ...

  3. 接口测试3-3Excel格式

    java操作Excel,需要第三方库poi #xml <dependency> <groupId>org.apache.poi</groupId> <arti ...

  4. MTR追踪的好工具

    yum install mtr 或者win下的winmtr 直接可以统计.

  5. Linux系统查看系统硬件配置信息

    1.查看CPU信息 # 查看cpu负载 uptime # cpu使用率 (没有sar 则yum -y install sysstat) sar top bn1 |grep %Cpu # 每个cpu使用 ...

  6. Delphi Webbrowser使用方法详解(二)

    delphi如何用webbrowser模拟登录网站? 我们就以如何登录博客园来做示例: 1.要登入一个网站,首先要获取网页的源代码,我们可以通过网页菜单--查看--查看源代码来获取. 2.我们找到登录 ...

  7. 部署mariadb数据库到linux(源码编译安装)

    各种库: apt install -y build-essential zlib1g-dev libpcre3 libpcre3-dev unzip cmake libncurses5-dev lib ...

  8. python logging模块使用教程

    简单使用 #!/usr/local/bin/python # -*- coding:utf-8 -*- import logging logging.debug('debug message') lo ...

  9. 资源 | 源自斯坦福CS229,机器学习备忘录在集结

    在 Github 上,afshinea 贡献了一个备忘录对经典的斯坦福 CS229 课程进行了总结,内容包括监督学习.无监督学习,以及进修所用的概率与统计.线性代数与微积分等知识. 项目地址:http ...

  10. fio与dd测试结果记录

    以下测试基于win7内安装的vbox虚机内进行. vbox-vm挂载了7.2k disk作为本地系统盘,挂载了ssd 8G空间作为mount /mnt/data /dev/sdb 今天顺便了做个一个简 ...