Can anyone give me an idea, how can we show or embed a YouTube video if we just have the URL or the Embed code.
Thanks for the help
Can anyone give me an idea, how can we show or embed a YouTube video if we just have the URL or the Embed code.
Thanks for the help
You mean you want to grad a video from youtube and upload it to your server OR you just intend to embed a youtube video in your pages?
If you want to upload videos programatically, check the YouTube Data API for PHP
Akshay,
I want to give the usability to users, they can give the URL/Embed code in the respective textbox, then the respective video should come onto page...
Hope i'm clear now
luvboy,
If i understand clearly, user provides the URL/code of the Youtube video and then that video is displayed on the page.
For that, just write a simple page, with layout etc.. Copy video embed code from youtube and paste it in your page. Replace embed code with some field, say VideoID. Set this VideoId to code provided by your user.
edit: see answer by Alec Smart.
Hi,
You have to ask users to store the 11 character code from the youtube video.
For e.g. http://www.youtube.com/watch?v=Ahg6qcgoay4
The eleven character code is : Ahg6qcgoay4
You then take this code and place it in your database. Then wherever you want to place the youtube video in your page, load the character from the database and put the following code:-
e.g. for Ahg6qcgoay4 it will be :
<object width="425" height="350" data="http://www.youtube.com/v/Ahg6qcgoay4" type="application/x-shockwave-flash"><param name="src" value="http://www.youtube.com/v/Ahg6qcgoay4" /></object>
Use a regex to extract the "video id" after watch?v=
Store the video id in a variable, let's call this variable vid
Get the embed code from a random video, remove the video id from the embed code and replace it with the vid
you got.
I don't know how to deal with regex in php, but it shouldn't be too hard
Here's example code in python:
>>> ytlink = 'http://www.youtube.com/watch?v=7-dXUEbBz70'
>>> import re
>>> vid = re.findall( r'v\=([\-\w]+)', ytlink )[0]
>>> vid
'7-dXUEbBz70'
>>> print '''<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/%s&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/%s&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>''' % (vid,vid)
<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/7-dXUEbBz70&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/7-dXUEbBz70&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>
>>>
The regular expression v\=([\-\w]+)
captures a (sub)string of characters and dashes that comes after v=
If you plan to store YouTube video ids in your database, I suggest that you look at these articles:
To create your own YouTube video player:
PHP example for extracting the parameter is as follows:
<?php
preg_match(
'/[\\?\\&]v=([^\\?\\&]+)/',
'http://www.youtube.com/watch?v=OzHvVoUGTOM&feature=channel',
$matches
);
// $matches[ 1 ] should contain the youtube id
?>
Searching for this same topic I found another method using Javascript an Youtube API's
Directly from: http://code.google.com/apis/ajax/playground/#simple_embed
Loading the API
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
And executing the following javascript code:
google.load("swfobject", "2.1");
function _run() {
var videoID = "ylLzyHk54Z0"
var params = { allowScriptAccess: "always" };
var atts = { id: "ytPlayer" };
// All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
swfobject.embedSWF("http://www.youtube.com/v/" + videoID + "&enablejsapi=1&playerapiid=player1",
"videoDiv", "480", "295", "8", null, null, params, atts);
}
google.setOnLoadCallback(_run);
The complete sample is in the previously referred page http://code.google.com/apis/ajax/playground
Here is a way how to upload videos from your website before to embed them: http://www.webdryver.com/php-tutorials/youtube-gdata-php-script-for-video-upload.html Thanks and keep the grate job.