微信订阅号 获取用户基本信息,登录及 php
<?php
//echo file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx20122233338996&secret=67911111111111111111111146fb11113");
//die;
$testObj = new Test();
$_SESSION = '';
if($testObj->checkSignature()){
if(!empty($_SESSION['openid'])){
$uinfo = file_get_contents("https://api.weixin.qq.com/cgi-bin/user/info?access_token=hwu2rSeVQ1222222222222222222222226nEojpAbiqtY6hIB_fvEm333333333333337dbeRhL-V9hnzzLWwhXLnrTNokyROJiACAEBW&openid=".$_SESSION['openid']);
echo $uinfo;
}else{
//session为空的话
$testObj->responseMsg();
}
}else{
// $testObj->valid();
}
class Test
{
/**
* 绑定url、token信息
*/
public function valid(){
$echoStr = $_GET["echostr"];
if ($this->checkSignature()) {
echo $echoStr;
}
exit();
}
/**
* 检查签名,确保请求是从微信发过来的
*/
public function checkSignature()
{
//获得参数 signature nonce token timestamp echostr
$nonce = $_GET['nonce'];
$token = 'dxxxxxxxxx';
$timestamp = $_GET['timestamp'];
$echostr = $_GET['echostr'];
$signature = $_GET['signature'];
//形成数组,然后按字典序排序
$array = array();
$array = array($nonce, $timestamp, $token);
sort($array);
//拼接成字符串,sha1加密 ,然后与signature进行校验
$str = sha1( implode( $array ) );
if( $str == $signature){
return true;
}else{
return false;
}
}
/**
* 接收消息,并自动发送响应信息
*/
public function responseMsg(){
//验证签名
if ($this->checkSignature()){
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
// $this->log_request_info();
//提取post数据
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;//发送人
$toUsername = $postObj->ToUserName;//接收人
$MsgType = $postObj->MsgType;//消息类型
$MsgId = $postObj->MsgId;//消息id
$time = time();//当前时间做为回复时间
//如果是文本消息(表情属于文本信息)
if($MsgType == 'text'){
$content = trim($postObj->Content);//消息内容
if(!empty( $content )){
//如果文本内容是图文,则回复图文信息,否则回复文本信息
if($content == "图文"){
//回复图文消息,ArticleCount图文消息个数,多条图文消息信息,默认第一个item为大图
$ArticleCount = 2;
$newsTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>%s</ArticleCount>
<Articles>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
</Articles>
</xml>";
$resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time, 'news',
$ArticleCount,'我是图文信息','我是描述信息','http://www.test.com/DocCenterService/image?photo_id=236',
'http://www.baidu.com','正式开通上线','描述2','http://jn.test.com/ac/skins/img/upload/img/20131116/48171384568991509.png',
'http://www.jd.com');
echo $resultStr;
$this->log($resultStr);
}else{
//回复文本信息
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
$contentStr = '你发送的信息是:接收人:'.$toUsername.',发送人:'.$fromUsername.',消息类型:'.$MsgType.',消息内容:'.$content.' www.icity365.com';
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $contentStr);
echo $resultStr;
$this->log($resultStr);
}
}else{
echo "Input something...";
$this->log($resultStr);
}
//如果是图片消息
}elseif ($MsgType == 'image'){
$MediaId = $postObj->MediaId;//图片消息媒体id,可以调用多媒体文件下载接口拉取数据。
$imageTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Image>
<MediaId><![CDATA[%s]]></MediaId>
</Image>
</xml>";
$resultStr = sprintf($imageTpl, $fromUsername, $toUsername, $time, $MsgType, $MediaId);
echo $resultStr;
$this->log("自动响应图片信息");
$this->log($resultStr);
//如果是视频
}else if($MsgType == 'video'){
$MediaId = $postObj->MediaId;//视频消息媒体id,可以调用多媒体文件下载接口拉取数据。
$ThumbMediaId = $postObj->ThumbMediaId;//视频消息缩略图的媒体id,可以调用多媒体文件下载接口拉取数据。
$videoTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Video>
<MediaId><![CDATA[%s]]></MediaId>
<ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
</Video>
</xml>";
$resultStr = sprintf($videoTpl, $fromUsername, $toUsername, $time, $MsgType, $MediaId,$ThumbMediaId,'我是标题','我是描述');
echo $resultStr;
$this->log("自动响应视频信息".$ThumbMediaId);
$this->log($resultStr);
//如果是地理位置
}else if($MsgType == 'location'){
$Location_X = $postObj->Location_X;//维度
$Location_Y = $postObj->Location_Y;//经度
$Scale = $postObj->Scale;//地图缩放大小
$Label = $postObj->Label;//地里位置信息
//回复文本信息
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
$msgType = "text";
$contentStr = '经度:'.$Location_Y.',维度:'.$Location_X.',地图缩放大小'.$Scale.',地理位置信息:'.$Label;
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
$this->log($resultStr);
//如果是事件
}else if($MsgType == 'event'){
$Event = $postObj->Event;
//subscribe(关注,也叫订阅)
if($Event == 'subscribe'){
$EventKey = $postObj->EventKey;//事件KEY值,qrscene_为前缀,后面为二维码的参数值
//未关注时,扫描二维码
if(!empty($EventKey)){
$Ticket = $postObj->Ticket;//二维码的ticket,可用来换取二维码图片
$this->log($fromUsername.'扫描二维码关注!EventKey='.$EventKey.',Ticket='.$Ticket);
}else{
$this->log($fromUsername.'关注我了!');
}
//unsubscribe(取消关注)
}elseif ($Event == 'unsubscribe'){
$this->log($fromUsername.'取消关注我了!');
//已关注时,扫描二维码事件
}elseif($Event == 'SCAN' || $Event == 'scan'){
$EventKey = $postObj->EventKey;//事件KEY值,是一个32位无符号整数,即创建二维码时的二维码scene_id
$Ticket = $postObj->Ticket;//二维码的ticket,可用来换取二维码图片
$this->log($fromUsername.'关注我了!EventKey='.$EventKey.',Ticket='.$Ticket);
//菜单点击事件
}elseif($Event == 'CLICK'){
$EventKey = $postObj->EventKey;//事件KEY值,与自定义菜单接口中KEY值对应
//回复文本信息
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
// $uinfo = file_get_contents("https://api.weixin.qq.com/cgi-bin/user/info?access_token=hwu2rSeVQay064G3e6k1111111111111111AbiqtY6hIB_fv111111111111111117dbeRhL-V9hnzzLWwhXLnrTNokyROJiACAEBW&openid=".$postObj->FromUserName);
// $uinfo = '';
$_SESSION['openid'] = $postObj->FromUserName;
$contentStr = '你点击了菜单,菜单项key='.$EventKey.'and 你的openid是'.$postObj->FromUserName;
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $contentStr);
echo $resultStr;
$this->log($resultStr);
//其他事件类型
}else{
$this->log('事件类型:'.$Event);
}
//其他消息类型,链接、语音等
}else{
//回复文本信息
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
$contentStr = '消息类型:'.$MsgType.'我们还没做处理。。。。【】';
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $contentStr);
echo $resultStr;
$this->log($resultStr);
}
}else {
echo "";
exit;
}
}else{
$this->log("验证签名未通过!");
}
}
/**
* 记录请求信息
*/
function log_request_info() {
$post = '';
foreach($_POST as $key => $value) {
$post = $post.$key.' : '.$value.' , ';
}
$get = '';
foreach($_GET as $key => $value) {
$get = $get.$key.' : '.$value.' , ';
}
$this->log("get信息:".$get);
$this->log("post信息:".$post);
}
/**
* 记录日志
* @param $str
* @param $mode
*/
function log($str){
echo $str;die;
$mode='a';//追加方式写
$file = "log.txt";
$oldmask = @umask(0);
$fp = @fopen($file,$mode);
@flock($fp, 3);
if(!$fp)
{
Return false;
}
else
{
@fwrite($fp,$str);
@fclose($fp);
@umask($oldmask);
Return true;
}
}
}
?>
微信订阅号 获取用户基本信息,登录及 php的更多相关文章
- 微信网页授权获取用户基本信息--PHP
现在就说说怎么通过网页授权获取用户基本信息(国家,省,市,昵称)等. 必要条件: 1)公众号认证 2)有网页授权获取用户基本信息的权限接口 注意:最近有朋友说:在公众平台申请的测试号,会出现无法取到用 ...
- 微信网页授权+获取用户基本信息+强制关注+JSSDK分享参数
网页授权+获取用户基本信息+强制关注+JSSDK分享参数 //支付宝红包口令列表 public function view(){ $openid = ""; Vendor('Wei ...
- 微信公众号开发用户授权登录报"redirect_uri 参数错误"错误
微信公众号开发 授权获取用户信息报错 "redirect_uri 参数错误" 出现这个情况要检查下 微信公众号配置了网页授权域名 在这里配置微信公众号redirect_uri中的域 ...
- 微信 网页授权获取用户基本信息(OAuth 2.0)
// 相关设置 $APPID = ""; $AppSecret = ""; $html = ""; // 拼接 URL // 跳转该连接 获 ...
- php,微信公众号,获取用户地理位置 定位 经纬度
<?php //php插件下载地址: https://files.cnblogs.com/files/fan-bk/jssdk_php.rar //建立一个php文件 require_once ...
- 微信公众号获取用户openId How to use cURL to get jSON data and decode the data?
w http://stackoverflow.com/questions/16700960/how-to-use-curl-to-get-json-data-and-decode-the-data
- Magicodes.WeiChat——WeChatOAuthTest(网页授权获取用户基本信息)
Demo访问地址:http://wechat.magicodes.net/app/AppDemo/WeChatOAuthTest?tenantId=1 关于公众号如何获取用户信息,请参考此文档:htt ...
- 微信订阅号里实现oauth授权登录,并获取用户信息 (完整篇)
摘要 这段时间一直有人问我,订阅号实现的oauth授权登录的问题,之前写的比较简单,很多人不明白.众所周知,微信公众号分订阅号.服务号.企业号:每个号的用途不一样,接口开放程度也不一样.微信还有个扯淡 ...
- C#微信公众号开发之网页授权oauth2.0获取用户基本信息(一)
咨询 请加 QQ::QQ群: 在微信里面,非认证的公众号账号,只能通过在微信回复菜单单击等事件获取openid,但是认证的公众账号(之前认证的订阅号是不可以的,现在新开放了政府媒体机构的认证订阅号)可 ...
随机推荐
- 从零开始的全栈工程师——JS面向对象(复习)
作用域 栈内存:js执行的环境堆内存:存放代码块的空间 存放方式 键值对形式存放 字符串的形式存放js在执行之前 浏览器会给他一个全局作用域叫window 每个作用域下都分为两个模块 一个是内存模块一 ...
- TopcoderSRM679 Div1 250 FiringEmployees(树形dp)
题意 [题目链接]这怎么发链接啊..... 有一个 \(n\) 个点的树,每个点有点权(点权可能为负) ,求包含点\(1\)的最 大权连通子图(的权值和) . \(n \leqslant 2500\) ...
- 检测IE浏览器兼容Edge模式及IE11
document.documentMode || +(navigator.userAgent.match(/MSIE (\d+)/) && RegExp.$1) 判断布尔值
- 服务器bios&raid管理
新服务器配置流程 https://github.com/alces-software/knowledgebase/wiki/server-dell-rseries-r630 dell DTK(sysc ...
- (四)WebDriver常用方法
点击和输入 前面我们已经学习了定位元素, 定位只是第一步, 定位之后需要对这个元素进行操作, 或单击(按钮) 或输入(输入框) , 下面就来认识 WebDriver 中最常用的几个方法: clear( ...
- June 05th 2017 Week 23rd Monday
No great discovery was ever made without a bold guess. 没有大胆的猜测就没有伟大的发现. I've read this sentence just ...
- 48. Rotate Image (matrix retation, transpose) Amazon problem
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- C++学习之【使用位操作符求素数分析】
先放普通代码: #include <iostream> using namespace std; void getPrime_1() { const int MAXN = 100; boo ...
- SSH 本地和服务器传输
[转]https://www.cnblogs.com/magicc/p/6490566.html SCP 使用方式如下: 1.上传本地文件到服务器 scp /path/filename usernam ...
- bzoj 3339 莫队
题意: 求任意一个区间的SG函数. 想到线段树,但是线段树合并很麻烦. 线段树——分块. 分块的一个应用就是莫队算法. 怎么暴力递推呢? 从一个区间到另一个区间,Ans 取决于 Ans 和 加入和删除 ...