How to get thumbnail of youtube from video link using jquery and php


First we need to get id of youtube video check below code

<script type="javascript">
url = 'http://www.youtube.com/watch?v=30JXcxL4Ldg';
if(url.match("[\?&]v=([^&#]*)") != null){
videoId = url.match("[\?&]v=([^&#]*)")[1];
}
</script>

Now using this id, we can get youtube video’s title, description, image………

prepare url using jquery’s getJSON method
first we need to create one javascript varible which is used to store our url of api with videoId.


<script type="javascript">
var urlVideo = 'http://gdata.youtube.com/feeds/api/videos/' + videoId + '?v=2&alt=jsonc';
</script>

and then


<script type="javascript">
$.getJSON(urlVideo, function(json){
$("<img alt="" />").attr("src", json.data.thumnail.hqDefault).appendTo("body");
});
</script>

You can also get the data on server-side using php

<?php

$videoId = ’30JXcxL4Ldg’;
$video = json_decode(file_get_contents(sprintf(“http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=jsonc&#8221;,$videoId)));

echo ‘<img alt=”” />data->thumnail->hqDefault.'”>’;

?>
And last you can also get some other information


$video->data->title; // title of video
$video->data->id; // id of video
$video->data->description; // description of video
$video->data->thumnail->hqDefault; // large of video
$video->data->thumnail->sqDefault; // small image of video

Advertisements

One thought on “How to get thumbnail of youtube from video link using jquery and php

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s