如何使用PHP检查YouTube上是否存在video?

如何使用PHP检查YouTube上是否存在video?

使用YouTube的API怎么样?
毕竟,这意味着使用一些官员,这是不太可能改变比parsing一些HTML页面。

有关更多信息: YouTube API和工具 – 开发人员指南:PHP

检索一个特定的video条目似乎很有趣:如果您发送请求到这样一个URL:

http://gdata.youtube.com/feeds/api/videos/videoID 

(用video的ID代替“videoID”,当然 – 在你的例子中是“GeppLPQtihA”)

如果video有效,您将获得一些ATOM订阅源; 和“无效的ID”,如果不是

而且,我坚持认为:通过这种方式, 你依靠一个logging在案的API ,而不是某种现在存在的行为,但不是保证。

Youtube支持oEmbed格式。
与Pascal MARTIN提供的xml响应相比,我只需要下载600字节的3800字节,使其更快,带宽更less(只有1/6的大小)。

 function yt_exists($videoID) { $theURL = "http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=$videoID&format=json"; $headers = get_headers($theURL); return (substr($headers[0], 9, 3) !== "404"); } $id = 'yyDUC1LUXSU'; //Video id goes here if (yt_exists($id)) { // Yep, video is still up and running :) } else { // These aren't the droids you're looking for :( } 

使用HEAD方法请求URL,如下所示:

 HEAD /watch?v=p72I7g-RXpg HTTP/1.1 Host: www.youtube.com HTTP/1.1 200 OK [SNIP] HEAD /watch?v=p72I7g-BOGUS HTTP/1.1 Host: www.youtube.com HTTP/1.1 303 See Other [SNIP] Location: http://www.youtube.com/index?ytsession=pXHSDn5Mgc78t2_s7AwyMvu_Tvxn6szTJFAbsYz8KifV-OP20gt7FShXtE4gNYS9Cb7Eh55SgoeFznYK616MmFrT3Cecfu8BcNJ7cs8B6YPddHQSQFT7fSIXFHd5FmQBk299p9_YFCrEBBwTgtYhzKL-jYKPp2zZaACNnDkeZxCr9JEoNEDXyqLvgbB1w8zgOjJacI4iIS6_QvIdmdmLXz7EhBSl92O-qHOG9Rf1HNux_xrcB_xCAz3P3_KbryeQk_9JSRFgCWWgfwWMM3SjrE74-vkSDm5jVRE3ZlUI6bHLgVb7rcIPcg 

正如@dbro评论的那样, Pascal MARTIN的回答在当时是可以接受的答案。 但是,由于API已经向前发展,固定和改进,所以一个新的解决scheme可以起作用。 请注意,这是基于@Pascal提供的技术,我引用:

 ...if you send a request to an URL like this one http://gdata.youtube.com/feeds/api/videos/videoID (Replacing "videoID" by the idea of the video, of course -- "GeppLPQtihA" in your example) You'll get some ATOM feed (**STOP HERE**) 

要使用的这个新的url适用于API的V3https://www.googleapis.com/youtube/v3/videos?id={the_id_of_the_video}&key={your_api_key}&part={parts}

哪里

  • {the_id_of_the_video}你应该知道这是什么

  • {your_api_key}是您的开发者控制台中可以find的应用程序的关键字

  • {parts}一个逗号分隔的列表, 在这里检查有效值

现在为结果

如果videoID是有效的,您将在项目字段中获取数据,其中包括video的ID和您通过parts参数查询的信息。

如果videoID不是有效的,那么你会得到一个空的项目

提供一个错误的给你一个错误400 (一个错误对象)。

你想validation,如果一个YouTubeurl是一个真正的YouTubevideo的url? 这是非常困难的,你可以使用正则expression式,但请记住,有大量有效的方式来表示YouTubeurl:

此外,video代码可以包含字母数字字符,下划线, – 字符(不知道他们叫什么),也可能更多。

另一种(低效率)的方式是使用cURL来获取所谓的video页面的HTML,并运行一些正则expression式来validation它是一个实际的video页面。

  http://www.youtube.com/watch?v=bQVoAWSP7k4  
 http://www.youtube.com/watch?v=bQVoAWSP7k4&feature=popular  
 http://www.youtube.com/watch?v=McNqjYiFmyQ&feature=related&bhablah  
 http://youtube.com/watch?v=bQVoAWSP7k4 
 var matches = $('#videoUrl').val().match(/http:\/\/(?:www\.)?youtube.*watch\?v=([a-zA-Z0-9\-_]+)/); if (matches) { alert('valid'); } else { alert('Invalid'); } 
 /** * Check youtube url, check video exists or not, * * @param $url full youtube video url * * @return string - yotube video id */ public static function checkYoutube($url) { if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match)) { $headers = get_headers('http://gdata.youtube.com/feeds/api/videos/' . $match[1]); if (strpos($headers[0], '200')) { return $match[1]; } return false; } return false; } 

链接:

https://github.com/DimitriMikadze/php-helpers

我使用YouTube API检查You Tube上是否存在video。 我下载了PHP的Google API客户端库。 我使用了以下function:

 /** * Used to check if the given movie is availabe on youtube * * It uses youtube api and checks if given movie is available on youtube * If a movie is not available then it returns false * * @param string $youtube_video_url the youtube movie url * * @return boolean $is_available indicates if the given video is available on youtube */ private function IsMovieAvailable($youtube_video_url) { /** The autoload.php file is included */ include_once("autoload.php"); /** Is available is set to false */ $is_available = false; /** The youtube video id is extracted */ $video_id = str_replace("https://www.youtube.com/watch?v=", "", $youtube_video_url); $DEVELOPER_KEY = $google_api_key; $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); // Call the search.list method to retrieve results matching the specified // query term. $searchResponse = $youtube->videos->listVideos('status', array('id' => $video_id)); /** Each item in the search results is checked */ foreach ($searchResponse['items'] as $video) { /** If the video id matches the given id then function returns true */ if ($video['id'] == $video_id) { $is_available = true; break; } } return $is_available; } 

你应该请求这个URL

 https://www.googleapis.com/youtube/v3/videos?id={the_id_of_the_video}&key={your_api_key}&part=status 

之后,您将收到包含uploadStatus字段的响应json

 { etag = "\"I_8xdZu766_FSaexEaDXTIfEWc0/8QgL7Pcv5G8OwpNyKYJa8PaQTc0\""; items = ( { ... status = { embeddable = 1; license = youtube; privacyStatus = public; publicStatsViewable = 1; uploadStatus = processed; }; } ); ... } 

uploadStatus有5个可能的值

删除,失败,处理,拒绝,上传

对于uploadStatus = processeduploaded =>您的YouTubevideo是可用的