This is my solution based on the latest Youtube Website Update (2019).
<?php
function YT_IN_DX($url){
$cookie_file_path = "cookies.txt";
$agent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/46.0";
$ch = curl_init();
$headers[] = "Connection: Keep-Alive";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_URL, $url);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
function YT_V_INFO($v){
$url="https://www.youtube.com/watch?v=$v";
$html=YT_IN_DX($url);
$video_links=Explode_Content('ytplayer.config = ', ';ytplayer.load', $html);
$jsondownload1=json_decode($video_links, true);
$json_str=$jsondownload1["args"]["player_response"];
$jsn_str=str_replace("u0026","&",$json_str);
$streamin_data_json=json_decode($jsn_str, true);
$videolink=$streamin_data_json["streamingData"]["formats"][0]["url"];
return $videolink;
}
function Explode_Content($first, $last, $string){
$exp=explode($first,$string);
$exp=explode($last,$exp[1]);
return $exp[0];
}
// Include these functions at the top.
// Get direct url from Youtube with two lines
$videolink = YT_V_INFO("6chhghoMGVQ"); // Add Youtube Video ID
echo $videolink; // Your download link
?>
For the video quality you can get only the low, for the medium or the HD quality you get the video separated AUDIO-LINK / VIDEO-LINK.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…