defroster Posted August 23, 2010 Share Posted August 23, 2010 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? url = "http://www.youtube.com/watch?v=kMAI26FPeyM"; Thanks Quote Link to comment https://forums.phpfreaks.com/topic/211470-extract-information-from-youtube-into-a-string/ Share on other sites More sharing options...
trq Posted August 23, 2010 Share Posted August 23, 2010 You woulds be much better off using the api that youtube provide. http://code.google.com/apis/youtube/overview.html Quote Link to comment https://forums.phpfreaks.com/topic/211470-extract-information-from-youtube-into-a-string/#findComment-1102593 Share on other sites More sharing options...
defroster Posted August 23, 2010 Author Share Posted August 23, 2010 Thanks. I have been looking into this but can't just get the videotitle and so on to extract. If someone has a coding example would be very much appreciated. /df Quote Link to comment https://forums.phpfreaks.com/topic/211470-extract-information-from-youtube-into-a-string/#findComment-1102614 Share on other sites More sharing options...
Wolphie Posted August 23, 2010 Share Posted August 23, 2010 Give this a try. <?php function video_entry($url) { if (strchr($url, 'http://')) { $video_id = parse_url($url, PHP_URL_QUERY); $video_id = str_replace('v=', '', $video_id); } else { $video_id = $url; } // Make the initial API request $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds/api/videos/' . $video_id); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Store XML results in a string $result = curl_exec($ch); curl_close($ch); if ($xml = simplexml_load_string($result)) { return $xml; } else { die ('An error occurred trying to load XML data.'); } } $video = video_entry('http://www.youtube.com/watch?v=kMAI26FPeyM'); echo $video->title; // Scooter Power!! ?> You can use either the video id, or the entire URL. e.g. $video = video_entry('kMAI26FPeyM'); // or $video = video_entry('http://www.youtube.com/watch?v=kMAI26FPeyM'); Also if you use print_r on $video, it will give you all of the array and object information returned. E.g. $video = video_entry('http://www.youtube.com/watch?v=kMAI26FPeyM'); echo '<pre>'; print_r($video); echo '</pre>'; will return: SimpleXMLElement Object ( [id] => http://gdata.youtube.com/feeds/api/videos/kMAI26FPeyM [published] => 2010-08-22T08:24:02.000Z [updated] => 2010-08-23T09:15:57.000Z [category] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [scheme] => http://schemas.google.com/g/2005#kind [term] => http://gdata.youtube.com/schemas/2007#video ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [scheme] => http://gdata.youtube.com/schemas/2007/categories.cat [term] => Entertainment [label] => Entertainment ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [scheme] => http://gdata.youtube.com/schemas/2007/keywords.cat [term] => scooter ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [scheme] => http://gdata.youtube.com/schemas/2007/keywords.cat [term] => power ) ) ) [title] => Scooter Power!! [content] => sp [link] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [rel] => alternate [type] => text/html [href] => http://www.youtube.com/watch?v=kMAI26FPeyM&feature=youtube_gdata ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [rel] => http://gdata.youtube.com/schemas/2007#video.responses [type] => application/atom+xml [href] => http://gdata.youtube.com/feeds/api/videos/kMAI26FPeyM/responses ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [rel] => http://gdata.youtube.com/schemas/2007#video.related [type] => application/atom+xml [href] => http://gdata.youtube.com/feeds/api/videos/kMAI26FPeyM/related ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [rel] => http://gdata.youtube.com/schemas/2007#mobile [type] => text/html [href] => http://m.youtube.com/details?v=kMAI26FPeyM ) ) [4] => SimpleXMLElement Object ( [@attributes] => Array ( [rel] => self [type] => application/atom+xml [href] => http://gdata.youtube.com/feeds/api/videos/kMAI26FPeyM ) ) ) [author] => SimpleXMLElement Object ( [name] => josdoming [uri] => http://gdata.youtube.com/feeds/api/users/josdoming ) ) Quote Link to comment https://forums.phpfreaks.com/topic/211470-extract-information-from-youtube-into-a-string/#findComment-1102631 Share on other sites More sharing options...
defroster Posted August 23, 2010 Author Share Posted August 23, 2010 Wow that is brilliant! Thank you so much!! I managed to extract -title -description -date published Is there a possibility to also get the votes, youtube username, and the tags like (scooter, power) from the clip? I was trying but was unable to get those. Thanks SO MUCH for the help! The code currently using: <?php function video_entry($url) { if (strchr($url, 'http://')) { $video_id = parse_url($url, PHP_URL_QUERY); $video_id = str_replace('v=', '', $video_id); } else { $video_id = $url; } // Make the initial API request $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds/api/videos/' . $video_id); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Store XML results in a string $result = curl_exec($ch); curl_close($ch); if ($xml = simplexml_load_string($result)) { return $xml; } else { die ('An error occurred trying to load XML data.'); } } $video = video_entry('kMAI26FPeyM'); #$video = video_entry('http://www.youtube.com/watch?v=kMAI26FPeyM'); echo $video->title; // Video title echo $video->content; // Description echo $video->published; // date ?> Quote Link to comment https://forums.phpfreaks.com/topic/211470-extract-information-from-youtube-into-a-string/#findComment-1102642 Share on other sites More sharing options...
Wolphie Posted August 23, 2010 Share Posted August 23, 2010 No idea how to get votes, but this gets the tags and the authors name: <?php function video_entry($url) { if (strchr($url, 'http://')) { $video_id = parse_url($url, PHP_URL_QUERY); $video_id = str_replace('v=', '', $video_id); } else { $video_id = $url; } // Make the initial API request $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds/api/videos/' . $video_id); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1); curl_setopt($ch, CURLOPT_PROXY, 'http://emeacache.uk.oracle.com:80'); // Store XML results in a string $result = curl_exec($ch); curl_close($ch); if ($xml = simplexml_load_string($result)) { $count = 0; foreach ($xml->category as $category) { if ($count > 1) { $xml->tags[] = $category[0]['term']; } $count++; } return $xml; } else { die ('An error occurred trying to load XML data.'); } } $video = video_entry('http://www.youtube.com/watch?v=kMAI26FPeyM'); echo $video->title; // Video title echo $video->content; // Description echo $video->published; // date echo $video->tags[0] . ' - ' . $video->tags[1]; // scooter - power echo $video->author->name; // author name ?> Quote Link to comment https://forums.phpfreaks.com/topic/211470-extract-information-from-youtube-into-a-string/#findComment-1102659 Share on other sites More sharing options...
defroster Posted August 23, 2010 Author Share Posted August 23, 2010 Thanks Wolphie. But unfortunately when I use the code you provided I get an error? An error occurred trying to load XML data. code used: <?php function video_entry($url) { if (strchr($url, 'http://')) { $video_id = parse_url($url, PHP_URL_QUERY); $video_id = str_replace('v=', '', $video_id); } else { $video_id = $url; } // Make the initial API request $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds/api/videos/' . $video_id); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1); curl_setopt($ch, CURLOPT_PROXY, 'http://emeacache.uk.oracle.com:80'); // Store XML results in a string $result = curl_exec($ch); curl_close($ch); if ($xml = simplexml_load_string($result)) { $count = 0; foreach ($xml->category as $category) { if ($count > 1) { $xml->tags[] = $category[0]['term']; } $count++; } return $xml; } else { die ('An error occurred trying to load XML data.'); } } $video = video_entry('http://www.youtube.com/watch?v=kMAI26FPeyM'); echo $video->title; // Video title echo $video->content; // Description echo $video->published; // date echo $video->tags[0] . ' - ' . $video->tags[1]; // scooter - power echo $video->author->name; // author name ?> Quote Link to comment https://forums.phpfreaks.com/topic/211470-extract-information-from-youtube-into-a-string/#findComment-1102662 Share on other sites More sharing options...
defroster Posted August 26, 2010 Author Share Posted August 26, 2010 Thanks Wolphie. But unfortunately when I use the code you provided I get an error? An error occurred trying to load XML data. code used: <?php function video_entry($url) { if (strchr($url, 'http://')) { $video_id = parse_url($url, PHP_URL_QUERY); $video_id = str_replace('v=', '', $video_id); } else { $video_id = $url; } // Make the initial API request $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds/api/videos/' . $video_id); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1); curl_setopt($ch, CURLOPT_PROXY, 'http://emeacache.uk.oracle.com:80'); // Store XML results in a string $result = curl_exec($ch); curl_close($ch); if ($xml = simplexml_load_string($result)) { $count = 0; foreach ($xml->category as $category) { if ($count > 1) { $xml->tags[] = $category[0]['term']; } $count++; } return $xml; } else { die ('An error occurred trying to load XML data.'); } } $video = video_entry('http://www.youtube.com/watch?v=kMAI26FPeyM'); echo $video->title; // Video title echo $video->content; // Description echo $video->published; // date echo $video->tags[0] . ' - ' . $video->tags[1]; // scooter - power echo $video->author->name; // author name ?> I'm still completely stuck trying to figure out why there is a XML error? any ideas? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/211470-extract-information-from-youtube-into-a-string/#findComment-1103980 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.