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

来源:互联网 发布:mysql where or 效率 编辑:程序博客网 时间:2024/06/05 15:47
<?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
原创粉丝点击