How to Check If a Video Exists on Youtube, Using PHP

How do I check if a video exists on YouTube, using PHP?

What about using Youtube's API?

After all, that would mean using some official, which is less likely to change than going with parsing some HTML page.

For more information: YouTube APIs and Tools - Developer's Guide: PHP

The Retrieving a specific video entry seems quite interesting: if you send a request to an URL like this one:

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

(replacing "videoID" by the ID of the video, of course – "GeppLPQtihA" in your example), you'll get some ATOM feed if the video is valid; and "Invalid id" if it's not


And, I insist: this way, you rely on a documented API, and not on some kind of behavior that exists today, but is not guaranteed.

how to check if a video id exists on youtube

I have fixed it using this technique, which doesn't require any use of 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!'));
}

How to check if YouTube video exists using YouTube Data API v3

Better solution:

if (sizeof($videoResponse['items'])) {
// Video exist, do stuff
}

Source: verify if video exist with youtube api v3

You can check if your videoResponse is an object:

$videoResponse = $youtube->videos->listVideos('snippet,statistics', array(
'id' => $videoId
));

if (is_object($videoResponse['items'][0])) {
// Video exist, do stuff
}

Or check if it is !empty:

if (!empty($videoResponse['items'][0])) {
// Video exist, do stuff
}

You can actually check for privacy status too:

$videoResponse = $youtube->videos->listVideos('status', array(
'id' => $videoId
));

$privacyStatus = $videoResponse['items'][0]['status']['privacyStatus'];

PHP: checking if YT url is valid and if video exists

You are missing the isValidURL() function. Try changing this line:

if (isValidURL($formatted_url)) {   

to

if(preg_match('/http:\/\/www\.youtube\.com\/watch\?v=[^&]+/', $formatted_url, $result)) {

or

$test = parse_url($formatted_url);
if($test['host']=="www.youtube.com"){

Check if Youtube and Vimeo-clips are valid

i see a answer in this site :
www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_23765374.html

and he said :

I would suggest using youtube's API since you are trying to validate if the video exists.
or if you don't want to go into API stuff then you can do simple trick.
check this link:

http://code.google.com/apis/youtube/developers_guide_php.html#RetrievingVideoEntry

to check for the existence of a video you will need to extract "v" value and send a request that contains the video id to :

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

where videoID is the "v" value
for example a video FLE2htv9oxc
will be queried like this
http://gdata.youtube.com/feeds/api/videos/FLE2htv9oxc
if it does not exist then you will get a page with "Invalid id"
if it exists, will return an XML feed having various info about the video.
this way you can check that the video exists.

hope this will get you in the right direction.

the same thing with vimeo , try to look in api documentation in there site.
http://www.vimeo.com/api

Checking if YouTube Video exists in playlist with Google Apps Script

I believe your goal as follows.

  • You want to add the video IDs by checking the playlist.
  • You want to retrieve the additional video IDs from the column "A" of the active sheet on Google Spreadsheet.
  • When the video IDs are existing in the playlist, you don't want to add them.
  • When the video IDs are not existing in the playlist, you want to add them.

Modification points:

  • In this case, I think that at first, it is required to retrieve the list from the playlist. And then, when the additional video ID is not existing in the playlist, the video ID is added.

When above points are reflected to your script, it becomes as follows.

Modified script:

function addVideoToYouTubePlaylist() {
// 1. Retrieve list from the playlist.
var playlistId = "PL6bCFcS8yqQxSPjwZ9IXFMfVm6kaNGLfi";
var list = [];
var pageToken = "";
do {
var res = YouTube.PlaylistItems.list(["snippet"], {playlistId: playlistId, maxResults: 50, pageToken: pageToken});
if (res.items.length > 0) list = list.concat(res.items);
pageToken = res.nextPageToken || "";
} while (pageToken);
var obj = list.reduce((o, e) => Object.assign(o, {[e.snippet.resourceId.videoId]: e.snippet.resourceId.videoId}), {});

// 2. Retrieve URLs like `https://www.youtube.com/watch?v=###` from the column "A" of the active sheet.
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();

// 3. Check whether the additional video ID is existing in the playlist and add it.
data.forEach(([a]) => {
var videoId = extractVideoID(a);
if (videoId && !obj[videoId]) { // <--- Modified
YouTube.PlaylistItems.insert({
snippet: {
playlistId: playlistId,
resourceId: {
kind: "youtube#video",
videoId: videoId
}
}
}, "snippet");
}
});
}

function extractVideoID(url){
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
if (!match) return; // <--- Added
if ( match && match[7].length == 11 ){
return match[7];
} else {
console.log("Could not extract video ID.");
var trimmedVideoID = url.replace("https://youtu.be/", "");
trimmedVideoID = trimmedVideoID.replace('https://www.youtube.com/watch?v=', "");
trimmedVideoID = trimmedVideoID.replace('https://youtube.com/watch?v=', "");
trimmedVideoID = trimmedVideoID.replace("&feature=youtu.be", "");
trimmedVideoID = trimmedVideoID.replace("&feature=share", "");
console.log(trimmedVideoID);
return trimmedVideoID;
}
}

Note:

  • If you have a lot of additional video IDs, to use the batch request might be useful. Ref

References:

  • PlaylistItems: list
  • PlaylistItems: insert

How do I check if a video exists on YouTube, in client side

@hitesh, Please remove the datatype:'jsonp' from the ajax request. This way you'll get json string if the video id is available and if its not available then the ajax error callback would be invoked. I tried on your fiddle and its working. Try like this-

//var videoID = 'kn8yzJITdvI';//not working 
var videoID = 'p4kIwWHP8Vc';//working
$.ajax({
url: "https://gdata.youtube.com/feeds/api/videos/" + videoID + "?v=2&alt=json",
//dataType: "jsonp",
success: function(data) {
console.log(data)
$("#result").text(data);
},
error: function(jqXHR, textStatus, errorThrown)
{
// Handle errors here
alert('ERRORS: ' + textStatus);
}
});

Here is another short implementation for the solution you need-

//var videoID = 'kn8yzJITdvI';//not working 
var videoID = 'p4kIwWHP8Vc';//working

$.getJSON('http://gdata.youtube.com/feeds/api/videos/'+videoID+'?v=2&alt=jsonc',function(data,status,xhr){
alert(data.data.title);
}).error(function() { alert("error"); });


Related Topics



Leave a reply



Submit