“…由于版权或判断问题,YouTube将禁用某些视频,但链接仍在我的列表中。有没有人可以推荐一个JS或者其他的解决方案或者文章,看看视频链接是否在x个时间段内没有启动来启动一个跳过或者下一个动作。请告知。”
既然已经涉及到PHP代码,那么一个可能的选择就是以下步骤:
https://www.youtube.com/oembed?
+
Youtube video URL
.
请求示例:
https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=R5mpcDWpYSA
fopen
检查视频可用性。附注a
file_exists($url)
在Youtube服务器上无法正常工作(它们总是返回一些页面内容,即使视频本身已被删除)。
(将回音)
OK 200
“或”
ERROR 404
“,取决于视频状态…)
<?php
//# is ERROR = https://www.youtube.com/watch?v=R5mpcDWpYSA
$url = "https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=R5mpcDWpYSA"; //# test video deleted.
//# is OK = https://www.youtube.com/watch?v=mLuh_O4mYbA
//$url = "https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=mLuh_O4mYbA"; //# test working (not deleted).
try
{
set_error_handler(function() { /* # temp ignore Warnings/Errors */ });
$fop = fopen($url, "rb");
if ( !$fop && $fop==false) { throw new Exception(); }
restore_error_handler(); //# restore Warnings/Errors
echo "OK 200 ::: Youtube video was found";
}
catch ( Exception $e )
{ echo "Error 404 ::: Youtube video not found (deleted or bad link)"; }
?>
方案2:
file_get_contents
向Youtube的
get_video_info?
.
https://www.youtube.com/get_video_info?video_id=R5mpcDWpYSA
示例代码:
<?php
//# ERROR = https://www.youtube.com/watch?v=R5mpcDWpYSA
$url = "https://www.youtube.com/get_video_info?video_id=R5mpcDWpYSA"; //# test video deleted.
//# OK = https://www.youtube.com/watch?v=mLuh_O4mYbA
//$url = "https://www.youtube.com/get_video_info?video_id=mLuh_O4mYbA"; //# test working (not deleted).
$src = file_get_contents($url);
//# find text... playabilityStatus%22%3A%7B%22status%22%3A%22OK ...
$str1 = "playabilityStatus%22%3A%7B%22status%22%3A%22";
$pos = strpos($src, $str1);
$result = substr( $src, $pos + (strlen($str1)), 5);
if( $result{0} == "O" && $result{1} == "K" )
{ echo "OK 200 ::: Youtube video was found"; }
else
{ echo "Error 404 ::: Youtube video not found (deleted or bad link)"; }
?>