How can I check to see if a Vimeo video exists?
Currently, I have a function that builds a vimeo player based on a vimeo ID
function create_video_player_by_ID($video_id){
$player = '<iframe src="http://player.vimeo.com/video/';
$player .= $video_id.'" ';
$player .= 'width="'.$this->开发者_Go百科width.'" ';
$player .= 'height="'.$this->height.'" ';
$player .= 'frameborder="0"></iframe>';
return $player;
}
Currently, I get a vimeo appology in the player window is the ID is invalid, but I would like to do more with that. How can I get a boolean to return before the video player, so I can do something else an failure?
You can use the HEAD request method using the video URL.
function check_remote_video_exists($video_url) {
$headers = @get_headers($video_url);
return (strpos($headers[0], '200') > 0) ? true : false;
}
Check your vimeo URL like so:
if (check_remote_video_exists('YOUR_VIMEO_VIDEO_URL')) {
// video exists, do stuff
} else {
// video does not exist, do other stuff
}
Hope this helps someone.
Try doing a HEAD request on the src URL to make sure it returns status 200 instead of 404.
You can also use the video API to get information about an idea. See their docs.
For embedding purposes, the best approach would be to make a call to oEmbed with the video url. It will return a non-200 code if the video cannot be embedded.
Vimeo oEmbed docs
In the documentation, it clearly mentioned that how can we get a video from Vimeo. You need to hit an URL with video id if the video exit then the response code is 200 otherwise if video does not exist then it will give 404 response. See here
https://developer.vimeo.com/api/reference/videos#get_video
精彩评论