2009-06-22 75 views
2

我正試圖將我從Youtube中的特定用戶檢索到的一堆視頻同步到視頻ID數據庫表中。在PHP中同步2個數據結構的最佳方式是什麼?

這是因爲YouTube不允許向視頻添加元信息。因此,我在我的服務器上創建了一個視頻表,並希望同步videoids。

即PHP/MySQL的應用程序< - > YouTube的

YouTube視頻的數據結構如下:

foreach ($feed as $entry) { 
    print "<p>"; 
    print $entry->getVideoId(); 
    print "</p>"; 
} 

對我的數據庫是這樣的:

$rs->MoveFirst(); 
while (!$rs->EOF) { 
    print "<p>"; 
    print $rs->fields['yt_id']; 
    print "</p>"; 
    $rs->MoveNext(); 
} 

你知道我如何同步這些數據,以便:

  1. 每當用戶在YouTube上上傳新視頻時,我可以調用一個同步函數來檢索最新的視頻並將其添加到mysql數據庫中?
  2. 但是,如果用戶在YouTube上刪除視頻,那麼沒有刪除?

回答

1

你可以使用array_diff()比較的ID,一旦你已經從兩個位置,如取他們:

//build array of video IDs in YouTube 
$arYT = array(); 
foreach ($feed as $entry) { 
    $arYT[] = $entry->getVideoId(); 
} 

//build array of video IDs in local DB 
$arDB = array(); 
$rs->MoveFirst(); 
while (!$rs->EOF) { 
    $arDB[] = $rs->fields['yt_id']; 
    $rs->MoveNext(); 
} 

//to download, we want IDs which are in YouTube but not in the local Db 
$idsToDownload = array_diff($arYT, $arDB); 

//to delete, we want IDs which are in the local DB but not in YouTube 
$idsToDelete = array_diff($arDB, $arYT); 

然後你就可以做這樣的事情:

//download new videos 
foreach ($idsToDownload as $id) { 
    //get video info for video $id and put into local db  
} 

//delete deleted videos 
foreach ($idsToDelete as $id) { 
    //delete $id from local DB 
} 
+0

感謝引入和array_diff();這是太棒了! – bigsurf 2009-06-23 04:28:59

相關問題