Get title with youtube api

Discussion in 'PHP' started by assault14, Feb 25, 2011.

  1. #1
    hey i am stuck, what i am trying to do here is, have a user submit a youtube url, then when it gets posted into the database it would grab the title of the video from the video id and post the title in the database.

    here -

    <?php
    
    
    require_once "../../maincore.php";
    require_once THEMES."templates/header.php"; 
    
    if (file_exists(INFUSIONS."video_infusions/locale/".$settings['locale'].".php")) {
    	include INFUSIONS."video_infusions/locale/".$settings['locale'].".php";
    } else {
    	include INFUSIONS."video_infusions/locale/English.php";
    }
    
    add_to_title(" - ".$locale['300a']);
    
    global $userdata;
    
    
    
    function youtube($y) {
    	if (strpos($y, "user") != 0) {
    	$y1 = substr($y, -11); 
    	return $y1;} else {
    	$y2 = strpos($y, "v=");
    	$y2 = substr($y, $y2+2, 11); return $y2; }
    	
    }
    
    $result = dbquery("SELECT * FROM ".$db_prefix."video_cats");
    if (dbrows($result) != 0) {
    
    	if (isset($_POST['save_video'])) {
    		$url = "http://gdata.youtube.com/feeds/api/videos/".$video_url."";
        	$doc = new DOMDocument;
        	$doc->load($url);
    		$video_title = $doc->getElementsByTagName("title")->item(0)->nodeValue;
    		$video_cat = stripinput($_POST['video_cat']);
    		$video_description = addslash($_POST['video_description']);
    		$video_url = youtube(stripinput($_POST['video_url']));
    		$video_type = "1";
    		$vk_img = "";
    		$user_id = $userdata['user_id'];
    		$result = dbquery("INSERT INTO ".$db_prefix."videos (video_user, video_title, video_description, video_url, video_cat, video_type, video_datestamp, video_count, video_image) VALUES ('".$user_id."', '".$video_title."', '".$video_description."', '".$video_url."', '".$video_cat."', '1', '".time()."', '0', '".$vk_img."' )");	
    		redirect("../?c=$video_cat");
        	
    
    	}
    
    		$video_url = "";
    		$formaction = FUSION_SELF;
    		;
    
    	$editlist = ""; $sel = "";
    	$result2 = dbquery("SELECT * FROM ".$db_prefix."video_cats ORDER BY video_cat_name");
    	if (dbrows($result2) != 0) {
    		while ($data2 = dbarray($result2)) {
    			$editlist .= "<option value='".$data2['video_cat_id']."'$sel>".$data2['video_cat_name']."</option>\n";
    		}
    	}
    	
    	opentable("");
    echo "
    <form name='inputform' method='post' action='$formaction'>
    <table widht='100%' align='center'>
    <tr>
    <td width='120' class='tbl'></td>
    <td ><p align='center' style='font-size:16px; float:left; color:#151515'><b>".$locale['452']."</b></p></td>
    </tr>
    
    <tr>     
    <td width='120'>".$locale['482']."</td>
    <td><input type='text' class=\"textbox\" name='video_url' value=\"http://www.youtube.com/watch?v=uBnJmlg-x48...\"  onblur=\"if(this.value=='') this.value='http://www.youtube.com/watch?v=uBnJmlg-x48...';\" onfocus=\"if(this.value=='http://www.youtube.com/watch?v=uBnJmlg-x48...') this.value='';\" style='width:100%; margin:5px 0'></td>
    </tr>
    <tr>
    <td align='center' colspan='2' class='tbl'>";
    	echo "<input type='submit' name='save_video' value='".$locale['471']."' class='button' style='width:120px;margin-top:6px;'></td>
    </tr>
    </table>
    </form>";
    closetable();
    
    }
    
    require_once THEMES."templates/footer.php";
    ?>
    Code (markup):
    thanks
     
    assault14, Feb 25, 2011 IP
  2. assault14

    assault14 Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    please help? thanks
     
    assault14, Feb 26, 2011 IP
  3. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Youtube API returns XML responses not DOM , so make use of the following :
    
    <?php
        $url = 'http://gdata.youtube.com/feeds/api/videos/';
        
        $data = file_get_contents($url);
        $xml = new SimpleXMLElement($data);
        
        foreach($xml->entry as $key => $value)
        {
            echo $value->title.'<br />';
        }
    ?>
    
    PHP:
     
    tvoodoo, Feb 26, 2011 IP