这是我正在使用的,效果很好。 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";