<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ``` <?php /** * 微信公眾號網頁授權 * * Class GetWxUser */ class GetWxUser { private $appId = ''; private $appSecret = ''; /** * 1、獲取微信用戶信息,判斷有沒有code,有使用code換取access_token,沒有去獲取code。 * * @return array 微信用戶信息數組 */ public function getUserInfos() { if (!isset($_GET['code'])) {//沒有code,去微信接口獲取code碼 $callback = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; //微信服務器回調url,這里是本頁url $this->getCode($callback); } else { $code = $_GET['code']; $data = $this->getAccessToken($code); //獲取網頁授權access_token和用戶openid // 增加access_token緩存 $accessToken = $data['access_token'] ?? ''; $openid = $data['openid'] ?? ''; return $this->getUserInfo($accessToken, $openid); //獲取微信用戶信息 } } /** * 2、用戶授權并獲取code * * @param string $callback 微信服務器回調鏈接url */ private function getCode($callback) { $appId = $this->appId; $scope = 'snsapi_userinfo'; $state = md5(uniqid(rand(), true)); //唯一ID標識符絕對不會重復 $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $appId . '&redirect_uri=' . urlencode($callback) . '&response_type=code&scope=' . $scope . '&state=' . $state . '#wechat_redirect'; header("Location:$url"); } /** * 3、使用code換取access_token * * @param string $code 用于換取access_token的code,微信提供 * * @return array [ * 'openid'=>1111, * 'access_token'=>3333 * ] */ private function getAccessToken($code) { // 緩存 $fileName = 'access_token.txt'; if (file_exists($fileName)) { if ($fileToken = file_get_contents($fileName)) { $data = json_decode($fileToken, 1); if (isset($data['access_token']) && !empty($data['access_token']) && isset($data['expire_time']) && !empty($data['expire_time']) ) { if ($data['expire_time'] > time()) { $accessToken = $data['access_token']; $openid = $data['openid']; return [ 'access_token' => $accessToken, 'openid' => $openid, ]; } } } } $appId = $this->appId; $appSecret = $this->appSecret; $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appId . '&secret=' . $appSecret . '&code=' . $code . '&grant_type=authorization_code'; $user = json_decode(file_get_contents($url)); if (isset($user->errcode)) { echo 'error:' . $user->errcode . '<hr>msg :' . $user->errmsg; exit; } $data = json_decode(json_encode($user), true); //記錄緩存 file_put_contents('access_token.txt', json_encode(['access_token' => $data['access_token'], 'openid' => $data['openid'], 'expire_time' => 5000])); return $data; } /** * 4、使用access_token獲取用戶信息 * * @param string $accessToken access_token * @param string $openid 用戶的openid * * @return array 用戶信息數組 */ private function getUserInfo($accessToken, $openid) { $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $accessToken . '&openid=' . $openid . '&lang=zh_CN'; $user = json_decode(file_get_contents($url)); if (isset($user->errcode)) { echo 'error:' . $user->errcode . '<hr>msg :' . $user->errmsg; exit; } return json_decode(json_encode($user), true); //返回的json數組轉換成array數組 } } $wx = new GetWxUser(); $res = $wx->getUserInfos(); var_dump($res); exit; ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看