Mancent Posted October 9, 2011 Share Posted October 9, 2011 I am wanting to write a script that will allow a user to search youtube videos! What I have so far is not working yet, but I know it can be done! I found this code and it works and its not a million api files to get one thing working! <?php # YouTube Video Collector - YouTube.class.php ##################################################### # # # YouTube Video Collector 2010 # # # # Author: CMBSystems (Chris Bearcroft) # # Copyright: CMBSystems 2010, All Rights Reserved # # Last Modded: 29-09-2010, 1:26 AM (GMT) # # # ##################################################### class YouTube { public function getURL($Search){ if(!empty($Search)){ $Search=split("\n",$Search); for($i=0;$i<count($Search);$i++){ $SearchURL=urlencode($Search[$i]); $SearchURL="http://www.youtube.com/results?search_category=10&search_type=videos&search_query={$SearchURL}"; preg_match('/\/watch\?v=(.*?)\"/',file_get_contents($SearchURL),$URL); if(strpos($URL[1],'&')){$URL=split('&',$URL[1]);$URL=$URL[0];}else{$URL=$URL[1];} $array[]="http://www.youtube.com/watch?v={$URL}"; } return $array; } } public function printForm($Search){ $Search=htmlspecialchars(stripslashes($Search)); return "<form method=\"get\" action=\"\">\n <table>\n <tr>\n". " <td><textarea cols=\"60\" rows=\"15\" name=\"search_query\">{$Search}</textarea></td>\n". " </tr>\n <tr>\n <td align=\"right\"><input type=\"submit\" value=\"Search YouTube\" /></td>\n". " </tr>\n </table>\n</form>\n"; } } ?> <?php # YouTube Video Collector - YouTube.php ##################################################### # # # YouTube Video Collector 2010 # # # # Author: CMBSystems (Chris Bearcroft) # # Copyright: CMBSystems 2010, All Rights Reserved # # Last Modded: 29-09-2010, 1:26 AM (GMT) # # # ##################################################### # Require the YouTube class require 'YouTube.class.php'; # Set the PHP vars $Form = YouTube::printForm($_GET['search_query']); $Result = YouTube::getURL($_GET['search_query']); print <<<YTVSC <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>YouTube Collector</title> </head> <body> {$Form} YTVSC; print_r($Result); print <<<YTVSC </body> </html> YTVSC; ?> It allows me to have a feed link on my site to get the data http://claritydreams.com/youtube/YouTube.php?YT_Search=Staind I can push that ?YT_Search as a search from with in my client but I want to convert the file in to xml to read the data back into the client. Quote Link to comment https://forums.phpfreaks.com/topic/248770-simple-search-to-list-youtube-results-help/ Share on other sites More sharing options...
Mancent Posted October 9, 2011 Author Share Posted October 9, 2011 Problem Solved! you guys can have it! http://claritydreams.com/youtube/search.php?SreachName=Staind <?php $SreachName = $_GET['SreachName']; if(empty($SreachName)) { echo "No Search"; return true; } // set feed URL $feedURL = 'https://gdata.youtube.com/feeds/api/videos?q='.$SreachName; // read feed into SimpleXML object $sxml = simplexml_load_file($feedURL); // echo $sxml->title; $youtube_xml = "<?xml version=\"1.0\"?>\n"; $youtube_xml .= "<root>\n"; $youtube_xml .= "<youtube>\n"; // iterate over entries in feed foreach ($sxml->entry as $entry) { // get nodes in media: namespace for media information $media = $entry->children('http://search.yahoo.com/mrss/'); // get video player URL $attrs = $media->group->player->attributes(); $watch = $attrs['url']; // get video thumbnail $attrs = $media->group->thumbnail[0]->attributes(); $thumbnail = $attrs['url']; // get <yt:duration> node for video length $yt = $media->children('http://gdata.youtube.com/schemas/2007'); $attrs = $yt->duration->attributes(); $length = $attrs['seconds']; // get <yt:stats> node for viewer statistics $yt = $entry->children('http://gdata.youtube.com/schemas/2007'); $attrs = $yt->statistics->attributes(); $viewCount = $attrs['viewCount']; // get <gd:rating> node for video ratings $gd = $entry->children('http://schemas.google.com/g/2005'); if ($gd->rating) { $attrs = $gd->rating->attributes(); $rating = $attrs['average']; } else { $rating = 0; } $youtube_xml .= "<watch>" . $watch . "</watch>\n"; $youtube_xml .= "<grouptitle>" . $media->group->title . "</grouptitle>\n"; $youtube_xml .= "<groupdiscription>" . $media->group->description . "</groupdiscription>\n"; $youtube_xml .= "<thumbnail>" . $thumbnail . "</thumbnail>\n"; $youtube_xml .= "<name>" . $entry->author->name. "</name>\n"; $youtube_xml .= "<views>" . $viewCount. "</views>\n"; $youtube_xml .= "<rating>" . $rating. "</rating>\n"; } $youtube_xml .= "</youtube>\n"; $youtube_xml .= "</root>\n"; echo $youtube_xml; ?> Quote Link to comment https://forums.phpfreaks.com/topic/248770-simple-search-to-list-youtube-results-help/#findComment-1277589 Share on other sites More sharing options...
Mancent Posted October 9, 2011 Author Share Posted October 9, 2011 Ok I do have one last problem with this script. The xml file looks like this <?xml version="1.0"?> <root> <youtube> <watch>https://www.youtube.com/watch?v=araU0fZj6oQ&feature=youtube_gdata_player</watch> <grouptitle>Staind - It's Been Awhile (Video)</grouptitle> <groupdiscription>© 2007 WMG It's Been Awhile (Video)</groupdiscription> <thumbnail>http://i.ytimg.com/vi/araU0fZj6oQ/0.jpg</thumbnail> <name>AtlanticVideos</name> <views>3377551</views> <rating>4.9742484</rating> <watch>https://www.youtube.com/watch?v=r70UpNT_ZUc&feature=youtube_gdata_player</watch> <grouptitle>Staind Outside</grouptitle> <groupdiscription>Get this song for free at my website! audioslavelover.ueuo.com Please subscribe Staind Outside And you, Can bring me to my knees Again All the times That I could beg you please In vain All the times That I felt insecure For you And I leave My burdens at the door But I'm on the outside And I'm looking in I can see through you See your true colors 'Cause inside you're ugly You're ugly like me I can see through you See to the real you All the times That I felt like this won't end It's for you And I taste What I could never have It was from you All the times That I cried My intentions Were full of pride All I waste More time than anyone But I'm on the outside And I'm looking in I can see through you See your true colors 'Cause inside you're ugly You're ugly like me I can see through you See to the real you All the times That I've cried All this wasted It's all inside And I feel All this pain Stuffed it down It's back again And I lie Here in bed All alone I can't mend But I feel Tomorrow will be okay But I'm on the outside And I'm looking in I can see through you See your true colors 'Cause inside you're ugly You're ugly like me I can see through you See to the real you</groupdiscription> <thumbnail>http://i.ytimg.com/vi/r70UpNT_ZUc/0.jpg</thumbnail> <name>audioslavelover</name> <views>5406501</views> <rating>4.9244437</rating> <watch>https://www.youtube.com/watch?v=MbfsFR0s-_A&feature=youtube_gdata_player</watch> <grouptitle>Staind - For You</grouptitle> <groupdiscription>Staind music video for "For You" from: Break the Cycle</groupdiscription> <thumbnail>http://i.ytimg.com/vi/MbfsFR0s-_A/0.jpg</thumbnail> <name>sta1ndsoul</name> <views>5623114</views> <rating>4.918325</rating> <watch>https://www.youtube.com/watch?v=v_hyHSZ7bQw&feature=youtube_gdata_player</watch> <grouptitle>Staind - Not Again (Official Video)</grouptitle> <groupdiscription>Directed By Paul Brown © 2011 WMG. The first single from the new self-titled album coming 9/13/11! Click here to download the single: bit.ly/nG2zuy staind.com http twitter.com (less info)</groupdiscription> <thumbnail>http://i.ytimg.com/vi/v_hyHSZ7bQw/0.jpg</thumbnail> <name>StaindVideos</name> <views>191849</views> <rating>4.972097</rating> <watch>https://www.youtube.com/watch?v=b4_bl8uTonQ&feature=youtube_gdata_player</watch> <grouptitle>Staind - Right Here [OFFICIAL VIDEO]</grouptitle> <groupdiscription>Staind - Right Here [OFFICIAL VIDEO]</groupdiscription> <thumbnail>http://i.ytimg.com/vi/b4_bl8uTonQ/0.jpg</thumbnail> <name>StaindVideos</name> <views>1806450</views> <rating>4.9584513</rating> <watch>https://www.youtube.com/watch?v=5bobskjQwWI&feature=youtube_gdata_player</watch> <grouptitle>Staind - Epiphany [OFFICIAL VIDEO]</grouptitle> <groupdiscription>Staind - Epiphany [OFFICIAL VIDEO]</groupdiscription> <thumbnail>http://i.ytimg.com/vi/5bobskjQwWI/0.jpg</thumbnail> <name>StaindVideos</name> <views>1201159</views> <rating>4.958879</rating> <watch>https://www.youtube.com/watch?v=03cR1tAFSmQ&feature=youtube_gdata_player</watch> <grouptitle>Staind - Zoe Jane</grouptitle> <groupdiscription>Staind - Zoe Jane Music video This is the video for a Great Song performed by an Amazing band.Please Enjoy it...!!</groupdiscription> <thumbnail>http://i.ytimg.com/vi/03cR1tAFSmQ/0.jpg</thumbnail> <name>skiantozza</name> <views>1697963</views> <rating>4.942352</rating> <watch>https://www.youtube.com/watch?v=wPW7T_tu3PM&feature=youtube_gdata_player</watch> <grouptitle>Staind - So Far Away [OFFICIAL VIDEO]</grouptitle> <groupdiscription>Staind - So Far Away [OFFICIAL VIDEO]</groupdiscription> <thumbnail>http://i.ytimg.com/vi/wPW7T_tu3PM/0.jpg</thumbnail> <name>StaindVideos</name> <views>1481944</views> <rating>4.9668226</rating> <watch>https://www.youtube.com/watch?v=22hvEdaPSMs&feature=youtube_gdata_player</watch> <grouptitle>Staind Tangled Up In You</grouptitle> <groupdiscription>staind new cd the illusion of progress</groupdiscription> <thumbnail>http://i.ytimg.com/vi/22hvEdaPSMs/0.jpg</thumbnail> <name>smonbro26</name> <views>1947123</views> <rating>4.939783</rating> <watch>https://www.youtube.com/watch?v=faewCXX1mvo&feature=youtube_gdata_player</watch> <grouptitle>Staind - Everything Changes [OFFICIAL VIDEO]</grouptitle> <groupdiscription>Staind - Everything Changes [OFFICIAL VIDEO]</groupdiscription> <thumbnail>http://i.ytimg.com/vi/faewCXX1mvo/0.jpg</thumbnail> <name>StaindVideos</name> <views>1240805</views> <rating>4.9553986</rating> <watch>https://www.youtube.com/watch?v=06lHYtjvgLQ&feature=youtube_gdata_player</watch> <grouptitle>Staind - Mudshovel (Video)</grouptitle> <groupdiscription>© 2006 WMG Mudshovel (Video)</groupdiscription> <thumbnail>http://i.ytimg.com/vi/06lHYtjvgLQ/0.jpg</thumbnail> <name>StaindVideos</name> <views>384063</views> <rating>4.957159</rating> <watch>https://www.youtube.com/watch?v=mVQpfoqsY8Q&feature=youtube_gdata_player</watch> <grouptitle>Staind - Outside (Video)</grouptitle> <groupdiscription>© 2006 WMG Outside (Video)</groupdiscription> <thumbnail>http://i.ytimg.com/vi/mVQpfoqsY8Q/0.jpg</thumbnail> <name>AtlanticVideos</name> <views>1404505</views> <rating>4.9825873</rating> <watch>https://www.youtube.com/watch?v=3exGGeeVJcU&feature=youtube_gdata_player</watch> <grouptitle>Staind - The Way I Am (Video)</grouptitle> <groupdiscription>© 2008 WMG The Way I Am (Video)</groupdiscription> <thumbnail>http://i.ytimg.com/vi/3exGGeeVJcU/0.jpg</thumbnail> <name>StaindVideos</name> <views>868824</views> <rating>4.976913</rating> <watch>https://www.youtube.com/watch?v=g-UIENPam98&feature=youtube_gdata_player</watch> <grouptitle>Staind - Outside (Live on Rrhof)</grouptitle> <groupdiscription>Staind performing Outside</groupdiscription> <thumbnail>http://i.ytimg.com/vi/g-UIENPam98/0.jpg</thumbnail> <name>haha420000</name> <views>1919250</views> <rating>4.9313602</rating> <watch>https://www.youtube.com/watch?v=bE4OMnvFDOU&feature=youtube_gdata_player</watch> <grouptitle>Staind - Outside</grouptitle> <groupdiscription>Staind - Outside Lyrics Comment and Rate</groupdiscription> <thumbnail>http://i.ytimg.com/vi/bE4OMnvFDOU/0.jpg</thumbnail> <name>SgtFireEles</name> <views>2549818</views> <rating>4.85639</rating> <watch>https://www.youtube.com/watch?v=gDVBvrw4HEg&feature=youtube_gdata_player</watch> <grouptitle>Staind - Eyes Wide Open</grouptitle> <groupdiscription>"Eyes Wide Open" from the new self titled album out September 13, 2011.</groupdiscription> <thumbnail>http://i.ytimg.com/vi/gDVBvrw4HEg/0.jpg</thumbnail> <name>StaindVideos</name> <views>243720</views> <rating>4.9686413</rating> <watch>https://www.youtube.com/watch?v=O2JK78-goxQ&feature=youtube_gdata_player</watch> <grouptitle>Staind - All I Want (video)</grouptitle> <groupdiscription>© 2008 WMG All I Want (video)</groupdiscription> <thumbnail>http://i.ytimg.com/vi/O2JK78-goxQ/0.jpg</thumbnail> <name>StaindVideos</name> <views>895227</views> <rating>4.9608426</rating> <watch>https://www.youtube.com/watch?v=wjlnljxsxYY&feature=youtube_gdata_player</watch> <grouptitle>Staind-So far away</grouptitle> <groupdiscription>Staind Deviant Art</groupdiscription> <thumbnail>http://i.ytimg.com/vi/wjlnljxsxYY/0.jpg</thumbnail> <name>jenninaz</name> <views>2247469</views> <rating>4.954743</rating> <watch>https://www.youtube.com/watch?v=WnQJLU8tnsk&feature=youtube_gdata_player</watch> <grouptitle>Staind - Not Again [New Music] [Official Lyric Video]</grouptitle> <groupdiscription>© 2011 WMG. The first single from the new self-titled album coming 9/13/11! Click here to download the single: bit.ly/nG2zuy staind.com http twitter.com</groupdiscription> <thumbnail>http://i.ytimg.com/vi/WnQJLU8tnsk/0.jpg</thumbnail> <name>StaindVideos</name> <views>384998</views> <rating>4.921177</rating> <watch>https://www.youtube.com/watch?v=Cxpi2M38dAM&feature=youtube_gdata_player</watch> <grouptitle>Staind - Fade (Video)</grouptitle> <groupdiscription>© 2006 WMG Fade (Video)</groupdiscription> <thumbnail>http://i.ytimg.com/vi/Cxpi2M38dAM/0.jpg</thumbnail> <name>StaindVideos</name> <views>785104</views> <rating>4.9594793</rating> <watch>https://www.youtube.com/watch?v=KT33KXLQgZE&feature=youtube_gdata_player</watch> <grouptitle>Staind - Waste</grouptitle> <groupdiscription>Artist: Staind Song: Waste Album: Break the Cycle</groupdiscription> <thumbnail>http://i.ytimg.com/vi/KT33KXLQgZE/0.jpg</thumbnail> <name>DEADOFNIGHT666</name> <views>294972</views> <rating>4.9174457</rating> <watch>https://www.youtube.com/watch?v=xO-G7YatytY&feature=youtube_gdata_player</watch> <grouptitle>Staind - This Is It</grouptitle> <groupdiscription>Stainds new single that is available to buy on itunes. Yay Im first to put it on youtube. This song is incredible.</groupdiscription> <thumbnail>http://i.ytimg.com/vi/xO-G7YatytY/0.jpg</thumbnail> <name>DysfunctionalGrey</name> <views>770874</views> <rating>4.883614</rating> <watch>https://www.youtube.com/watch?v=qP-ZfBbMvFc&feature=youtube_gdata_player</watch> <grouptitle>Staind - Falling (video) Album Version audio</grouptitle> <groupdiscription>© 2006 WMG Falling (video) Album Version audio</groupdiscription> <thumbnail>http://i.ytimg.com/vi/qP-ZfBbMvFc/0.jpg</thumbnail> <name>StaindVideos</name> <views>475345</views> <rating>4.923939</rating> <watch>https://www.youtube.com/watch?v=ZRRnU8VzOok&feature=youtube_gdata_player</watch> <grouptitle>Staind - "So Far Away" (Acoustic in Yahoo Studios)</grouptitle> <groupdiscription>Staind performing "So Far Away" live @ Yahoo Studios</groupdiscription> <thumbnail>http://i.ytimg.com/vi/ZRRnU8VzOok/0.jpg</thumbnail> <name>sta1ndsoul</name> <views>3063754</views> <rating>4.9500628</rating> <watch>https://www.youtube.com/watch?v=pm9KBabovmc&feature=youtube_gdata_player</watch> <grouptitle>Epiphany-Staind</grouptitle> <groupdiscription>Epiphany-Staind Come on now, lets try and get the 1mil mark show your mates, post the link on your site or what ever, Thanks for watching</groupdiscription> <thumbnail>http://i.ytimg.com/vi/pm9KBabovmc/0.jpg</thumbnail> <name>SoDamnInsane91</name> <views>1175633</views> <rating>4.927681</rating> </youtube> </root> My problem is this line <watch> I need to remove and make it say this Any ideas how i can do that? $attrs = $media->group->player->attributes(); $watch = $attrs['url']; Quote Link to comment https://forums.phpfreaks.com/topic/248770-simple-search-to-list-youtube-results-help/#findComment-1277597 Share on other sites More sharing options...
jcbones Posted October 10, 2011 Share Posted October 10, 2011 Try using strrpos() to get the last amperstand. <?php $attrs = $media->group->player->attributes(); $watch = substr($attrs['url'],0,strrpos($attrs['url'],'&')); Quote Link to comment https://forums.phpfreaks.com/topic/248770-simple-search-to-list-youtube-results-help/#findComment-1277602 Share on other sites More sharing options...
QuickOldCar Posted October 10, 2011 Share Posted October 10, 2011 Take a look here at some youtube embed scripts I made and will see how can obtain the video id value. http://www.phpfreaks.com/forums/index.php?topic=342974.msg1619535#msg1619535 Quote Link to comment https://forums.phpfreaks.com/topic/248770-simple-search-to-list-youtube-results-help/#findComment-1277608 Share on other sites More sharing options...
Mancent Posted October 10, 2011 Author Share Posted October 10, 2011 Fantastic: works wonderful! Just awesome! If I can just get that facebook frineds list paging json working I will be able to get much more done.. I guess I just have to wait until It's solved! Quote Link to comment https://forums.phpfreaks.com/topic/248770-simple-search-to-list-youtube-results-help/#findComment-1277610 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.