php 微信接口开发实例,微信公共号开发基础接口 thinkphp实例

【实例简介】

【实例截图】

【核心代码】

namespace Home\Controller;

use Think\Controller;

use Org\Net\Wechat;

class IndexController extends Controller {

protected $weObj;

private $apihost = "http://你的网址";

// private $redirect_uri = $this->apihost."/wx.php/Home/Index/callback";

public function _initialize(){

$options = C('WX_CONFIG');

$this->weObj = new Wechat($options);

}

public function index(){

$this->weObj->valid();

$type = $this->weObj->getRev()->getRevType();

switch($type) {

case Wechat::MSGTYPE_TEXT:

$this->weObj->text("hello, I'm wechat")->reply();

exit;

break;

case Wechat::MSGTYPE_EVENT:

$event = $this->weObj->getRev()->getRevEvent();

$openid = $this->weObj->getRev()->getRevFrom();

if($event['event'] == 'subscribe'){

$id = $this->addUser($openid);

$this->weObj->text("您是第".$id."个用户")->reply();

}

if($event['event'] =='CLICK' && $event['key'] == 'MENU_KEY_NEWS')

{

$this->weObj->text('点击查看用户资料')->reply();

}

break;

case Wechat::MSGTYPE_IMAGE:

break;

default:

$this->weObj->text("help info")->reply();

}

}

private function addUser($openid){

$mod = D('User');

$res = $mod->where(array('openid'=>$openid))->getField('id');

if($res) return $res;

$data = $this->weObj->getUserInfo($openid);

$data['login_ip'] = get_client_ip();

$data['add_time'] = time();

$res = $mod->add($data);

return $res;

// $mod->add

}

public function userInfo($openid){

$openid = I('get.openid');

if(!$openid) return false;

$data = D('User')->where(array('openid'=>$openid))->find();

foreach ($data as $k => $v) {

echo "

".$k.'---'.$v."

";

}

}

public function menu(){

//获取菜单操作:

$menu = $this->weObj->getMenu();

//设置菜单

$newmenu =  array(

"button"=>

array(

array('type'=>'click','name'=>'查看用户消息','key'=>'MENU_KEY_NEWS'),

array('type'=>'view','name'=>'登录查看消息','url'=>$this->apihost.'/wx.php/Home/Index/getUser'),

)

);

$result = $this->weObj->createMenu($newmenu);

}

public function getUser($value='')

{

$options = C('WX_CONFIG');

$auth = new \Org\Net\WechatAuth($options['appid'],$options['appsecret']);

$url = $auth->getRequestCodeURL($redirect_uri=$this->apihost."/wx.php/Home/Index/callback", '','snsapi_base');

redirect($url);

}

public function callback(){

$options = C('WX_CONFIG');

$auth = new \Org\Net\WechatAuth($options['appid'],$options['appsecret']);

$code = I('get.code');

$auth->getAccessToken('code', $code);

$openid = $auth->openid;

$data = $auth->getUserInfo($openid, $lang = 'zh_CN');

foreach ($data as $k => $v) {

    if($k =='headimgurl') echo '';

echo "

".$k."---".$v."
";

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值