• 0

YouTube PHP Script?


Question

I'm looking for a script that will PULL my videos from youtube and display them in a list or whatever.

I've transfered all my websites vids to youtube and at the moment i'm just linking to the youtube page but would love to integrate it into my website properly.

does such a thing exist?

Link to comment
Share on other sites

17 answers to this question

Recommended Posts

  • 0

You really just want to parse the XML file for your "My Videos" feed?

http://gdata.youtube.com/feeds/base/users/[u][b]YOURUSERNAME[/b][/u]/uploads?client=ytapi-youtube-user&v=2

Link to comment
Share on other sites

  • 0

No problems. They may have API's to integrate with, but that was the simplest option in my mind - that said, I don't know what restrictions it may have (max count, etc)

Link to comment
Share on other sites

  • 0

No problems. They may have API's to integrate with, but that was the simplest option in my mind - that said, I don't know what restrictions it may have (max count, etc)

Just out of curiosity, anyway in getting that in JSON format? :p

Link to comment
Share on other sites

  • 0

I'm looking at the XML file now.

<div class="entry">
 	<h3><a href="http://www.youtube.com/watch?v=5pfu1b63s20&feature=youtube_gdata">Vibe @ The Studio, Durham 01-10-2009 Part 2</a>

How would i grab the title from that?

I've been looking online and it suggests using GetElementById or TagName but it does't have an Id.

(n00b)

Link to comment
Share on other sites

  • 0

I personally use SimplePie's class for XML parsing and feed fetching. You may want to look at it. It just makes it easier.

They may have API's to integrate with, but that was the simplest option in my mind - that said, I don't know what restrictions it may have (max count, etc)

As long as you cache the feeds you are fine (insert the records in database etc). If you don't and your website gets alot of hits, then the server will get banned eventually. That said, fetching the feed like once in a hour and you should be fine.

Link to comment
Share on other sites

  • 0

I personally use SimplePie's class for XML parsing and feed fetching. You may want to look at it. It just makes it easier.

As long as you cache the feeds you are fine (insert the records in database etc). If you don't and your website gets alot of hits, then the server will get banned eventually. That said, fetching the feed like once in a hour and you should be fine.

Wow that's amazing i'm already up and running.

One problem is the links are coming out like this:

http://www.youtube.c...3Dyoutube_gdata

i quickly tried:

$replace = str_replace("%3D", "=", $item);

which did change the link in the URL on the page, but when it's clicked it takes you to this:

http://www.youtube.c...3Dyoutube_gdata

This is the full code i used:

<?
foreach ($feed->get_items() as $item): ?>
<div class="item">
<h2><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></h2>

<p><?php echo $item->get_description(); ?></p>
<p><small>Posted on <?php echo $item->get_date('j F Y | g:i a'); ?></small></p>
</div>
<?php endforeach; ?> 

Link to comment
Share on other sites

  • 0

Thanks it worked great :-)

Is it now possible to limit this to show a certain number of videos? a limit on the loop.

This is the code now:

 <? foreach ($feed->get_items() as $item): ?> 	
<div style="padding:15px;"> 	
<h2><a href="<?php echo urldecode($item->get_permalink()); ?>"><?php echo $item->get_title(); ?></a></h2> 	
<p><?php echo $item->get_description(); ?></p> 	
<p><small>Posted on <?php echo $item->get_date('j F Y | g:i a'); ?></small></p> 	
</div> 	
<?php endforeach; ?> 

Link to comment
Share on other sites

  • 0

You could switch to a regular for-loop from 0 to n.

<?php
$items = feed->get_items();
for ($i = 0; $i < 5; ++$i) {
    $item = $items[$i];
    // Output item
}
?>

You could also keep the foreach-loop and add a key enumerator variable. After this enumerator hits n, you break out of the loop before outputting the next item.

<?php
foreach ($feed->get_items() as $i => $item) {
    if($i >= 5) break;
    // Output item
}
?>

Of course, it would be better and more efficient if you'd put the limit directly on the get_items() function or even on the feed request query itself rather than retrieving all items and then only using some.

Link to comment
Share on other sites

  • 0

Of course, it would be better and more efficient if you'd put the limit directly on the get_items() function or even on the feed request query itself rather than retrieving all items and then only using some.

Yep. See get_items().

foreach($feed->get_items(0,5) as $item) [...]

Link to comment
Share on other sites

  • 0

That also worked, it's coming along nicely.

Last question lol.

<p><?php echo $item->get_description(); ?></p>

that code grabs the author, thumbnail, description, rating etc,,

is it possible to just grab the thumbnail?

Link to comment
Share on other sites

  • 0

I've found that Youtube thumbnails all contain a url such as:

http://img.youtube.com/vi/*VIDEO_ID*/2.jpg

So i created a line of code to strip the VIDEO_ID out of the url:

$thumb = substr(urldecode($item-&gt;get_permalink()),strpos(urldecode($item-&gt;get_permalink()),"="),11);

This SHOULD grab all 11 characters after the =

But it's showing the string as:

=IW3wM5s5OE

Now what would i use to remove the "=" from the string?

I've tried a few replace functions but it doesn't seem to be working.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.