NightZ Posted October 20, 2009 Share Posted October 20, 2009 I have a script on a site that pulls in you-tube videos, The problem is, that the thumb nails for the site, no longer show up. I have tried to add codes to remove the line causing the problem but this fails me. The code is this if anyone has a way to fix it. <?php function yt($ytsearch) { global $ytcurrenttag; global $ytvids; global $ytid; $ytvids = array(); $ytid=0; function ytinsidethetag($ytparser, $ytdata){ global $ytcurrenttag; global $ytvids; global $ytid; if(!strcmp(strtolower($ytcurrenttag),"title")) { $ytvids[$ytid]['type'] = "yt"; $ytvids[$ytid]['title']=$ytdata; $ytvids[$ytid]['title'][0] = ucwords($ytvids[$ytid]['title'][0]); } } function ytopentag($ytparser, $ytdata, $ytattr=''){ global $ytcurrenttag; $ytcurrenttag = $ytdata; global $ytvids; global $ytid; if(!strcmp(strtolower($ytcurrenttag),"media:player")) { $ytvids[$ytid]['link']=str_replace("watch?v=","v/",$ytattr['URL']); $ytvids[$ytid]['thumb']=str_replace("http://www.youtube.com/watch?v=","http://img.youtube.com/vi/",$ytvids[$ytid]['link']); $ytvids[$ytid]['thumb'].="/2.jpg"; } } function ytclosetag($ytparser, $ytdata){ global $ytid; if(!strcmp(strtolower($ytdata),"entry")) $ytid++; } $ytxml_parser = xml_parser_create(); xml_set_element_handler($ytxml_parser, "ytopentag", "ytclosetag"); xml_set_character_data_handler($ytxml_parser, "ytinsidethetag"); include("fetch.php"); $ytdata = fetchrssyt("http://gdata.youtube.com/feeds/videos?vq=$ytsearch&start-index=1&max-results=40"); if(!(xml_parse($ytxml_parser, $ytdata))) die("Error on line " . xml_get_current_line_number($ytxml_parser)); xml_parser_free($ytxml_parser); return $ytvids; } ?> The code in the browser for the thumb comes up. http://img.youtube.com/vi/lOgQyIMX_XU&feature=youtube_gdata/2.jpg and i think for it to work i need. http://img.youtube.com/vi/lOgQyIMX_XU/2.jpg lOgQyIMX_XU = whatever video needs to be displayed. Someone said to try this. $ytid = str_replace('feature=youtube_gdata', '',$ytid); $ytid = str_replace('&', '',$ytid); $ytid = str_replace('amp;', '',$ytid); but it did nothing for the problem Thanks Quote Link to comment Share on other sites More sharing options...
dreamwest Posted October 20, 2009 Share Posted October 20, 2009 Use default jpg http://i4.ytimg.com/vi/{$id_here}/default.jpg Quote Link to comment Share on other sites More sharing options...
NightZ Posted October 21, 2009 Author Share Posted October 21, 2009 Tried using the default,and still no fix. Quote Link to comment Share on other sites More sharing options...
dreamwest Posted October 21, 2009 Share Posted October 21, 2009 If you have the id you have the image. so url: http://www.youtube.com/watch?v=nJ6rDVKzuTM will have thumb: http://i4.ytimg.com/vi/nJ6rDVKzuTM/default.jpg Quote Link to comment Share on other sites More sharing options...
NightZ Posted October 21, 2009 Author Share Posted October 21, 2009 Yes i can get a single image, but having one image for all the videos ,is not much good. i need for the thumbs for each video to appear. They have to show up automatically on the search page,depending on the persons video choice. Quote Link to comment Share on other sites More sharing options...
NightZ Posted October 22, 2009 Author Share Posted October 22, 2009 I can get a single image to show up for all the videos, by using your way. This is what i have used up to now. but this is still not right. Quote Link to comment Share on other sites More sharing options...
barrywood Posted October 22, 2009 Share Posted October 22, 2009 I'm able to get each thumbnail to show up by using this code: http://img.youtube.com/vi/'.$rec['youtube_Link'].'/default.jpg You can see it in action on the site I did for EBow. Quote Link to comment Share on other sites More sharing options...
dreamwest Posted October 22, 2009 Share Posted October 22, 2009 I can get a single image to show up for all the videos, by using your way. This is what i have used up to now. but this is still not right. You just need to loop the xml results. If you look at this url feed: http://gdata.youtube.com/feeds/videos?vq=funny You will get the source, now just extract what you want with pregmatch: <media:thumbnail url="http://i.ytimg.com/vi/IytNBm8WA1c/2.jpg" height="90" width="120" time="00:03:54.500"/> $src = file_get_contents("http://gdata.youtube.com/feeds/videos?vq=funny"); preg_match_all('~thumbnail url\s?=\s?[\'"](.*?).jpg[\'"]~is', $src, $match); foreach ($match[1] as $tmb) { echo "<img src='{$tmb}.jpg' height='90' width='120'><br>"; } Simple Quote Link to comment Share on other sites More sharing options...
dreamwest Posted October 22, 2009 Share Posted October 22, 2009 Ive revised this, this will give you title, player and thumb $src = file_get_contents("http://gdata.youtube.com/feeds/videos?vq=Gitano%20corazon&start-index=1&max-results=10"); preg_match_all('~<media:player url\s?=\s?[\'"](.*?)</media:title>~is', $src, $match); foreach ($match[1] as $stuff) { $tmb = explode('watch?v=',$stuff); $tmb = explode('&feature',$tmb[1]); $tmb = $tmb[0]; $title = explode("<media:title type='plain'>",$stuff); $title = $title[1]; $play = explode("'/><media:thumbnail",$stuff); $play = $play[0]; echo "<img src='http://i.ytimg.com/vi/{$tmb}/2.jpg' height='90' width='120'><br>Title: {$title}<br>Player: {$play}<br>"; } Quote Link to comment Share on other sites More sharing options...
NightZ Posted October 22, 2009 Author Share Posted October 22, 2009 This code will not work for this site. And many thanks for your help. Here is the site problem, http://holybiblez.com/music/video/index.php?search=An%20Ending%20-%20BRIAN%20ENO&source=all Quote Link to comment Share on other sites More sharing options...
NightZ Posted October 22, 2009 Author Share Posted October 22, 2009 If i change this code part from this. if(!strcmp(strtolower($ytcurrenttag),"media:player")) { $ytvids[$ytid]['link']=str_replace("watch?v=","v/",$ytattr['URL']); $ytvids[$ytid]['thumb']=str_replace("http://www.youtube.com/watch?v=","http://img.youtube.com/vi/",$ytvids[$ytid]['link']); $ytvids[$ytid]['thumb'].="/2.jpg"; } } to this. if(!strcmp(strtolower($ytcurrenttag),"media:player")) { $ytvids[$ytid]['link']=str_replace("watch?v=","v/",$ytattr['URL']); $ytvids[$ytid]['thumb']=str_replace("http://www.youtube.com/watch?v=","http://img.youtube.com/vi/",$ytvids[$ytid]['thumb']); $ytvids[$ytid]['thumb'].="http://i4.ytimg.com/vi/KLpFnw-zFX4/default.jpg"; } } I get the thumb nails for all the videos,but they are all the same picture.(for every search result) Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 22, 2009 Share Posted October 22, 2009 PHP already knows how to parse XML: <?php $xml = new SimpleXMLElement('http://gdata.youtube.com/feeds/videos?vq=Gitano%20corazon&start-index=1&max-results=10', null, true); $xml->registerXPathNamespace('m', 'http://search.yahoo.com/mrss/'); foreach ($xml->xpath('//m:player') as $player) { parse_str(parse_url($player['url'], PHP_URL_QUERY), $query); $thumbnail = 'http://i.ytimg.com/vi/' . $query['v'] . '/2.jpg'; echo '<img src="' . $thumbnail . '">'; } Quote Link to comment Share on other sites More sharing options...
NightZ Posted October 23, 2009 Author Share Posted October 23, 2009 This code doesn't work right, it is bringing in, a line of thumbnails across the top.(only the thumbs from the rss page show) and are fixed in place. I need the main code i'm using to be the same. if(!strcmp(strtolower($ytcurrenttag),"media:player")) { $ytvids[$ytid]['link']=str_replace("watch?v=","v/",$ytattr['URL']); $ytvids[$ytid]['thumb']=str_replace("http://www.youtube.com/watch?v=","http://img.youtube.com/vi/",$ytvids[$ytid]['link']); $ytvids[$ytid]['thumb'].="/2.jpg"; } } As the thumbs try to load, they are adding in address-bar the line. &feature=youtube_gdata This is the problem,if i take this out from the address-bar,the thumb appears. (also can you tell me, where to place the codes that you give.Am i meant to just add them within my original code or is it to replace my code) Quote Link to comment Share on other sites More sharing options...
NightZ Posted October 24, 2009 Author Share Posted October 24, 2009 If it was working before,why has it stopped,nothing at my end was altered. Can someone work a code ,into my one. Thanks Quote Link to comment Share on other sites More sharing options...
NightZ Posted November 2, 2009 Author Share Posted November 2, 2009 Still not working,there must be a simple answer to this. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted November 2, 2009 Share Posted November 2, 2009 What is wrong with the snippet I provided you with? http://www.phpfreaks.com/forums/index.php/topic,273565.msg1293765.html#msg1293765 Quote Link to comment Share on other sites More sharing options...
NightZ Posted November 2, 2009 Author Share Posted November 2, 2009 The code you gave me doesn't works with the site, Here is the site error. http://holybiblez.com/music/video/index.php?search=meet%20joe%20black&source=all Your code brings in a line of thumbnails,across the top but not in the search boxes. (im php retarded as well) Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted November 2, 2009 Share Posted November 2, 2009 Well, it's obviously not plug and play. If you have this: <?php $xml = new SimpleXMLElement('http://gdata.youtube.com/feeds/videos?vq=Gitano%20corazon&start-index=1&max-results=10', null, true); $xml->registerXPathNamespace('m', 'http://search.yahoo.com/mrss/'); $videos = array(); foreach ($xml->xpath('//m:player') as $player) { parse_str(parse_url($player['url'], PHP_URL_QUERY), $query); $videos[] = array( 'url' => (string) $player['url'], 'thumbnail' => 'http://i.ytimg.com/vi/' . $query['v'] . '/2.jpg', ); } print_r($videos); You'll get something like this: Array ( [0] => Array ( [url] => http://www.youtube.com/watch?v=Fi679_RsBGM&feature=youtube_gdata [thumbnail] => http://i.ytimg.com/vi/Fi679_RsBGM/2.jpg ) [1] => Array ( [url] => http://www.youtube.com/watch?v=S2afjJjeHhk&feature=youtube_gdata [thumbnail] => http://i.ytimg.com/vi/S2afjJjeHhk/2.jpg ) [2] => Array ( [url] => http://www.youtube.com/watch?v=CEX8kG6DRGA&feature=youtube_gdata [thumbnail] => http://i.ytimg.com/vi/CEX8kG6DRGA/2.jpg ) [3] => Array ( [url] => http://www.youtube.com/watch?v=bfg4d8_xJes&feature=youtube_gdata [thumbnail] => http://i.ytimg.com/vi/bfg4d8_xJes/2.jpg ) [4] => Array ( [url] => http://www.youtube.com/watch?v=voYyPpTFSdM&feature=youtube_gdata [thumbnail] => http://i.ytimg.com/vi/voYyPpTFSdM/2.jpg ) [5] => Array ( [url] => http://www.youtube.com/watch?v=ht79Rq4dQKo&feature=youtube_gdata [thumbnail] => http://i.ytimg.com/vi/ht79Rq4dQKo/2.jpg ) [6] => Array ( [url] => http://www.youtube.com/watch?v=K2JV3f52p3E&feature=youtube_gdata [thumbnail] => http://i.ytimg.com/vi/K2JV3f52p3E/2.jpg ) [7] => Array ( [url] => http://www.youtube.com/watch?v=brK9xMdziac&feature=youtube_gdata [thumbnail] => http://i.ytimg.com/vi/brK9xMdziac/2.jpg ) [8] => Array ( [url] => http://www.youtube.com/watch?v=P1jBKFKyMJA&feature=youtube_gdata [thumbnail] => http://i.ytimg.com/vi/P1jBKFKyMJA/2.jpg ) [9] => Array ( [url] => http://www.youtube.com/watch?v=ZzT69vbtPSc&feature=youtube_gdata [thumbnail] => http://i.ytimg.com/vi/ZzT69vbtPSc/2.jpg ) ) Quote Link to comment Share on other sites More sharing options...
NightZ Posted November 2, 2009 Author Share Posted November 2, 2009 This is the main page code. <?php function yt($ytsearch) { global $ytcurrenttag; global $ytvids; global $ytid; $ytvids = array(); $ytid=0; function ytinsidethetag($ytparser, $ytdata){ global $ytcurrenttag; global $ytvids; global $ytid; if(!strcmp(strtolower($ytcurrenttag),"title")) { $ytvids[$ytid]['type'] = "yt"; $ytvids[$ytid]['title']=$ytdata; $ytvids[$ytid]['title'][0] = ucwords($ytvids[$ytid]['title'][0]); } } function ytopentag($ytparser, $ytdata, $ytattr=''){ global $ytcurrenttag; $ytcurrenttag = $ytdata; global $ytvids; global $ytid; if(!strcmp(strtolower($ytcurrenttag),"media:player")) { $ytvids[$ytid]['link']=str_replace("watch?v=","v/",$ytattr['URL']); $ytvids[$ytid]['thumb']=str_replace("http://www.youtube.com/watch?v=","http://img.youtube.com/vi/",$ytvids[$ytid]['link']); $ytvids[$ytid]['thumb'].="/2.jpg"; } } function ytclosetag($ytparser, $ytdata){ global $ytid; if(!strcmp(strtolower($ytdata),"entry")) $ytid++; } $ytxml_parser = xml_parser_create(); xml_set_element_handler($ytxml_parser, "ytopentag", "ytclosetag"); xml_set_character_data_handler($ytxml_parser, "ytinsidethetag"); include("fetch.php"); $ytdata = fetchrssyt("http://gdata.youtube.com/feeds/videos?vq=$ytsearch&start-index=1&max-results=40"); if(!(xml_parse($ytxml_parser, $ytdata))) die("Error on line " . xml_get_current_line_number($ytxml_parser)); xml_parser_free($ytxml_parser); return $ytvids; } ?> Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted November 2, 2009 Share Posted November 2, 2009 Right, so just replace that with: function yt($search) { $xml = new SimpleXMLElement('http://gdata.youtube.com/feeds/videos?vq=' . urlencode($search) . '&start-index=1&max-results=40', null, true); $xml->registerXPathNamespace('m', 'http://search.yahoo.com/mrss/'); $videos = array(); foreach ($xml->xpath('//m:player') as $player) { parse_str(parse_url($player['url'], PHP_URL_QUERY), $query); $videos[] = array( 'link' => (string) $player['url'], 'thumb' => 'http://i.ytimg.com/vi/' . $query['v'] . '/2.jpg', ); } return $videos; } Then yt('test') will give you an array of links to videos and their corresponding thumbnails when searching for "test". Quote Link to comment Share on other sites More sharing options...
NightZ Posted November 2, 2009 Author Share Posted November 2, 2009 I get no videos from this. http://holybiblez.com/music/video/index.php?search=coldplay&source=all This is what i get. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted November 2, 2009 Share Posted November 2, 2009 Show your code using that function. Quote Link to comment Share on other sites More sharing options...
NightZ Posted November 2, 2009 Author Share Posted November 2, 2009 <?php function yt($search) { $xml = new SimpleXMLElement('http://gdata.youtube.com/feeds/videos?vq=' . urlencode($search) . '&start-index=1&max-results=40', null, true); $xml->registerXPathNamespace('m', 'http://search.yahoo.com/mrss/'); $videos = array(); foreach ($xml->xpath('//m:player') as $player) { parse_str(parse_url($player['url'], PHP_URL_QUERY), $query); $videos[] = array( 'link' => (string) $player['url'], 'thumb' => 'http://i.ytimg.com/vi/' . $query['v'] . '/2.jpg', ); } return $videos; } ?> Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted November 2, 2009 Share Posted November 2, 2009 Yeah I know how that code looks, because I wrote it myself. I want to see the code that is using/calling the yt() function. Quote Link to comment Share on other sites More sharing options...
NightZ Posted November 2, 2009 Author Share Posted November 2, 2009 Do you mean the fetch php? That is. <? function fetchrssyt($rss){ if (!function_exists('curl_init')) die('please install cURL'); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $rss); curl_setopt($curl, CURLOPT_REFERER, "http://localhost/"); curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0;Windows NT 5.1)"); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 30); $output = curl_exec($curl); curl_close($curl); return $output; } ?> or do you mean,the bottom of my code. include("fetch.php"); $ytdata = fetchrssyt("http://gdata.youtube.com/feeds/videos?vq=$ytsearch&start-index=1&max-results=40"); if(!(xml_parse($ytxml_parser, $ytdata))) die("Error on line " . xml_get_current_line_number($ytxml_parser)); xml_parser_free($ytxml_parser); return $ytvids; } no too sure which it is, Quote Link to comment 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.