Lets say I have a youtube video url www.youtube.com/watch?v=B4CRkpBGQzU&feature=youtube_gdata&par1=1&par2=2
I want to get the video thumbnail -> i3.ytimg.com/vi/B4CRkpBGQzU/default.jpg I just need a simple php script that I will input the $url and get the $img If posible I would like to strip all other parameters and be left with just the www.youtube.com/watch?v=B4CRkpBGQzU as the $url
To extract the identifier from the following URL :
$url = 'www.youtube.com/watch?v=B4CRkpBGQzU&feature=youtube_gdata&par1=1&par2=2';
You can first use parse_url()
to get the query string :
$queryString = parse_url($url, PHP_URL_QUERY);
var_dump($queryString);
Which, here, would give you :
string 'v=B4CRkpBGQzU&feature=youtube_gdata&par1=1&par2=2' (length=49)
And, then, use parse_str()
to extract the parameters from that query string :
parse_str($queryString, $params);
var_dump($params);
Which would get you the following array :
array
'v' => string 'B4CRkpBGQzU' (length=11)
'feature' => string 'youtube_gdata' (length=13)
'par1' => string '1' (length=1)
'par2' => string '2' (length=1)
And, now, it's just a matter of using the v
item from that array, injecting it into the thumbnail URL :
if (isset($params['v'])) {
echo "i3.ytimg.com/vi/{$params['v']}/default.jpg";
}
Which gives :
i3.ytimg.com/vi/B4CRkpBGQzU/default.jpg
<?php
function getYoutubeImage($e){
//GET THE URL
$url = $e;
$queryString = parse_url($url, PHP_URL_QUERY);
parse_str($queryString, $params);
$v = $params['v'];
//DISPLAY THE IMAGE
if(strlen($v)>0){
echo "<img src='http://i3.ytimg.com/vi/$v/default.jpg' width='150' />";
}
}
?>
<?php
getYoutubeImage("http://www.youtube.com/watch?v=CXWSlho1mMY&feature=plcp");
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With