Reputation: 83
I am trying to play a youtube video that a user enters into a form. Specifically a user will paste a youtube video URL into my form, hit submit, and on the next page it will play that video in an iFrame. Does anyone have any idea how to do this? Somehow it needs to convert the YouTube URL (https://www.youtube.com/watch?v=VIDEO_ID) to the embed URL (http://www.youtube.com/v/VIDEO_ID) and then make that the source of the iFrame on the next page. Thanks in advance for your help!
Upvotes: 2
Views: 913
Reputation: 98911
Try this:
<?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;
}
?>
Check the YouTube iframe options here
If you want the html5 player, append html5=1
to the iframe src
, like this:
<iframe frameBorder="no" width="100%" height="100%" src="http://www.youtube.com/embed/$videoid?html5=1" allowfullscreen></iframe>
Upvotes: 1