thinkphp 开发的获取用户信息
2016-03-10 08:28
615 查看
<?php namespace Home\Controller; use Think\Controller; use Com\Wechat; use Com\WechatAuth; class IndexController extends Controller { public function index(){ $token="weixin"; $wechat=new Wechat($token);
$data=$wechat->request();
if($data && is_array($data)){
switch($data['MsgType']){ case "text" : $this->Text($wechat,$data); }
} }
//回复文本消息 private function Text($wechat,$data){
if(strstr($data['Content'],"文本")){ $text="我正在使用Thinkphp开发微信";
$this->logger("发送消息:\n".$text);
$wechat->replyText($text);
}else if(strstr($data['Content'],"myself")){
$this->users($wechat,$data); }
}
private function users($wechat,$data){ session_start(); $openid=$data['FromUserName']; $appid="wx4dae5d61b7f9935c"; $appSecret="24a91315a1a62a9efe323accf5c93839"; $token=session('token') ;
if($token){
$WechatAuth=new WechatAuth($appid,$appSecret,$token);
}else{ $WechatAuth=new WechatAuth($appid,$appSecret);
$accsseToken=$WechatAuth->getAccessToken(); $token=$accsseToken['access_token']; session('token',$token); } $user=$WechatAuth->userInfo($openid); $text="你的openid是:".$user['openid']."\n你的昵称是:".$user['nickname']."\n 你的性别是:".$user['sex']."\n你的城市是:".$user['city']."\n你所在国家是".$user['country']."\n 你在的省份是:".$user['province'];
$this->logger("发送用户的信息".$text); $wechat->replyText($text); }
private function logger($content){ $logSize=100000;
$log="log.txt";
if(file_exists($log) && filesize($log) > $logSize){ unlink($log); }
file_put_contents($log,date('H:i:s')." ".$content."\n",FILE_APPEND);
} }
$data=$wechat->request();
if($data && is_array($data)){
switch($data['MsgType']){ case "text" : $this->Text($wechat,$data); }
} }
//回复文本消息 private function Text($wechat,$data){
if(strstr($data['Content'],"文本")){ $text="我正在使用Thinkphp开发微信";
$this->logger("发送消息:\n".$text);
$wechat->replyText($text);
}else if(strstr($data['Content'],"myself")){
$this->users($wechat,$data); }
}
private function users($wechat,$data){ session_start(); $openid=$data['FromUserName']; $appid="wx4dae5d61b7f9935c"; $appSecret="24a91315a1a62a9efe323accf5c93839"; $token=session('token') ;
if($token){
$WechatAuth=new WechatAuth($appid,$appSecret,$token);
}else{ $WechatAuth=new WechatAuth($appid,$appSecret);
$accsseToken=$WechatAuth->getAccessToken(); $token=$accsseToken['access_token']; session('token',$token); } $user=$WechatAuth->userInfo($openid); $text="你的openid是:".$user['openid']."\n你的昵称是:".$user['nickname']."\n 你的性别是:".$user['sex']."\n你的城市是:".$user['city']."\n你所在国家是".$user['country']."\n 你在的省份是:".$user['province'];
$this->logger("发送用户的信息".$text); $wechat->replyText($text); }
private function logger($content){ $logSize=100000;
$log="log.txt";
if(file_exists($log) && filesize($log) > $logSize){ unlink($log); }
file_put_contents($log,date('H:i:s')." ".$content."\n",FILE_APPEND);
} }
相关文章推荐
- PHP Date ( I need to use)
- 2016/3/10 PHP (超文本预处理器) 是什么?
- PHP二维码生成的方法(google APi,PHP类库,libqrencode等)
- 利用notepad++运行php
- PHP: fopen() Permission denied
- PHP实现仿百度文库,豆丁在线文档效果(word,excel,ppt转flash)
- 简单谈谈php浮点数精确运算
- PHP浮点数的一个常见问题
- Zend Framework教程之配置文件application.ini解析
- Zend Framework教程之Application和Bootstrap用法详解
- php实现无限级分类查询(递归、非递归)
- 我的PHP退役了
- weiphp 与sina云应用实践
- FileInputStream与FileOutputstream
- PHP 类的访问控制与封装 public,protected,private 修饰符
- TP-link 无线路由器WDS设置方法图解_无线桥接设置 wifi无线变有线路由 中转站
- 关于vsftpd下显示的修改时间与系统时间不一致的解决方法
- 启用vsftpd日志及其解读
- 启用vsftpd日志及其解读
- php 无法加载php.ini问题