如果失败,如何重试操作[关闭]

I'm using a PHP script to play YouTube videos by their ID and get the video title and related videos.

My problem is that sometimes the page finishes loading before the title or the related video links show up.

I am using 000WebHost.com free web hosting; it has some restrictions like disabling some functions including set_time_limit() for security reasons, so I can't use that.

Is there any way I can retry loading the title and video links if, for example, the length was zero?

Something like this:

<?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        
    }
?>

Any ideas?

Why not use a do-while loop? This would keep trying to load the video information until it either timed out or received a valid response:

/**
 * @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
}