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的作用很大。后面会介绍一下。

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

    这边就是最后的效果

  • 相关阅读:
    JVM调优之Tomcat启动参数配置及详解
    项目启动部署时报错:java.lang.NoSuchMethodError
    Springboot系列:@SpringBootApplication注解
    Tomcat下部署SpringBoot
    springboot 使用传统方式部署
    Spring Boot 学习笔记一(SpringBoot启动过程)
    Spring Boot【快速入门】
    Windows系统CPU内存网络性能统计第一篇 内存
    MinGW-w64非官方编译器集成安装包(很清楚)
    SyncML是一平台无关的信息同步标准协议集
  • 原文地址:https://www.cnblogs.com/youku/p/LoveOrHate.html
Copyright © 2011-2022 走看看