Extract information from YouTube into a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
defroster
Forum Commoner
Posts: 49
Joined: Wed Mar 24, 2010 12:05 pm

Extract information from YouTube into a string

Post by defroster »

Hello,

Anyone know a way I could extract information from YouTube. If I have the URL for the video, how could I get the title, runtime, tags and so on into different strings?

Code: Select all

   url = "http://www.youtube.com/watch?v=kMAI26FPeyM";

Thanks
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Extract information from YouTube into a string

Post by yacahuma »

This is how you get the title and tags. I dont know what the runtime is

Code: Select all

$url =   'http://www.youtube.com/watch?v=kMAI26FPeyM';
$html = new DOMDocument();
@$html->loadHtmlFile($url);
$xpath = new DOMXPath( $html );
$xpathstr = '//*[@id="eow-title"]';
$nodelist = $xpath->query( $xpathstr );
foreach ($nodelist as $n){
echo $n->nodeValue."<br />"; 
}

$xpathstr = '//*[@id="eow-tags"]/li';  //or '//*[@id="eow-tags"] for everything in one line
$nodelist = $xpath->query( $xpathstr );
foreach ($nodelist as $n){
echo $n->nodeValue."<br />";

}
amargharat
Forum Commoner
Posts: 82
Joined: Wed Sep 16, 2009 2:43 am
Location: Mumbai, India
Contact:

Re: Extract information from YouTube into a string

Post by amargharat »

.
Last edited by amargharat on Mon Aug 30, 2010 2:13 am, edited 1 time in total.
amargharat
Forum Commoner
Posts: 82
Joined: Wed Sep 16, 2009 2:43 am
Location: Mumbai, India
Contact:

Re: Extract information from YouTube into a string

Post by amargharat »

You can apply simple way to get youtube's video title, description and keywords as follows,

Code: Select all

$url =   'http://www.youtube.com/watch?v=kMAI26FPeyM';
$data = get_meta_tags($url);

$title = $data["title"];
$description = $data["description"];
$keywords = $data["keywords"];
defroster
Forum Commoner
Posts: 49
Joined: Wed Mar 24, 2010 12:05 pm

Re: Extract information from YouTube into a string

Post by defroster »

Thank you very much. They both work great! /df
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Extract information from YouTube into a string

Post by yacahuma »

the big difference is that the get_meta_tags will only get you what is in the head section. The Dom will get you anything in the document. If all you need is whatever they put in the head section I will use the get_meta_tags. Much simpler.
Post Reply