function getModels(int $limit = 10000): array { $cacheFile = __DIR__ . '/../cache/models.json'; // 1) start with whatever we already have on disk (or an empty array) $existing = []; if (file_exists($cacheFile)) { $data = json_decode(file_get_contents($cacheFile), true); $existing = $data['data']['models'] ?? []; } // re-index by uniqueModelId for easy merging $byId = []; foreach ($existing as $m) { if (!empty($m['uniqueModelId'])) { $byId[$m['uniqueModelId']] = $m; } } // 2) fetch each category feed and merge new models $categories = ['girl','lesbian','fetish','mature','hot_flirt','soul_mate']; foreach ($categories as $cat) { $url = 'https://ptcdw.com/api/model/feed' . '?siteId=261964' . "&category={$cat}" . "&limit={$limit}" . '&extendedDetails=1' . '&responseFormat=json' // … your other params … ; $json = @file_get_contents($url); if (! $json) continue; $feed = json_decode($json, true); if (! empty($feed['data']['models']) && is_array($feed['data']['models'])) { foreach ($feed['data']['models'] as $m) { if (! empty($m['uniqueModelId'])) { // overwrite or insert $byId[$m['uniqueModelId']] = $m; } } } } // 3) rebuild the indexed array $models = array_values($byId); // 4) regenerate slug & fullSlug on every run foreach ($models as &$m) { $base = strtolower(preg_replace('/[^a-z0-9]+/','-',$m['displayName'])); $m['slug'] = trim($base,'-'); $parts = []; if (!empty($m['persons'][0]['age'])) { $parts[] = intval($m['persons'][0]['age']) . 'y'; } if (!empty($m['ethnicity'])) { $parts[] = strtolower($m['ethnicity']); } if (!empty($m['persons'][0]['body']['eyeColor'])) { $parts[] = strtolower($m['persons'][0]['body']['eyeColor']) . '-eyes'; } $parts[] = 'live-cam'; $suffix = preg_replace('/[^a-z0-9]+/','-',implode('-',$parts)); $m['fullSlug'] = trim(strtolower($suffix),'-'); } unset($m); // 5) write back your merged cache file_put_contents($cacheFile, json_encode(['data'=>['models'=>$models]], JSON_PRETTY_PRINT) ); return $models; }