php 获取 微信 open_id 部分代码

  如果当前页面没有获取到用户的open_id;则需要调用 微信授权页面 ,然后添加 authentication  code, state参数返回 本页面; 最后通过 authentication code在服务器中调用curl post 获取 open_id; 最后把结果存入数据库表.



function getWXOpenId($state=''){

        //获取用户 cookie中的的open_id;
        $openId = $this->getCookie('user_openid');

        //echo "\n
".__FILE__.__LINE__.'
';var_dump($openId);
        $WXBWHL = D("WXBWHL");
        $state = $state?$state:$_REQUEST['state'];
        if (!$openId) {
          
          //echo "\n
".__FILE__.__LINE__.'
';var_dump($_REQUEST['code']);var_dump($WXBWHL->wxGetCodeUrl($url, $state));

          if (!$_REQUEST['code']) {
            header('Location: ' . $WXBWHL->wxGetCodeUrl($WXBWHL->getCurrentUrl(), $state));
            exit;
          }

          $code = $_REQUEST['code'];
          $tokeninfo = $WXBWHL->obtainLoginAccessToken($code);

          $openId = $tokeninfo['openid'];

          $this->setCookie('user_openid', $openId);

          //echo "\n
".__FILE__.__LINE__.'
';var_dump($tokeninfo);var_dump($openId);var_dump($state);var_dump($code);exit;
        }

        //写入微信用户表
        $Weixinuser = D('weixinuser');
        $row_wxuser = $Weixinuser->where("weixin_id='{$openId}'")->find();
        if (!$row_wxuser) {
            $wx_user = $WXBWHL->obtainLoginUserInfo($openId, $tokeninfo['access_token']);

            $row_wxuser = array('weixin_id' => $wx_user['openid'], 'username' => $wx_user['nickname'], 'pic' => $wx_user['headimgurl'], 'createdate' => strtotime($this->NOW));
            $Weixinuser->data($row_wxuser)->add();
        }

        //echo "\n
".__FILE__.__LINE__.'
';var_dump($row_wxuser);exit;
        $ret = $row_wxuser;
        $ret['openId'] = $openId;
        return $ret;
    }
public static function wxGetCodeUrl($callback,$state,$scope = 'snsapi_userinfo'){
    
   // $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".self::APP_KEY."&redirect_uri=".$callback."&response_type=code&scope=snsapi_base&state=".$state."#wechat_redirect"; 
    return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".self::APP_KEY."&redirect_uri=".urlencode($callback)."&response_type=code&scope={$scope}&state=".$state."#wechat_redirect";
   
    
  }


  public function getCurrentUrl(){
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    return "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";


  }
public static function obtainLoginAccessToken($code){
    $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".self::APP_KEY."&secret=".self::APP_SECRET."&code=".$code."&grant_type=authorization_code";
    $rstdata = json_decode(HttpUtil::curlGet($url),true);
    return $rstdata;
  }

/**
   * 网页授权后的获取的用户信息[昵称、OPENID]
   * @param  $weixin_id toUserName
   * @param  $access_token
   * @return mixed
   */
  public static function obtainLoginUserInfo($weixin_id,$access_token){
    $url="https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$weixin_id; 
    $rstdata = json_decode(HttpUtil::curlGet($url),true);
    return $rstdata;
  }







你可能感兴趣的:(php)