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........!!

Recommended Answers

All 8 Replies

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);

thanks a lot ma'm you are awesome.........!!

thanks a lot ma'm you are awesome.........!!

No problem, and thanks. :D

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.

Try using <?php instead of <?

Try using <?php instead of <?

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

You need to do:

echo $code;

After the:

$code = str_replace('{id}', $id, $code);

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.