【问题标题】:how to check if a video id exists on youtube如何检查youtube上是否存在视频ID
【发布时间】:2015-12-27 14:51:31
【问题描述】:

我想做的是检查用户输入的视频是否真的存在,我搜索了很多,发现了这个:ReRetrieving_Video_Entry,但它看起来已经被弃用了,所以怎么可能使用@987654324 @ 检查视频是否存在?

【问题讨论】:

  • 您是否从您的问题中阅读了该页面链接上的弃用通知? "注意: YouTube 数据 API (v2) 已于 2014 年 3 月 4 日正式弃用。有关更多信息,请参阅我们的弃用政策。请使用 YouTube 数据 API (v3)用于新的集成并将仍然使用 v2 API 的应用程序迁移到 v3 API。” - 使用较新的版本 developers.google.com/youtube/v3 - 这就是你的“答案” ;-)

标签: php youtube google-api


【解决方案1】:

我已经使用这种技术修复了它,不需要使用任何API

   $headers = get_headers('https://www.youtube.com/oembed?format=json&url=http://www.youtube.com/watch?v=' . $key);

    if(is_array($headers) ? preg_match('/^HTTP\\/\\d+\\.\\d+\\s+2\\d\\d\\s+.*$/',$headers[0]) : false){
        // video exists

    } else {
        // video does not exist
        echo json_encode(array('Error','There is no video with that Id!'));
    }

【讨论】:

    【解决方案2】:

    这是我正在使用的,效果很好。 Youtube API v2. Deprecated

    $video = "cK3N2DC3Fds";            
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds/api/videos/'.$video);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    $content = curl_exec($ch);
    curl_close($ch);
    if ($content && $content !== "Invalid id" && $content !== "No longer available") {           
        $xml   = new SimpleXMLElement($content);
    }else {
     //Doesn't exist
    }
    

    您可以使用YouTube Data API (v3) 检查视频是否存在。从here下载/克隆API。

    这是我制作的一个脚本,用于检查是否存在给定 youtube 视频 ID 的视频。

    require_once dirname(__FILE__).'/../google-api/src/Google/autoload.php'; // or wherever autoload.php is located
    
        $DEVELOPER_KEY = 'yourkey';
    
        $client = new Google_Client();
        $client->setDeveloperKey($DEVELOPER_KEY);
    
      // Define an object that will be used to make all API requests.
        $youtube = new Google_Service_YouTube($client);
    
        $video = "cK3N2DC3Fds"; //Youtube video ID
        $searchResponse = $youtube->search->listSearch('id', array(
          'q' => $video, //The search query, can be a name or anything,
          'maxResults' => 1, //Query result limit
          "type" => "video"
        ));
    
        $exists = false;
        foreach ($searchResponse['items'] as $searchResult) {
            //if type is video, this will always be "youtuve#video"
            if($searchResult['id']['kind'] == "youtube#video"){ 
                if($video ==  $searchResult['id']['videoId']){
                    $exists = true;
                }
            }
         }
    
         if(!$exists){
            echo "video not found";
         }else echo "video found";
    

    【讨论】:

    • 无论我更改视频多少次id,提要链接总是返回No longer available
    • 真的吗?它,实际上在我的网站上工作,给我一个视频 ID,然后检查一下。
    • 你说得对,我会试着给你另一个答案,它不再起作用了。对不起。
    • @MixedCoder 用 youtube API V3 更新了我的答案,我运行了许多测试并且一切正常!
    • 为什么 OP 应该“试试这个”?一个好的答案总是会解释所做的事情以及这样做的原因,不仅适用于 OP,而且适用于 SO 的未来访问者。
    猜你喜欢
    • 2012-11-10
    • 2016-02-26
    • 2010-11-25
    • 2015-05-02
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 2015-08-10
    相关资源
    最近更新 更多