您的位置:首页 > 编程语言 > PHP开发

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);

} }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: