首页 文章

播放带有表单的YouTube视频

提问于
浏览
2

我正在尝试播放用户输入表单的YouTube视频 . 具体而言,用户会将YouTube视频网址粘贴到我的表单中,点击提交,然后在下一页上,它将在iFrame中播放该视频 . 有谁知道怎么做?不知何故,它需要将YouTube网址(https://www.youtube.com/watch?v=VIDEO_ID)转换为嵌入网址(http://www.youtube.com/v/VIDEO_ID),然后在下一页上将其作为iFrame的来源 . 在此先感谢您的帮助!

1 回答

  • 1

    试试这个:

    <?php
    if(isset($_POST["youtube"]))
    {
    if(strlen($_POST["youtube"]) == 11)
    {
    $videoid= $_POST["youtube"];
    echo <<< EOF
    <iframe frameBorder="no" width="100%" height="100%" src="http://www.youtube.com/embed/$videoid" allowfullscreen></iframe>
    EOF;
    }else{
    $yturl = rawurldecode($_POST["youtube"]);
    parse_str( parse_url( $yturl, PHP_URL_QUERY ), $yt_array_of_vars );
    $videoid= $yt_array_of_vars['v'];
    echo <<< EOF
    <iframe frameBorder="no" width="100%" height="100%" src="http://www.youtube.com/embed/$videoid" allowfullscreen></iframe>
    EOF;
    }
    
    }else{
    
    $posturl = htmlspecialchars($_SERVER["PHP_SELF"]);
    echo <<< EOF
    <html>
    <head>
    <style>
        html,body, div, iframe{
            height: 100%;
            overflow: hidden;
            overflow-x: hidden;
            overflow-y: hidden;
            margin: 0; padding: 0;
        }
    </style>
    </head>
    <body>
    <form method="post" action="$posturl"> 
    <div align="center">
    <p> <h3> Type the Youtube URL or VIDEOID</h3></p>
    <input type="text" name="youtube" value="">
    <input type="submit" name="submit" value="Submit">
    </form>
    </body>
    </html>
    EOF;
    }
    ?>
    

    检查YouTube iframe选项here

    如果你想要html5播放器,请将 html5=1 追加到iframe src ,如下所示:

    <iframe frameBorder="no" width="100%" height="100%" src="http://www.youtube.com/embed/$videoid?html5=1" allowfullscreen></iframe>
    

相关问题