zoukankan      html  css  js  c++  java
  • 微信服务号开发-商城微信登录

    最近帮朋友写了个微信服务号,服务号名字叫做十四行诗。没错是卖月饼的商城。

    简单介绍下微信登录,与官方文档不同,简单画了一下UML图

    简单的说就是先建立了一个index.php(直接拍域名就过去了。),然后传一个appid,微信公众号后台能拿到

    <?php
    $appid = '';
    
    header('location:https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri=http://www.xxx.com/oauth.php&response_type=code&scope=snsapi_userinfo&state=123&connect_redirect=1#wechat_redirect');
    ?>

    一个回调

    header("Content-Type: text/html;charset=utf-8"); 
    $code = $_GET['code'];
    $state = $_GET['state'];
    //换成自己的接口信息
    $appid = '';
    $appsecret = '';
    if (empty($code)) $this->error('授权失败');
    $    = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
    $token = json_decode(file_get_contents($token_url));
    if (isset($token->errcode)) {
        echo '<h1>错误:</h1>'.$token->errcode;
        echo '<br/><h2>错误信息:</h2>'.$token->errmsg;
        exit;
    }

    获取到一个token

    $access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;
    //转成对象
    $access_token = json_decode(file_get_contents($access_token_url));
    if (isset($access_token->errcode)) {
        echo '<h1>错误:</h1>'.$access_token->errcode;
        echo '<br/><h2>错误信息:</h2>'.$access_token->errmsg;
        exit;
    }
    $user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN';
    //转成对象
    $user_info = json_decode(file_get_contents($user_info_url));
    if (isset($user_info->errcode)) {
        echo '<h1>错误:</h1>'.$user_info->errcode;
        echo '<br/><h2>错误信息:</h2>'.$user_info->errmsg;
        exit;
    }

    然后得到了user_info。这里面有用户的openid(唯一加密后的微信号),用户的头像headimgurl,用户的昵称nickname

    再然后把他存下来

    function send_post($url, $post_data) {
        $postdata = http_build_query($post_data);
        $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-type:application/x-www-form-urlencoded',
            'content' => $postdata,
            'timeout' => 15 * 60 // 超时时间(单位:s)
        )
        );
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
    
        return $result;
    }
    
    $post_data = array(
        'partner_code' => $user_info->openid,
        'name' => $user_info->nickname,
        'headurl' => $user_info->headimgurl
    );
    send_post('/member_register', $post_data);
    //打印用户信息
    print_r($user_info);
    
    header('location:/index.html');
    
    
    setcookie('partner_code',$user_info->openid);
    setcookie('name',$user_info->nickname);
    setcookie('headurl',$user_info->headimgurl);

    这边一共用了2种存放,一种是post到数据库,还有一种是直接存cookie

    当然这边存cookie的作用很大。后面会介绍一下。

    这边是绑定下入口链接以及

    这边就是最后的效果

  • 相关阅读:
    Civil 3D .NET二次开发第11章代码升级至2018版注意事项
    创建道路曲面
    ObjectARX® for Beginners: An Introduction
    mshcMigrate制作的mshc文件中有链接打不开
    Word 2013无法发布文章到博客园
    ionic 安装插件报错:源文本中存在无法识别的标记
    typescript文件中 使用回调函数无法调用函数外的变量和方法的办法
    ionic2---自定义插件
    angular2----使用swiper做轮播图
    angular2----router
  • 原文地址:https://www.cnblogs.com/youku/p/LoveOrHate.html
Copyright © 2011-2022 走看看