您的位置:首页 > 移动开发 > 微信开发

微信php接入设计案列

2016-03-10 09:20 363 查看
<?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); break; case 'image': $this->replayphoto($Wechat,$data); default: break; } } }

//回复文本消息 private function Text($wechat,$data){

if(strstr($data['Content'],"你")||strstr($data['Content'],"n")||strstr($data['Content'],"开发人")||strstr($data['Content'],"h")||strstr($data['Content'],"w")){ $text="我是ULIKE工作室,期待您的加入,欢迎添加我的微信号ideal_person_Li!";

$this->logger("发送消息:\n".$text);

$wechat->replyText($text);

}else if(strstr($data['Content'],"我")){

$this->users($wechat,$data); }

} public function replayphoto($Wechat,$data) { $Wechat->replyImage($data['MediaId']); } private function users($wechat,$data){ $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']." 你的性别是:".$user['sex']."\n你的城市是:".$user['city']."\n你所在国家是".$user['country']." 你在的省份是:".$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);

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