php获取微信用户信息(没测试过)
<?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获取微信用户信息(没测试过)的更多相关文章
- python flask获取微信用户信息报404,nginx问题
在学习flask与微信公众号时问题,发现测试自动回复/wechat8008时正常,而测试获取微信用户信息/wechat8008/index时出现404.查询资料后收发是nginx配置问题. 在loca ...
- SpringBoot中获取微信用户信息从未如此简单!
前言 不知道你是否参加过拼多多上邀请微信好友砍价功能,这个功能实现首先需要考虑的就是获取微信用户的信息.获取用户信息就是获取公众号下微信用户的信息,今天我就来讲讲如何从公众号下获取微信用户信息. 需要 ...
- java、JavaScript获取微信用户信息登录优化方案
1.获取微信用户信息要调用微信的好几个接口,再加上自己系统的接口就会变的很慢,影响用户体验,之前走过的弯路我就不赘述了,直接说新的方案. 2.第一步都是向微信发起获取用户code请求: 请求接口:ht ...
- Magicodes.WeiChat——使用OAuth 2.0获取微信用户信息
使用Magicodes.WeiChat,可以很方便的获取到微信用户的信息.在使用OAuth 2.0之前,你先需要做以下操作: 1)在开发者中心修改[网页授权获取用户基本信息],在弹出的界面输入自己的根 ...
- 小白学react之网页获取微信用户信息
通过上一篇<小白学react之EJS模版实战>我们学习了怎样通过EJS模版生成我们高定制化的index.html文件. 本篇我们将会继续延续我们的alt-tutorial项目的实战计划.去 ...
- python flask获取微信用户信息流程
需要了解的几个url 用户第一次访问时的url,包含以下几个参数 https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID& ...
- Java 微信登录授权后获取微信用户信息昵称乱码问题解决
String getUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+access_toke ...
- Java微信公众平台开发(十二)--微信用户信息的获取
转自:http://www.cuiyongzhi.com/post/56.html 前面的文章有讲到微信的一系列开发文章,包括token获取.菜单创建等,在这一篇将讲述在微信公众平台开发中如何获取微信 ...
- Java微信公众平台开发(十)--微信用户信息的获取
前面的文章有讲到微信的一系列开发文章,包括token获取.菜单创建等,在这一篇将讲述在微信公众平台开发中如何获取微信用户的信息,在上一篇我们有说道微信用户和微信公众账号之间的联系可以通过Openid关 ...
随机推荐
- 【数据库】mysql的安装
打开下载的mysql安装文件mysql-5.0.27-win32.zip,双击解压缩,运行“setup.exe”,出现如下界面 mysql安装向导启动,按“Next”继续 选择安装类型,有“Typic ...
- dede模块管理一片空白或没有列表内容的解决办法
为什么dede后台模块管理,打开之后一片空白,又或者没有列表,插件与其他模块的使用也是正常的. 这主要是因为我们在安装模块,然后又卸载模块,卸载的时候选择了删除安装文件,就会出这个问题. 这里面分为两 ...
- dedeampz安装wordpress程序,然后新增数据库方法
看到一篇文章,用dedeampz安装wordpress程序,增加新的数据库的. 安装方法很简单,就是把..\DedeAMPZ\WebRoot\Default 下面的dede的代码 可以给移走放到别的地 ...
- Google.ProtocolBuffers.dll 之.Net应用(一)
原创文章,转载必需注明出处:http://www.cnblogs.com/wu-jian/ http://www.cnblogs.com/wu-jian/archive/2011/02/22/1961 ...
- C/C++动态分配连续空间,下标越界导致的free():invalid next size问题
昨天帮导师做的一个程序出了内存泄露的bug(在VS上程序运行一切正常,等return返回后才出错) 而且是程序运行结束后才出现的错误,在退出前一切代码都顺利执行完了,只是return之后出错. 之后我 ...
- Sigar简介
大家好,我是Sigar.也许好多人还不认识我.下面就介绍一下我自己,好让大家对我有一个大致的了解. 我的全名是System Information Gatherer And Reporter,中文名是 ...
- python selenium-7自动发送邮件
https://jingyan.baidu.com/article/647f0115b78f8d7f2148a8e8.html 1.发送HTML格式的邮件 import smtplib from em ...
- Windows 应用程序交互过程
应用程序 Windows的应用程序一般包含窗口(Window),它主要为用户提供一种可视化的交互方式(窗口是由线程(Thread)创建的).Windows 系统通过消息机制来让系统和用户进行交互 ...
- 本地管理表空间和字典管理表空间的特点,ASSM有什么特点
字典管理表空间(Dictionary-Managed Tablespace简称DMT),8i以前包括以后都还可以使用的一种表空间管理模式,通过数据字典管理表空间的空间使用. Oracle使用两个字典来 ...
- pycharm格式报错: Remove redundant parentheses
处理:所在代码行,最外层括号去掉