2009-06-22 76 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

相关问题