4
Contributors
8
Replies
11
Views
7 Years
Discussion Span
Last Post by Mark_24
1

i want to pass a youtube code like
http://www.youtube.com/watch?v=NMAYr709-9Y&feature=dir
and it return me the embed code.........!! how can i do that with php and curl........!!

This it the embed code for that particular page:

<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/NMAYr709-9Y&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/NMAYr709-9Y&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>

Notice the only unique thing is: NMAYr709-9Y

So you can just generate that embed code without having to make a http request to youtube.

eg:

// url of video
$url = 'http://www.youtube.com/watch?v=NMAYr709-9Y&feature=dir';

// we get the unique video id from the url by matching the pattern
preg_match("/v=([^&]+)/i", $url, $matches);
$id = $matches[1];

// this is your template for generating embed codes
$code = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/{id}&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/{id}&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>';

// we replace each {id} with the actual ID of the video to get embed code for this particular video
$code = str_replace('{id}', $id, $code);
0

This it the embed code for that particular page:

<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/NMAYr709-9Y&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/NMAYr709-9Y&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>

Notice the only unique thing is: NMAYr709-9Y


hi,

i tried above 2nd one but i am failed to get video.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?   // url of video
    $url = 'http://www.youtube.com/watch?v=NMAYr709-9Y&feature=dir';
   // we get the unique video id from the url by matching the pattern
    preg_match("/v=([^&]+)/i", $url, $matches);
    $id = $matches[1];
   // this is your template for generating embed codes
 $code = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/{id}&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/{id}&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>';
   // we replace each {id} with the actual ID of the video to get embed code for this particular video
  $code = str_replace('{id}', $id, $code); ?>
</body>
</html>

. i am writing script like above i am not getting video what is wrong with my code. help me.

0

Try using <?php instead of <?

i tried but i am not getting any video.....

Edited by rajabhaskar525: n/a

0

I would prefer iframes over the objects.
PS: A complete solution to this issue would be https://github.com/dereuromark/MediaEmbed which can extract the "host slug" and "id", stores those in the DB and completes the output HTML upon display (either as iframe - preferred - or as object fallback).

mark

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.