/** * authorize() 方法用来获取易班授权 * @param array $aArg 请求参数 * @return none */ public static function authorize($aArg = array()) { if (self::authorized()) { return false; } if (empty($aArg)) { $aArg = $_REQUEST; } $currentUrl = explode('?', current_url()); self::$callbackUrl = self::$callbackUrl ?: $currentUrl[0]; $default = array('client_id' => self::$appId, 'redirect_uri' => self::$callbackUrl, 'display' => self::$display); $aArg = array_merge($default, $aArg); if (isset($aArg['code'])) { if (self::getAccessToken($aArg['code'])) { echo "授权成功。"; header('Location: ' . $_SESSION['HTTP_REFERER']); } else { echo "授权失败。"; } } elseif (isset($aArg['verify_request'])) { $data = pack('H*', addslashes($aArg['verify_request'])); $result = json_decode(rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, self::$appSecret, $data, MCRYPT_MODE_CBC, self::$appId)), true); if ($result['visit_oauth']) { setcookie('yiban_api_access_token', $result['visit_oauth']['access_token'], $result['visit_oauth']['token_expires']); echo "授权成功。"; header('Location: ' . $_SESSION['HTTP_REFERER']); } else { echo "授权失败。"; } } else { $_SESSION['HTTP_REFERER'] = self::$redirectUrl ?: (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''); header('Location: ' . self::$baseUrl . '/oauth/authorize?' . http_build_query($aArg)); } }
?> <!DOCTYPE html> <html> <head> <title>易班 Api 授权接口实例</title> </head> <body> <?php if (!YibanApi::authorized()) { ?> 还未获得授权,<a href="?action=login">现在登录</a> <?php } else { echo "授权用户信息如下:\n"; print_r(YibanApi::request(array('path' => 'user/me', 'return' => 'array'))); } ?> </body> </html> <!-- 下面是 ajax 调用的(jQuery)方式: --> <!-- $.ajax({ url: 'yiban.api.php?action=request', data: {path: 'user/me'}, success: function(json){ console.log(JSON.parse(json)); } }); -->