signature issue when I want to get a direct URL from YouTube via PHP signature issue when I want to get a direct URL from YouTube via PHP php php

signature issue when I want to get a direct URL from YouTube via PHP


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.


This is another code to get each video qualities from youtube get_video_info

<?phpfunction 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/get_video_info?video_id=$v";    $html        = urldecode(YT_IN_DX($url));    $video_links = Explode_Content('playabilityStatus', 'adSafetyReason', $html);    $json        = str_replace("\u0026", "&", $video_links);    $json        = '{"playabilityStatus' . $json . 'adSafetyReason":{"isEmbed":true}}';    $array       = json_decode($json, true);    if (isset($array["playabilityStatus"]["status"]) && $array["playabilityStatus"]["status"] == "UNPLAYABLE") {        $data = array("error" => $array["playabilityStatus"]["status"]);    }else{        $formats = $array["streamingData"]["formats"];        for ($a = 0; $a <= (count($formats) - 1); $a++){            $data[] = array(                "url" => $array["streamingData"]["formats"][$a]["url"],                "mimeType" => $array["streamingData"]["formats"][$a]["mimeType"],                "quality" => $array["streamingData"]["formats"][$a]["quality"],                "qualityLabel" => $array["streamingData"]["formats"][$a]["qualityLabel"],                "width" => $array["streamingData"]["formats"][$a]["width"],                "height" => $array["streamingData"]["formats"][$a]["height"],                "audioQuality" => $array["streamingData"]["formats"][$a]["audioQuality"],                "approxDurationMs" => $array["streamingData"]["formats"][0]["approxDurationMs"]            );        }    }    return $data;}function Explode_Content($first, $last, $string){    $exp = explode($first, $string);    $exp = explode($last, $exp[1]);    return $exp[0];}$videoinfo = YT_V_INFO("6chhghoMGVQ");// $videoinfo=YT_V_INFO("sJsoyuQAepQ");print_r($videoinfo);?>

With this method you can't get some videos ID data, if not allowed by owner.this is two examples :

$videoinfo = YT_V_INFO("6chhghoMGVQ");

Output :

Array(    [0] => Array        (            [url] => https://r4---sn-p5h-gc5d.googlevideo.com/videoplayback?id=e9c861821a0c1954&itag=18&source=youtube&requiressl=yes&mm=31,26&mn=sn-p5h-gc5d,sn-hpa7znsd&ms=au,onr&mv=m&pl=21&ei=fmWRXLGxLIKZ1wb61YyIBQ&susc=yt&initcwndbps=232500&mime=video/mp4&gir=yes&clen=13861924&ratebypass=yes&dur=276.271&lmt=1448537568788216&mt=1553032481&fvip=4&c=WEB&ip=196.64.208.187&ipbits=0&expire=1553054174&sparams=ip,ipbits,expire,id,itag,source,requiressl,mm,mn,ms,mv,pl,ei,susc,initcwndbps,mime,gir,clen,ratebypass,dur,lmt&signature=E2DEC9FF027D3810BF4309AD81E1C22E51BDA8F8.DCA10F844A294D5D267F518DBF0951B757855D2B&key=yt8            [mimeType] => video/mp4; codecs="avc1.42001E, mp4a.40.2"            [quality] => medium            [qualityLabel] => 360p            [width] => 640            [height] => 360            [audioQuality] => AUDIO_QUALITY_LOW            [approxDurationMs] => 276271        )    [1] => Array        (            [url] => https://r4---sn-p5h-gc5d.googlevideo.com/videoplayback?id=e9c861821a0c1954&itag=22&source=youtube&requiressl=yes&mm=31,26&mn=sn-p5h-gc5d,sn-hpa7znsd&ms=au,onr&mv=m&pl=21&ei=fmWRXLGxLIKZ1wb61YyIBQ&susc=yt&initcwndbps=232500&mime=video/mp4&ratebypass=yes&dur=276.271&lmt=1470942139723144&mt=1553032481&fvip=4&c=WEB&ip=196.64.208.187&ipbits=0&expire=1553054174&sparams=ip,ipbits,expire,id,itag,source,requiressl,mm,mn,ms,mv,pl,ei,susc,initcwndbps,mime,ratebypass,dur,lmt&signature=B99844F18562D76976B29A1E6358A3C6D6A0DA78.CD747441B3C6883D3639B584BDACC4023C0A9082&key=yt8            [mimeType] => video/mp4; codecs="avc1.64001F, mp4a.40.2"            [quality] => hd720            [qualityLabel] => 720p            [width] => 1280            [height] => 720            [audioQuality] => AUDIO_QUALITY_MEDIUM            [approxDurationMs] => 276271        )    [2] => Array        (            [url] => https://r4---sn-p5h-gc5d.googlevideo.com/videoplayback?id=e9c861821a0c1954&itag=43&source=youtube&requiressl=yes&mm=31,26&mn=sn-p5h-gc5d,sn-hpa7znsd&ms=au,onr&mv=m&pl=21&ei=fmWRXLGxLIKZ1wb61YyIBQ&susc=yt&initcwndbps=232500&mime=video/webm&gir=yes&clen=16923548&ratebypass=yes&dur=0.000&lmt=1448363037942780&mt=1553032481&fvip=4&c=WEB&ip=196.64.208.187&ipbits=0&expire=1553054174&sparams=ip,ipbits,expire,id,itag,source,requiressl,mm,mn,ms,mv,pl,ei,susc,initcwndbps,mime,gir,clen,ratebypass,dur,lmt&signature=B477F9D1D7A34ECFAF8C7CDBC94A46B365633A26.DEC0E30537E6DE02FD960E515CC5DDB4CFEFC65C&key=yt8            [mimeType] => video/webm; codecs="vp8.0, vorbis"            [quality] => medium            [qualityLabel] => 360p            [width] => 640            [height] => 360            [audioQuality] => AUDIO_QUALITY_MEDIUM            [approxDurationMs] => 276271        ))

This is another video ID not allowed by his owner.

$videoinfo=YT_V_INFO("sJsoyuQAepQ");

Output :

Array ( [error] => UNPLAYABLE )