"client_credentials", "client_id" => $client_id, "client_secret" => $client_secret, "scope" => "openid profile email" ]; // 初始化 cURL $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $token_url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Content-Type: application/x-www-form-urlencoded" ]); // 執行請求 $response = curl_exec($ch); curl_close($ch); // 解析 JSON 回應 $result = json_decode($response, true); // 顯示 Token if (isset($result["access_token"])) { echo "Access Token: " . $result["access_token"] . "\n"; } else { echo "Error: " . $response . "\n"; } //realms/aiacademy/protocol/openid-connect/userinfo #https://auth.aiacademy.tw/realms/aiacademy/protocol/openid-connect/userinfo $access_token = $result["access_token"] ; // 從上一步獲得 $api_url = "https://auth.aiacademy.tw/admin/realms/aiacademy/users"; // 測試用的 Keycloak UserInfo 端點 // 初始化 cURL $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $api_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Bearer $access_token", "Content-Type: application/json" ]); curl_setopt($ch, CURLOPT_VERBOSE, true); // 顯示詳細請求資訊 curl_setopt($ch, CURLOPT_HEADER, true); // 取得回應標頭 // 執行請求 $response = curl_exec($ch); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $header = substr($response, 0, $header_size); $body = substr($response, $header_size); curl_close($ch); // 顯示 API 回應 echo "
"; echo "
"; echo "HTTP Status Code: " . $http_code . "
"; echo "Response Headers:\n" . $header . "
"; echo "Response Body:\n" . $body . "
"; ?>