如何使用Kaltura PHP5.3 SDK获取超1万条限制的全部媒体条目?
Hey there, let's break down why you're only pulling ~25k media entries instead of your full 90k, and walk through fixes to get all your data.
Key Issues Blocking Full Data Pull
1. You're only fetching the first page of results
Your current listMediaByLastPlay function calls Kaltura's listAction once, with the default pageIndex of 1. Even with a pageSize of 1000, any date range with more than 1000 entries will leave all subsequent pages uncollected. That's a big source of missing data right there.
2. Your filter excludes entries that were never played
You're filtering strictly by lastPlayedAt ranges, which means any entry that has never been played (so lastPlayedAt is null) gets completely excluded. This is almost certainly where your missing 65k entries are hiding.
Fixes to Retrieve All Entries
Fix 1: Handle pagination properly
Update your listMediaByLastPlay function to loop through all pages until you get fewer entries than your page size (indicating you've hit the last page):
public function listMediaByLastPlay($startDate, $endDate) { $pageSize = 1000; $client = $this->getClient(\KalturaSessionType::ADMIN); $mediaObj = $client->media; $filter = new \KalturaMediaEntryFilter(); $filter->lastPlayedAtGreaterThanOrEqual = $startDate; $filter->lastPlayedAtLessThanOrEqual = $endDate; $filter->orderBy = '+lastPlayedAt'; $pager = new \KalturaFilterPager(); $pager->pageSize = $pageSize; $allObjs = array(); $pageIndex = 1; do { $pager->pageIndex = $pageIndex; $result = $mediaObj->listAction($filter, $pager); $currentPageObjs = $result->objects; foreach($currentPageObjs as $v) { $t = array(); $t['dataUrl'] = $v->dataUrl; $t['flavorParamsIds'] = $v->flavorParamsIds; $t['plays'] = $v->plays; $t['views'] = $v->views; $t['lastPlayedAt'] = $v->lastPlayedAt; $allObjs[] = $t; } $pageIndex++; // Stop loop when we get fewer entries than page size (last page) } while (count($currentPageObjs) == $pageSize); return $allObjs; }
Fix 2: Include entries with no play history
Add a separate function to fetch entries that were never played, then combine those results with your date-range pulls:
public function listMediaNeverPlayed() { $pageSize = 1000; $client = $this->getClient(\KalturaSessionType::ADMIN); $mediaObj = $client->media; $filter = new \KalturaMediaEntryFilter(); $filter->lastPlayedAtIsNull = true; // Targets entries with no play history $pager = new \KalturaFilterPager(); $pager->pageSize = $pageSize; $allObjs = array(); $pageIndex = 1; do { $pager->pageIndex = $pageIndex; $result = $mediaObj->listAction($filter, $pager); $currentPageObjs = $result->objects; foreach($currentPageObjs as $v) { $t = array(); $t['dataUrl'] = $v->dataUrl; $t['flavorParamsIds'] = $v->flavorParamsIds; $t['plays'] = $v->plays; $t['views'] = $v->views; $t['lastPlayedAt'] = $v->lastPlayedAt; // Will be null for these entries $allObjs[] = $t; } $pageIndex++; } while (count($currentPageObjs) == $pageSize); return $allObjs; }
Update your main processing function
Modify __mywrite to include the never-played entries in your total count:
public function __mywrite($route, $console, $range) { $configObj = $this->readMyWriteHistoryConfigFile(); $lastProcessObj = $this->readMyWriteLastProcessFile(); $veryStartDate = $configObj->veryStartDate; $maxDate = $configObj->maxDate; $startDate = $veryStartDate; $endDate = $startDate + $range; $totalCount = 0; // Fetch played entries by date range while($startDate <= $maxDate) { $objs = $this->listMediaByLastPlay($startDate, $endDate); $totalCount += count($objs); echo "\n$startDate - $endDate:\n"; echo "\n". count($objs). "\n"; $startDate = $endDate + 1; $endDate = $endDate + $range; } // Add entries that were never played $neverPlayedObjs = $this->listMediaNeverPlayed(); $totalCount += count($neverPlayedObjs); echo "\nNever played entries: ". count($neverPlayedObjs). "\n"; echo "\nTotal count: $totalCount\n"; }
Quick Validation Tip
To cross-check expected vs. fetched entries per range, use Kaltura's countAction with your filter:
$count = $mediaObj->countAction($filter); echo "Expected entries in range: $count\n";
This will help you confirm if pagination is working correctly and if you're accounting for all entry types.
内容的提问来源于stack exchange,提问作者kenpeter

