微信公众平台开发笔记

  1. URL跳转,
    链接跳转
    https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid."&redirect_uri=".urlencode("http://192.168.43.46/auit/index.php?c=app&m=oauth")."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect

  2. 用户进入页面,拿code换取ACCESS_TOKEN, 请求代码

file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->secret."&code=".$_GET['code']."&grant_type=authorization_code");
  1. 用ACCESS_TOKEN换取用户数据,请求代码
file_get_contents("https://api.weixin.qq.com/sns/userinfo?access_token=".$data->access_token."&openid=".$data->openid."&lang=zh_CN")

流程示例代码

public function oauth(){
      if (isset($_GET['code'])){
          $data=json_decode(file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->secret."&code=".$_GET['code']."&grant_type=authorization_code"));
          $userInfo=json_decode(file_get_contents("https://api.w
         eixin.qq.com/sns/userinfo?access_token=".$data->access_token."&openid=".$data->openid."&lang=zh_CN"));
          $user = array(
              'openid'  => $userInfo->openid,
              'nickname'     => $userInfo->nickname,
              'headimgurl' => $userInfo->headimgurl
          );
          $this->session->set_userdata($user);
          $this->db->replace('user', $user);
          redirect('client');
      }else{
          echo "出现未知错误,如果重复出现该错误,请联系开发者。错误代码:Oauth:10203";
      }
  }

php密码操作(PHP 5 >= 5.5.0, PHP 7)

  1. 加密操作:string password_hash ( string $password , int $algo [, array $options ] )

示例:

password_hash("admin", PASSWORD_DEFAULT); 
  1. 判断操作: bool password_verify ( string $password , string $hash )

示例:

password_verify($password, $admin_data->row()->password)

版权声明: (https://www.thinkmoon.cn/post/20)
本文首发于指尖魔法屋-微信公众平台开发笔记
转载或引用必须申明原指尖魔法屋来源及源地址!