三、网页授权获取用户信息

来源:互联网 发布:上色软件 编辑:程序博客网 时间:2024/04/30 10:14

网页获取用户信息:

1. 首先 将 auth2 的 可信域名 改成 你即将调用的 返回用户 信息的 域名
这样 才不会出现  redirect_uri 参数错误 



2、 接下来 获取code
 /**   * @todo   auth2 跳转获取 code   * @param  null   * @return null 跳转   */  public function authqy(){       $params = array(           'appid' => $this->_appid,      //企业号appid           'redirect_uri' => site_url('home/index/getinfo'), //获取code的地址           'response_type' => 'code',     //固定           'scope' => 'snsapi_base',      //固定           'state' => 'wjw',              //任意字符串        );      $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?';      $url = $url . http_build_query($params) .'#wechat_redirect';       echo '<script> location.href = "'.$url.'";</script>';      exit;  }


3、根据code 获取用户信息  获取token的时候   如果不知道 secret (请参考 笔记二)
/**   * @todo   auth2 跳转获取 code   * @param  null   * @return null 跳转   */  public function authqy(){       $params = array(           'appid' => $this->_appid,      //企业号appid           'redirect_uri' => site_url('home/index/getinfo'), //获取code的地址           'response_type' => 'code',     //固定           'scope' => 'snsapi_base',      //固定           'state' => 'wjw',              //任意字符串        );      $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?';      $url = $url . http_build_query($params) .'#wechat_redirect';       echo '<script> location.href = "'.$url.'";</script>';      exit;  }   /**   * @todo 根据code 获取用户信息   * @param  string $code  get方式 微信回调参数code   * @return null   */  public function getinfo(){       $code = $this->input->get('code');      if($code){          $token = $this->get_token();           $url = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?';          $params = array(               'access_token' => $token,               'code' => $code,            );          $this->curl->options(array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false));          $url =  $url . http_build_query($params);          $res = $this->curl->simple_get($url);          $info = json_decode($res);          if(isset($info->DeviceId)){              $data = array();              $data['userid'] = isset($info->UserId)?$info->UserId:''; //企业成员id              $data['openid'] = isset($info->OpenId)?$info->OpenId:'';//非企业成员 openid              $data['deviceid'] = $info->DeviceId; //手机微信设备号 删除重装会更新  否则不会改变               //这里获取到了 用户信息              var_dump($data);              exit;          }else{              die("微信返回有误!".$res);          }      }else{          die("微信返回有误!");      }  }    /**   * @todo   获取公众号token   * @param   null   * @return  string $token   */  public function get_token(){       $tokenstr = file_get_contents('./token.txt');      if($tokenstr){          $tokenarr = explode(',', $tokenstr);          if($tokenarr[1] + 7200 > time()){              return $tokenarr[0];          }          //否则token失效      }//否则token为空       //重新获取token      $url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?';      $params = array(           'corpid' => $this->_appid,           'corpsecret' => $this->_appsecret,        );      $url = $url . http_build_query($params);      $this->curl->options(array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false));      $res = $this->curl->simple_get($url);      $result = json_decode($res);      if(isset($result->access_token)){          file_put_contents('./token.txt', $result->access_token.','.time());          return $result->access_token;      }else{          die('token 获取失败!');      }   }


注:这里如果获取的 
        userid  不为空 则为关注过企业号的
        openid 不为空 则为未关注企业号的







0 0
原创粉丝点击