true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => http_build_query([ "grant_type" => "refresh_token", "refresh_token" => $data['refresh_token'], "client_id" => $client_id, "client_secret" => $client_secret ]) ]); $response = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpcode != 200) { echo "❌ Błąd odświeżania tokena: $response
"; return null; } $newData = json_decode($response, true); if (!$newData) return null; saveToken($file, $newData); echo "🔄 Token odświeżony OK
"; return $newData; } // Pobierz token (z odświeżeniem jeśli trzeba) function getAccessToken($file, $client_id, $client_secret) { $data = loadToken($file); if (!$data) return null; if (time() >= ($data['expires_at'] ?? 0)) { echo "ℹ️ Token wygasł – odświeżam...
"; $data = refreshToken($file, $client_id, $client_secret); } return $data ? $data['access_token'] : null; } // === START === $access_token = getAccessToken($token_file, $client_id, $client_secret); if (!$access_token) { // Tokenu brak → trzeba zalogować się ręcznie $auth_url = "https://kontoapi.insert.com.pl/connect/authorize?" . http_build_query([ 'client_id' => $client_id, 'response_type' => 'code', 'redirect_uri' => $redirect_uri, 'scope' => 'subiekt123 offline_access' ]); die("🔑 Brak ważnego tokena. Kliknij aby się zalogować: $auth_url"); } // === PRZYKŁADOWE WYWOŁANIE API (dodanie klienta) === $api_url = "https://api.subiekt123.pl/1.0/clients?subscription-key=$subscription_key"; $payload = [ "name" => "Nowy Klient " . date("Y-m-d H:i:s"), "kind" => "Company", "emails" => ["klient@example.com"], "address" => [ "line1" => "Testowa 1", "city" => "Wrocław", "zipCode" => "50-000", "country" => "PL" ], "tin" => "1234567890", "tinKind" => "Nip" ]; $ch = curl_init($api_url); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ "Authorization: Bearer $access_token", "Content-Type: application/json", "accept: application/json", "Ocp-Apim-Subscription-Key: $subscription_key", "x-api-version: 1.0" ], CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($payload) ]); $response = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); echo "HTTP Status: $httpcode
"; echo "Response:
" . htmlspecialchars($response) . "
";