zend-frameworkzend-gdata

Zend_Gdata_YouTube detect removed video


I have the following code which is used to load video information from YouTube onto my site:

try {
    $yt = new Zend_Gdata_YouTube();
    $videoEntry = $yt->getVideoEntry($video_path);
    $duration = $videoEntry->getVideoDuration();
} catch (Zend_Gdata_App_HttpException $e) {
    //do something with the error
}

The problem I have is that I've detected, so far, 2 scenarios where the catch block will be executed - if the video has been removed, or if there is some sort of communication error.

If the video has been removed, I want to remove it from my local listing. How can I tell which error I'm getting - I've been looking for a description of the error codes, and can't find this, though I'm sure other people have run into this issue before.


Solution

  • If the video ID you are looking for was deleted or never existed, the HTTP status code for the request will be 400 and the body of the response is Invalid id. You can get the underlying response object to determine if the request was invalid, or if the request failed completely.

    try {
        $yt         = new Zend_Gdata_YouTube();
        $videoEntry = $yt->getVideoEntry($video_path);
        $duration   = $videoEntry->getVideoDuration();
    } catch (Zend_Gdata_App_HttpException $e) {
        $response = $e->getResponse();
    
        if ($response !== null) {
            if ($response->getStatus() == 400 && $response->getBody() == 'Invalid id') {
                // the video requested does not exist or was deleted
            } else {
                // some other error
            }
        }
    }