user1434156
user1434156

Reputation:

Obtaining youtube embedded video code through php

I am a new learner to php. I am trying to retrieve embeded information from youtube videos URLs. I was originally following the code from this SITE but unfortunately the author doesn't provide access to actual code.

I am having difficulties extracting the php variable values so that i can get the embedded video code.

The htmlentities echo is returning this errorsyntax error, unexpected T_CONSTANT_ENCAPSED_STRING

source: SITE

<html>
    <form method="post" action="">
    URL:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="text" value="<?php $vari ?>" name="yurl"> <!--$vari-->
    <br>
    <br>
    Height:&nbsp;
    <input type="text" value="<?php $hth ?>" name="yheight"> <!--$hth-->
    <br>
    <br>
    Width:&nbsp;&nbsp;
    <input type="text" value="<?php $wdth ?>" name="ywidth"> <!--$wdth-->
    <br>
    <br>
    Autoplay:&nbsp;
    <input type="checkbox" value="<?php $auto1; $auto2; ?>" name="yautop">  <!--$auto1 $auto2-->
    <br>
    <br>
    <input type="submit" value="Generate Embed Code" name="ysubmit">
    </form>
</html>

php code for getting youtube url info

<?php
$vari ='';
$step1 =explode ('v=', $vari);
$step2 =explode ('&amp;',$step1[1]);

$auto1='?autoplay=1';

$auto2='&autoplay=1';


//Iframe code with autoplay
echo htmlentities ('<iframe src="http://www.youtube.com/embed/'.$step2[0].$auto1'"
frameborder="0" width="'.$wdth.'" height="'.$hth.'"></iframe>';

//embed code with autoplay
echo htmlentities ('<embed width="'.$wdth.'" height="'.$hth.'"
type="application/x-shockwave-flash" src="http://www.youtube.com/v/'.$step2[0].$auto2'"
wmode="transparent" embed="" /></embed>');
?>

Upvotes: 0

Views: 598

Answers (3)

Sodruldeen Mustapha
Sodruldeen Mustapha

Reputation: 1271

you can use the below function to get the embed url code from both facebook and youtube the $link is the address shown on the web browser as it is

//get youtube link
    function youtube_embed_link($link){
        $new=str_replace('https://www.youtube.com/watch?v=','', $link);
        $link='https://www.youtube.com/embed/'.$new;
        return $link;
    }
    //get facebook link
    function fb_embed_link($link) {
        $link = 'https://www.facebook.com/plugins/video.php?href='.$link.'&show_text=0&width=560';
        return $link;
    }
//get embed url for fb nd youtube
function get_vid_embed_url($link){
    $embed_url='sss';
//if video link is facebook
    if (strpos($link, 'facebook') !== false) {
        $embed_url=fb_embed_link($link);
    }
//if video link is youtube
    if (strpos($link, 'youtube.com') !== false) {
        $embed_url=youtube_embed_link($link);
    }
    return $embed_url;
}

Upvotes: 0

pinaldesai
pinaldesai

Reputation: 1815

Try Below Code... Should Work

<?php
$vari ='';

if($_POST)
{
    $vari       = $_POST['yurl'];
    $hth        = $_POST['yheight'];
    $wdth       = $_POST['ywidth'];
    $is_auto    =   0;

    $step1 =explode ('v=', $vari);
    $step2 =explode ('&amp;',$step1[1]);

    if(isset($_POST['yautop'] ))
    {
        if($_POST['yautop'] == 1)
            $is_auto    =   1;
    }
    $auto1  = '?autoplay='.$is_auto;
    $auto2  = '&autoplay='.$is_auto;

//Iframe code with autoplay

echo htmlentities ('<iframe src="http://ww.youtube.com/embed/'.$step2[0].$auto1.'" frameborder="0" width="'.$wdth.'" height="'.$hth.'"></iframe>');

//embed code with autoplay
echo htmlentities ('<embed width="'.$wdth.'" height="'.$hth.'" type="application/x-shockwave-flash" src="http://www.youtube.com/v/'.$step2[0].$auto2.'" wmode="transparent" embed="" /></embed>');
}
?>

Problems were

  1. concatenation operator (.) was missing
  2. variable name was invalid for "$atuo1"
  3. post block was missing.

Updates

please replace iframe line with below one (note : one "w" was missing in URL it was "ww")

echo htmlentities ('<iframe src="http://www.youtube.com/embed/'.$step2[0].$auto1.'" frameborder="0" width="'.$wdth.'" height="'.$hth.'"></iframe>');

Upvotes: 0

Anirudh Ramanathan
Anirudh Ramanathan

Reputation: 46728

You have missed a concatenation operator (.) after $step2[0].$auto2 and $step2[0].$auto1 in both the echo blocks.

echo htmlentities (...)

It should be:

 echo htmlentities ('<embed width="'.$wdth.'" height="'.$hth.'"type="application/x-shockwave-flash" src="http://www.youtube.com/v/"'.$step2[0].$auto2.'"wmode="transparent" embed="" /></embed>');

Also, in your Iframe code with autoplay you have misspelled $auto1. That wouldn't generate an error, but it wont work right.

Upvotes: 1

Related Questions