2013-07-31 44 views
-3

我正在使用PHP腳本通過其ID來播放YouTube視頻並獲取視頻標題和相關視頻。如何在發生故障時重試操作

我的問題是,有時頁面在標題或相關視頻鏈接出現之前完成加載。

我使用000WebHost.com免費虛擬主機;它有一些限制,如出於安全原因禁用某些功能,包括set_time_limit(),所以我不能使用它。

如果例如長度爲零,是否有任何方法可以重試加載標題和視頻鏈接?

事情是這樣的:

<?php 
    if (strlen($title) == 0) { 
     // Do something here to retry loading the title from 
     // http://www.youtube.com/get_video_info?video_id=VIDEO_ID   
    } 
?> 

任何想法?

+0

做的是買託管,一些廉價的網絡的最好的事情是不是這樣的限制。 – Brad

回答

1

爲什麼不使用do-while循環?這將繼續嘗試加載視頻信息,直到其超時或接收到有效響應:

/** 
* @return boolean Whether the given argument is a valid 
* YouTube API response 
**/ 
function isValidApiResponse($response) 
{ 
    // ... your logic here ... // 
} 

$videoId  = 'someId'; 
$timeoutTime = time() + 10; // Stop trying after 10 seconds 

do { 
    $apiResponse = CURL::get("https://gdata.youtube.com/feeds/api/videos/{$videoId}?v=2"); 
    $pageLoaded = isValidApiResponse($apiResponse); 
} while ((time() < $timeoutTime) AND (! $pageLoaded)); 

if ($pageLoaded) { 
    // SUCCESS: Page loaded properly 
} else { 
    // ERROR: Timed out 
} 
+0

非常感謝你 – WebRookie

+0

+1好點,我想出了另一種情況 –