Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [klasa] Filmy z youtube, ... z własnego profilu
bim2
post
Post #1





Grupa: Zarejestrowani
Postów: 1 873
Pomógł: 152
Dołączył: 9.04.2006
Skąd: Berlin

Ostrzeżenie: (0%)
-----


Kilka postów niżej poprawiona klasa
----------------------------------

Klasa łączy sie z youTube.com wchodzi w dany profil i pobiera filmy. Klase napisałem troszkę temu dla kolegi, a dziś robiąc porządki ją znalazłem więc zamieszczam.

  1. <?php
  2. class youTube
  3. {
  4. public $sNick;
  5. public $sUrl = 'http://pl.youtube.com/profile_videos?user=%s';
  6. public $sUrl_ = 'http://pl.youtube.com/profile_videos?user=%s&page=%s';
  7. private $page;
  8. private $sString;
  9. public $aMatches_;
  10. private $bRun=false;
  11. function __construct($sNick)
  12. {
  13. $this->sNick = $sNick;
  14.  
  15. }
  16. public function connect()
  17. {
  18. if(empty($this->page))
  19. {
  20. $sUrl = sprintf($this->sUrl, $this->sNick);
  21. $this->sString = @file_get_contents($sUrl);
  22. } else {
  23.  
  24. $sUrl = sprintf($this->sUrl_, $this->sNick, $this->page);
  25. $this->sString = @file_get_contents($sUrl);
  26. }
  27. }
  28. public function getVideos()
  29. {
  30. if($this->bRun)
  31. {
  32. return $this->aMatches;
  33. } else {
  34. $this->getVideos_();
  35. return $this->aMatches;
  36. }
  37. }
  38. public function randVideo()
  39. {
  40. $aMatches_ = $this->getVideos();
  41. $iKey = rand(0, count($aMatches_)-1);
  42. return $this->aMatches_[$iKey];
  43. }
  44. public function getVideos_()
  45. {
  46. $this->bRun = true;
  47. $this->connect();
  48. preg_match_all("#<!-- end vEntry -->(.*?)<!-- end vEntry -->#is", $this->sString, $aMatches); 
  49. foreach($aMatches[1] AS $sValue)
  50. {
  51. $aMatches__ = preg_replace('!.*watch?v=(.*?)".*!is', '$1', $sValue);
  52. $this->aMatches_[] = '<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/'.$aMatches__.'"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'.$aMatches__.'" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>';
  53. }
  54. $this->page = preg_replace("#.*?<a href=\"/profile_videos?user=".$this->sNick."&amp;p=r&amp;page=([0-9]*?)\" class=\"pagerNotCurrent\">Dalej</a>.*#is", '$1', $this->sString);
  55. if((int)$this->page>0)
  56. {
  57. $this->getVideos_();
  58. }
  59. }
  60. }
  61. ?>

I jak użyć:
  1. <?php
  2. $yt = new youTube($sNick);
  3. echo $yt->randVideo(); //losuje film
  4. print_r($yt->getAllVideos()); //zwraca wszystkie filmy
  5. ?>


Ten post edytował bim2 5.01.2008, 22:00:08


--------------------
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 22.08.2025 - 09:56