Agregando conhecimento…
Esta é uma função do PHP que retorna um vídeo do YouTube ou MetaCafe. E uma outra função que retorna a imagem miniatura do vídeo do YouTube (120×90 px)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | function embedVideo($url,$width,$height){ /* * RETORNA VIDEOS DO YOUTUBE E METACAFE * * É POSSÍVEL IMPLEMENTAR MAIS EXPRESSÕES REGULARES * * é possível adaptar um retorno em string também, * aí fica a critério de quem usar a função * */ if(preg_match("#http://(.*)\.youtube\.com/watch\?v=(.*)(&(.*))?#", $url, $matches)){ echo ' <object width="'.$width.'" height="'.$height.'"> <param name="movie" value="http://www.youtube.com/v/'.$matches[2].'&hl=pt-br&fs=1"></param> <param name="allowFullScreen" value="true"></param> <param name="allowscriptaccess" value="always"></param> <embed src="http://www.youtube.com/v/'.$matches[2].'&hl=pt-br&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'.$width.'" height="'.$height.'"></embed> </object> '; }elseif(preg_match("#http://www\.metacafe\.com/watch/(([^/].*)/([^/].*))/?#", $url, $matches)){ echo '<embed flashVars="playerVars=showStats=no|autoPlay=no|videoTitle=" src="http://www.metacafe.com/fplayer/'.$matches[1].'.swf" width="'.$width.'" height="'.$height.'" wmode="transparent" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed>'; } } $youtubeVideo1 = 'http://in.youtube.com/watch?v=Km7PcdMzaN4'; $youtubeVideo2 = 'http://in.youtube.com/watch?v=xNi7QwAL3XY&feature=dir'; $metacafeVideo1 = 'http://www.metacafe.com/watch/2215104/amazing_video/'; $metacafeVideo2 = 'http://www.metacafe.com/watch/2204556/sensational_cars_of_the_future/'; embedVideo($youtubeVideo1,425,344); embedVideo($youtubeVideo2,425,344); embedVideo($metacafeVideo1,400,348); embedVideo($metacafeVideo2,400,348); function youtubeImage($url,$tipo='src') { $img = ''; // O TAMANHO PADRAO DA IMAGEM DO YOUTUBE É 120x90 if(preg_match("#http://(.*)\.youtube\.com/watch\?v=(.*)(&(.*))?#", $url, $matches)){ if(isset($matches[2]) && $matches[2]!=''){ $img = 'http://s2.ytimg.com/vi/'.$matches[2].'/default.jpg'; } } return $tipo=='src' ? $img : '<img src="'.$img.'" />'; } echo youtubeImage($youtubeVideo1,'img'); |
Página para publicar e armazenar conteúdo relacionado a Desenvolvimento Web:
PHP, MySQL, Javascript, JQuery, CSS e as novas tecnologias do momento.
Façam comentários, perguntas e sugestões que ficarei satisfeito por recebê-los.
Shiguenori Junior
Sergio
fevereiro 18th, 2010 at 16:32
Ow, a URL ta antiga, substitua por essa:
http://img.youtube.com/vi/, e outra, tem jeito de arrumar essa expressão regular pra tirar o &feature=popular ??
Vlw, flw
Junior
fevereiro 19th, 2010 at 18:18
@Sergio não sou muito bom com expressões regulares, mas você pode usar:
Qualquer coisa manda um comentário
After Death
outubro 13th, 2010 at 11:52
Embedar video ae!!
Vinicius Almeida
janeiro 5th, 2011 at 14:16
Muito boas as funções!
Eu fiz uma modificação, agora as imagens funcionam mesmo com o &featured. Vai servir pro @Sergio ai em cima tbm:
Junior
janeiro 15th, 2011 at 9:45
@Vinicius Almeida obrigado pela dica. Uma observação no echo da função embedVideo() está em branco, provavelmente o wordpress removeu o conteúdo html, mas acredito que seja o mesmo código
Luis Henrique
junho 20th, 2012 at 11:30
Muito bom cara, ajudou bastante.
Lucas
agosto 11th, 2012 at 1:52
Legal cara! Esse post http://blog.glaucocustodio.com/2012/08/11/cadastro-de-videos-do-youtube-com-php/ complementa o seu..
Juninho
setembro 17th, 2012 at 13:46
Tem como encinar a enviar o codigo para o banco de dados ? grato desde já !
Junior
outubro 15th, 2012 at 17:51
@Juninho dá uma olhada nesse tutorial, veja as partes que falam sobre: Conexão ao Banco, Seleção do Banco e Inserção;
Qualquer dúvida é só falar que eu tento explicar melhor.