Page 1 of 1
Extract information from YouTube into a string
Posted: Mon Aug 23, 2010 1:43 am
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
Re: Extract information from YouTube into a string
Posted: Sun Aug 29, 2010 8:24 pm
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 />";
}
Re: Extract information from YouTube into a string
Posted: Mon Aug 30, 2010 2:11 am
by amargharat
.
Re: Extract information from YouTube into a string
Posted: Mon Aug 30, 2010 2:12 am
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"];
Re: Extract information from YouTube into a string
Posted: Mon Aug 30, 2010 4:09 am
by defroster
Thank you very much. They both work great! /df
Re: Extract information from YouTube into a string
Posted: Mon Aug 30, 2010 5:09 am
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.