分类 微信开发 下的文章

微信开发之获取用户详细信息

    public function wxlogin($code = null)
    {
        //判断是否微信浏览器打开,并判断是否登录
        if ((strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')) !== false && empty($_SESSION['openid']) || empty($_SESSION['userid']) || empty($_SESSION['username'])) {
       //引导用户进入授权页面同意授权,获取code
            if ($code !== null) {
                //获取openID
                $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?';
                $params = array(
                    'appid'     => '',
                    'secret'    => '',
                    'code'      => $code,
                    'grant_type'=> 'authorization_code',
                );
                $openId = json_decode(file_get_contents($url . http_build_query($params)), true);
                if(!isset($openId['openid'])) $openId['openid'] = -1;
                //设置session
                $_SESSION['openid']  = $openId['openid'];
                //判断数据库是否存在该用户信息
                $url = 'http://'.SITE_DOMAIN.'user/isuser?';
                $params = array(
                    'openid'     => $openId['openid'],
                    'token'      => md5('cunzaiganit.com'),
                );
                $user = json_decode(file_get_contents($url . http_build_query($params)), true);
                //如果数据库没有该用户信息
                if($user['result']){
                    //获取用户信息
                    $url = 'https://api.weixin.qq.com/sns/userinfo?';
                    $params = array(
                        'access_token' => $openId['access_token'],
                        'openid' => $openId['openid'],
                        'lang' => 'zh_CN'
                    );
                    $info = json_decode(file_get_contents($url . http_build_query($params)), true);

                    //将信息存数据库
                    $url = 'http://'.SITE_DOMAIN.'user/adduser?';
                    $params = array(
                        'openid'     => $info['openid'],
                        'name'     => $info['nickname'],
                        'login_name'     => $info['nickname'],
                        'sex'     => $info['sex'],
                        'province'     => $info['province'],
                        'city'     => $info['city'],
                        'country'     => $info['country'],
                        'headlink'     => $info['headimgurl'],
                    );
                    $rows = json_decode(file_get_contents($url . http_build_query($params)),true);
                    $_SESSION['userid']  = $rows['id'];
                    $_SESSION['username']  = $rows['name'];
                } else {
                    //如果有该用户
                    $_SESSION['userid']  = $user['id'];
                    $_SESSION['username']  = $user['name'];
                }
            } else {
                $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?';

                $params = array(
                    'appid'         => '',
                    'redirect_uri'  => 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'],
                    'response_type' => 'code',
                    'scope'         => 'snsapi_userinfo',
                    'state'         => md5('baidu.com') . '#wechat_redirect'
                );
                die(header('Location:'.$url . http_build_query($params)));
            }
        }
    }

微信开发之模板消息实例


public function test()
{
        //获取token
        $wxtoken = $this->jssdks->getToken();
        //拼接URL
        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$wxtoken;
        //发送需要发送的模板信息内容
        $params = json_encode(array(
            'touser' => "openid",//接收者的OpenId
            'template_id' => '',//模板ID,可去微信公众号后台查看
            "url" => "http://".STYLE_DOMAIN,//用户接收到信息,点击后挑战的地址
            'topcolor' => "#FF0000",
            'data' => array(
                //以下内容可参考微信公众号后台填写
                'first' => array(
                    "value" => "业务到期提示",
                    "color" => "#173177"
                ),
                'type' => array(
                    "value" => "到期提示",
                    "color" => "#173177"
                ),
                'date' => array(
                    "value" => date('Y-m-d'),
                    "color" => "#173177"
                ),
                'remark' => array(
                    "value" => '本业务将在月底自动取消。如需续用,请您在到期前点击链接,根据指引办理业务。感谢您的体验!',
                    "color" => "#173177"
                ),
            )
            )
        );
        //调用封装好的Curl发送数据。
        $result = $this->getapi->get_json_decode($params,$url);
 }

最新文章

最近回复

  • 裤衩哥:这文章我转了,而且我还以我自己的名字发了。2333333,你来咬...
  • :怎么生成带logo的二维码呀????
  • zirmi:哎呀,你的域名爆破工具 输入的字符不够啊
  • 小余:二维码如何弄啊,哥
  • 裤衩哥:可以要你的菊花吗?
  • admin:楼上+1
  • admin:吃瓜群众 为 落魂 打call
  • :牛逼了..........
  • 帮帮:厉害!谢谢了朋友!解决了我的问题。
  • 落魂:好多小伙伴找我要这个CSS样式,其实这个是使用的一套模块!大家可...

分类

归档

友情链接