我想通过PHP从YouTube获得直接URL时出现签名问题

A. *_*aby 9 php youtube youtube-api youtube-data-api

经过研究,我如何从YouTube视频中获得直接URL,就像这样一个问题:如何从YouTube URL中获得视频的直接URL?[关闭]

我终于做了一个简单的脚本,用php从YouTube视频中生成了一个直接URL,它可以正常工作,但不适用于所有视频

它不适用于“签名”视频,

这是我的代码:

if(isset($_GET['url']) && $_GET['url'] != ""){
    parse_str( parse_url( $_GET['url'], PHP_URL_QUERY ), $vars );


    $id=$vars['v'];
    $dt=file_get_contents("https://www.youtube.com/get_video_info?video_id=$id&el=embedded&ps=default&eurl=&gl=US&hl=en");
    //var_dump(explode("&",$dt));
    if (strpos($dt, 'status=fail') !== false) {

        $x=explode("&",$dt);
        $t=array(); $g=array(); $h=array();

        foreach($x as $r){
            $c=explode("=",$r);
            $n=$c[0]; $v=$c[1];
            $y=urldecode($v);
            $t[$n]=$v;
        }

            $x=explode("&",$dt);
            foreach($x as $r){
                $c=explode("=",$r);
                $n=$c[0]; $v=$c[1];
                $h[$n]=urldecode($v);
            }
            $g[]=$h;
            $g[0]['error'] = true;
            $g[0]['instagram'] = "egy.js";
            $g[0]['apiMadeBy'] = 'El-zahaby';
        echo json_encode($g,JSON_PRETTY_PRINT);

    }else{

        $x=explode("&",$dt);
        $t=array(); $g=array(); $h=array();

        foreach($x as $r){
            $c=explode("=",$r);
            $n=$c[0]; $v=$c[1];
            $y=urldecode($v);
            $t[$n]=$v;
        }
        $streams = explode(',',urldecode($t['url_encoded_fmt_stream_map']));
        foreach($streams as $dt){ 
            $x=explode("&",$dt);
            foreach($x as $r){
                $c=explode("=",$r);
                $n=$c[0]; $v=$c[1];
                $h[$n]=urldecode($v);
            }
            $g[]=$h;
        }
        echo json_encode($g,JSON_PRETTY_PRINT);
       // var_dump( $g[1]["quality"],true);
    }
}else{
    @$myObj->error = true;
    $myObj->msg = "there is no youtube link";

    $myObj->madeBy = "El-zahaby";
    $myObj->instagram = "egy.js";
    $myJSON = json_encode($myObj,JSON_PRETTY_PRINT);

    echo $myJSON;

}
Run Code Online (Sandbox Code Playgroud)

example of issue : https://you-link.herokuapp.com/?url=yt.com?v=csy7cF1T2vk 签名错误YouTube API

update:

the code on : gist.github

any solution ? thanks a lot

Ana*_*kir 3

这是我基于最新 Youtube 网站更新(2022)的解决方案。

<?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);
        $json_code=Explode_Content('var ytInitialPlayerResponse = ', ';</script><div id="player" class="skeleton flexy"', $html);
        $jsondownload1=json_decode($json_code, true)["streamingData"]["formats"];
        $data=str_replace("\u0026","&",$jsondownload1);
        return $data;
    }

    function Explode_Content($first, $last, $string){
        $exp=explode($first,$string);
        $exp=explode($last,$exp[1]);
        return $exp[0];
    }
    
    // Include these functions at the top.
    
    $video_info = YT_V_INFO("6chhghoMGVQ"); // Add Youtube Video ID
    
    //// Get direct video link
    echo YT_V_INFO("6chhghoMGVQ")[0]["url"];
    
    //// Get video quality informations in array.
    print_r($video_info);
?>
Run Code Online (Sandbox Code Playgroud)

对于视频质量,您只能获得低质量,对于中质量或高清质量,您可以获得单独的 AUDIO-LINK / VIDEO-LINK视频。