Web Design Forum: Help with YouTube API PHP Script - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Help with YouTube API PHP Script Rate Topic: -----

#1 User is offline   JamesW 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 320
  • Joined: 28-July 09
  • Reputation: 2
  • Gender:Male
  • Location:London/Essex, UK
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 06 August 2009 - 11:36 AM

This is quite complicated so I'm not going to go into every detail but to give you a brief summary...

- Song Names are held in MySQL Database
- Take them out and echo into a table
- Add the song name and artist into this url:
http://gdata.youtube...e&max-results=1

- Using simple_xml_load_file I load the xml file that the search url ^ generates and then pick out the video url with:

$attrs = $media->group->player->attributes();
$watch = $attrs['url']; 


Then this is where I think the problem comes, to play the video using YouTube's api player you have to enter in the Video ID example dvgZkm1xWPE

I couldn't find a way to just pick this out of the XML file so I had to take the whole URL and then parse it with this piece of code I found online:

$string = $watch;
$url = parse_url($string);
parse_str($url['query']);


Now this works fine for the first row of my table but then every other row just plays the same video as the first row. I'm not sure why but I'm guessing it's because the $v variable it generates isn't changing which is what I need it to do..

Any ideas on how to get it to change for every row or a better way to go about this would really be really helpful !

Thanks :)


Before someone asks this is all the code:
<?php
	$con = mysql_connect("localhost","*******","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("fsdb", $con);

$result = mysql_query("SELECT *FROM test");



while($row = mysql_fetch_array($result))
 
 
 {
	  	  
	  
	$name = $row['Name'];
	$artist = $row['Artist'];
	
	
	echo '<table width="90%" align="center" border="1" cellpadding="5" cellspacing="0"><tr><th width="25%" class="row">';
	echo $row['Name'];
	echo '</th>';
	echo '<th width="25%" " class="row">';
	echo $row['Artist'];
	echo '</th>';
	echo '<th width="25%" class="row">';
	echo '<a href="http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsSearch?term=';
	echo $row['Name'];
	echo "+";
	echo $row['Artist'];
	echo '&media=music&limit=1">Download</a>';
	echo '</th>';
	echo '<th width="25%" class="row">';

	$gdata = "http://gdata.youtube.com/feeds/api/videos?q=$name+$artist&orderby=relevance&max-results=1";
    

	$request_url = $gdata;
	$xml = simplexml_load_file($gdata) or die("Song could not be found, sorry.");
     
	foreach ($sxml->entry as $entry) 
        // get nodes in media: namespace for media information
        $media = $entry->children('http://search.yahoo.com/mrss/');
		
	//All the media namespaced items under a YouTube video entry 
  $media = $xml->entry->children('http://search.yahoo.com/mrss/'); 
 

        // get video player URL
        $attrs = $media->group->player->attributes();
        $watch = $attrs['url']; 
		//echo $gdata;
		echo $watch;
		
$string = $watch;
$url = parse_url($string);
parse_str($url['query']);
?>
<!-- Use the Google AJAX Libraries API:
        http://code.google.com/apis/ajaxlibs/ --> 
    <script src="http://www.google.com/jsapi"></script> 
    <script> 
      google.load("swfobject", "2.1");
    </script> 
    <style type="text/css"> 
 
    body {
      font-family: verdana, helvetica;
      background-color: white;
    }
 
    #timedisplay {
      border: solid 1px red;
      width: 50px;
    }
    </style> 
    <script type="text/javascript"> 
 
        function updateHTML(elmId, value) {
          document.getElementById(elmId).innerHTML = value;
        }
 
        function setytplayerState(newState) {
          updateHTML("playerstate", newState);
        }
 
        function onYouTubePlayerReady(playerId) {
          ytplayer = document.getElementById("myytplayer");
          setInterval(updateytplayerInfo, 250);
          updateytplayerInfo();
          ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
          ytplayer.addEventListener("onError", "onPlayerError");
        }
 
        function onytplayerStateChange(newState) {
          setytplayerState(newState);
        }
 
        function onPlayerError(errorCode) {
          alert("An error occured: " + errorCode);
        }
 
        function updateytplayerInfo() {
          updateHTML("bytesloaded", getBytesLoaded());
          updateHTML("bytestotal", getBytesTotal());
          updateHTML("videoduration", getDuration());
          updateHTML("videotime", getCurrentTime());
          updateHTML("startbytes", getStartBytes());
          updateHTML("volume", getVolume());
        }
 
        // functions for the api calls
        function loadNewVideo(id, startSeconds) {
          if (ytplayer) {
            ytplayer.loadVideoById(id, parseInt(startSeconds));
          }
        }
 
        function cueNewVideo(id, startSeconds) {
          if (ytplayer) {
            ytplayer.cueVideoById(id, startSeconds);
          }
        }
 
        function play() {
          if (ytplayer) {
            ytplayer.playVideo();
          }
        }
 
        function pause() {
          if (ytplayer) {
            ytplayer.pauseVideo();
          }
        }
 
        function stop() {
          if (ytplayer) {
            ytplayer.stopVideo();
          }
        }
 
        function getPlayerState() {
          if (ytplayer) {
            return ytplayer.getPlayerState();
          }
        }
 
        function seekTo(seconds) {
          if (ytplayer) {
            ytplayer.seekTo(seconds, true);
          }
        }
 
        function getBytesLoaded() {
          if (ytplayer) {
            return ytplayer.getVideoBytesLoaded();
          }
        }
 
        function getBytesTotal() {
          if (ytplayer) {
            return ytplayer.getVideoBytesTotal();
          }
        }
 
        function getCurrentTime() {
          if (ytplayer) {
            return ytplayer.getCurrentTime();
          }
        }
 
        function getDuration() {
          if (ytplayer) {
            return ytplayer.getDuration();
          }
        }
 
        function getStartBytes() {
          if (ytplayer) {
            return ytplayer.getVideoStartBytes();
          }
        }
 
        function mute() {
          if (ytplayer) {
            ytplayer.mute();
          }
        }
 
        function unMute() {
          if (ytplayer) {
            ytplayer.unMute();
          }
        }
        
        function getEmbedCode() {
          alert(ytplayer.getVideoEmbedCode());
        }
 
        function getVideoUrl() {
          alert(ytplayer.getVideoUrl());

        }
        
        function setVolume(newVolume) {
          if (ytplayer) {
            ytplayer.setVolume(newVolume);
          }
        }
 
        function getVolume() {
          if (ytplayer) {
            return ytplayer.getVolume();
          }
        }
 
        function clearVideo() {
          if (ytplayer) {


            ytplayer.clearVideo();
          }
        }
        
 
    </script> 
<!--Api Player--><span id="ytapiplayer"> </span> 
 <script type="text/javascript"> 
      // <![CDATA[
 
      // allowScriptAccess must be set to allow the Javascript from one 
      // domain to access the swf on the youtube domain
      var params = { allowScriptAccess: "always", bgcolor: "#cccccc" };
      // this sets the id of the object or embed tag to 'myytplayer'.
      // You then use this id to access the swf and make calls to the player's API
      var atts = { id: "myytplayer" };
      swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=ytplayer", 
       //Height and width go below
	   "ytapiplayer", "100", "100", "8", null, null, params, atts);
      //]]>
    </script> 
 
 
 
 
 
 
    


<!-- Video id-->
      <input type="hidden" size="11" id="cuevideoid" value="<?php echo $v; ?>" /> 
      
      
  <!-- Start at 0 seconds = always hidden --> 
   <input type="hidden" size="4" id="startseconds2" value="0" /> 
 
      
      <!-- Controls --> 
      
        <div style="float: left; width: 80px;">
    
    
    <!--PLAY and LOAD--><a href="javascript:void(0);" onclick="
    cueNewVideo(document.getElementById('cuevideoid').value, document.getElementById('startseconds2').value);
    play();">
    <img src="/fstest/img/play.png" width="25" height="25" alt="Play" /></a> 
    
    <!-- PAUSE    Temporarily Removed! -->
   <!-- <a href="javascript:void(0);" onclick="pause();">Pause</a> -->
    
    <!-- STOP -->
      <a href="javascript:void(0);" onclick="stop();"><img src="/fstest/img/stop.png" width="25" height="25" alt="Stop" /></a>
      
      </div>
    
    
   

  <input id="vol" type="hidden" value"100"  size="2" /> 
      <a href="javascript:void(0)" onclick="setVolume(document.getElementById('vol').value)"> <span id="volume"></span> 

<?php
	echo '</th>';
	echo '</tr>';
	echo '</table>';
	

  }



	
	
	
mysql_close($con);


?>


0

#2 User is offline   morgano 

  • Dedicated Member
  • PipPip
  • Group: Members
  • Posts: 118
  • Joined: 10-October 08
  • Reputation: 18
  • Gender:Male
  • Location:Wales
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 06 August 2009 - 12:49 PM

I'm not 100% sure what your video ID variable is but I'm guessing it's "$v" if it is then...

while($row = mysql_fetch_array($result))
 
 
 {
                  
        unset($v);
        $name = $row['Name'];
        $artist = $row['Artist'];


This will at least clear the variable. Although I'm thinking the problem is deeper.
0

#3 User is offline   JamesW 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 320
  • Joined: 28-July 09
  • Reputation: 2
  • Gender:Male
  • Location:London/Essex, UK
  • Experience:Advanced
  • Area of Expertise:Designer/Coder

Posted 06 August 2009 - 01:19 PM

Will give that a try, thanks
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users