Zend_Gdata_YouTube检测已删除的视频

时间:2012-11-05 15:53:48

标签: zend-framework zend-gdata

我有以下代码用于将YouTube上的视频信息加载到我的网站上:

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
}

我遇到的问题是,到目前为止,我已经检测到2个将执行catch块的情况 - 如果视频已被删除,或者是否存在某种通信错误。

如果视频已被删除,我想将其从我的本地商家信息中删除。我怎么知道我得到了哪个错误 - 我一直在寻找错误代码的描述,但是找不到这个,尽管我确定其他人之前遇到过这个问题。

1 个答案:

答案 0 :(得分:2)

如果您要查找的视频ID已删除或从不存在,则请求的HTTP状态代码为400,响应正文为Invalid id。您可以获取基础响应对象以确定请求是否无效,或者请求是否完全失败。

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