微信企业号(获取微信用户信息)

<?php
class IndexAction extends Action
{
    public function index(){
$code = $this->_GET('code');
        $userid = $this->UserID($code);
        $userid = json_decode($userid , true);
        $userid = $userid['UserId'];


        $AccessToken = $this->AccessToken();
        $AccessToken = json_decode($AccessToken , true);
        $AccessToken = $AccessToken['access_token'];
        // 获取手机号
        $url = 'https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token='.$AccessToken.'&userid='.$userid;
        $get_user = $this->request_post($url,$ispost=false);
        $get_user = json_decode($get_user , true);
        $where['parentstel'] = $get_user['mobile'];
        $parents = M('parents')->where($where)->find();
        $where = "";
        $where['parentsid'] = $parents['id'];
        $child = M('cprelative')->field('childid')->where($where)->find();
        $where = "";
        cookie('nurseryid',$parents['nurseryid']);
        cookie('parentsid',$parents['id']);
        cookie('childid',$child['childid']);
        cookie('classid',$parents['classid']);
        // cookie('nurseryid',221);
        // cookie('parentsid',1703);
        // cookie('classid',454);
        // cookie('nurseryid',4);
        // cookie('parentsid',1923);
        // cookie('classid',2);
        // cookie('childid',2226);
        echo "<script type='text/javascript'>
            window.location.href='".__APP__."/Dynamic/index';</script>";
}
public function index1()
{
// 获取code
echo "<script type='text/javascript'>
window.location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=可信域名&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';</script>";
}
public function AccessToken()
    {
        $url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=appid&corpsecret=secret';
        $AccessToken = $this->request_post($url,$ispost=false);
        return $AccessToken;
    }
    public function UserID($code)
    {
        $AccessToken = $this->AccessToken();
        $AccessToken = json_decode($AccessToken , true);
        $AccessToken = $AccessToken['access_token'];
        // 获取uerid
        $url ='https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token='.$AccessToken.'&code='.$code;
        $userid = $this->request_post($url,$ispost=false);
        return $userid;
    }
    public function request_post($url = '',$ispost=true, $post_data = array()) {
        $o = "";
        foreach ( $post_data as $k => $v ) 
        { 
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        $post_data = substr($o,0,-1);
        $key=md5(base64_encode($post_data));
        $curlPost = 'key='.$key;
        header("Content-type: text/html; charset=utf-8");
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$url);//抓取指定网页        
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
        if($ispost){
            var_dump($curlPost);
            curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
            curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        }
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        return $data;
    }
}
?>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值