Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Uruchomienie kodu
Forum PHP.pl > Forum > Przedszkole
-CvB-
Witam, od jakiegoś czasu bawię się w PHP i od rana meczy mnie pewien kod który znalazłem w sieci.
Jednym zdaniem nie umiem wczytać tabeli który on generuje.
Oto kod:
  1. // usage: $table = spTibExperience::getExperienceTable(array('Antica', 'Pacera'));
  2. // if any element in $table has less than 300 entries then there was a pretty serious failure (since it automatically retries)
  3.  
  4. class spTibExperience {
  5. const PAGES = 11;
  6. const PERPAGE = 25;
  7. const MAXTIME = 18000; // keep trying for 5 hours, enough time for maintenance/update/etc. to run, in theory..
  8. const TIMEOUT = 30; // seconds
  9. const MAXBACKOFF = 600; // maximum "backoff" in case of failure
  10.  
  11. static function getExperienceTable($worlds = array()) {
  12. $start_t = time();
  13.  
  14. $table = array();
  15. $failcnt = 0;
  16. $output = array();
  17.  
  18. do {
  19. $failures = array();
  20.  
  21. if (!function_exists('curl_multi_init')) {
  22. error_log('spTibExperience requires the cURL PHP extension');
  23. return false;
  24. }
  25.  
  26. foreach ($worlds as $world) {
  27. $mh = curl_multi_init();
  28. $chs = array();
  29. if (!isset($output[$world])) $output[$world] = array();
  30.  
  31. for ($page = 0; $page <= self::PAGES; $page++) {
  32. if (isset($output[$world][$page]))
  33. continue;
  34.  
  35. $chs[$page] = curl_init();
  36. curl_setopt($chs[$page], CURLOPT_URL, $url = sprintf('http://www.tibia.com/community/?subtopic=highscores&world=%s&list=%s&page=%d', $world, 'experience', $page));
  37. curl_setopt($chs[$page], CURLOPT_RETURNTRANSFER, true);
  38. curl_setopt($chs[$page], CURLOPT_FRESH_CONNECT, true);
  39. curl_setopt($chs[$page], CURLOPT_TIMEOUT, self::TIMEOUT);
  40. curl_multi_add_handle($mh, $chs[$page]);
  41.  
  42. if (@$GLOBALS['debug']) { error_log("preparing to fetch $url"); }
  43. }
  44.  
  45. do {
  46. $mrc = curl_multi_exec($mh, $active);
  47. } while ($active);
  48.  
  49. for ($page = 0; $page <= self::PAGES; $page++) {
  50. if (isset($output[$world][$page]))
  51. continue;
  52.  
  53. $out = curl_multi_getcontent($chs[$page]);
  54.  
  55. curl_close($chs[$page]);
  56.  
  57. $failure = false;
  58.  
  59. if (preg_match_all('#<TD WIDTH=10%>(.*?)</TD>.*?subtopic=characters&name=.*?">(.*?)</A></TD><TD WIDTH=15%>(.*?)</TD><TD WIDTH=20%>(.*?)</TD></TR>#s', $out, $m)) {
  60. if (count($m[2]) != self::PERPAGE) {
  61. if (@$GLOBALS['debug']) { error_log("on $world page $page, found " . count($m[2]) . ", not " . self::PERPAGE); }
  62. $failure = true;
  63. }
  64. /*
  65.   else if (rand(1, 2) == 1) {
  66.   if (@$GLOBALS['debug']) { error_log("failing because i can"); }
  67.   $failure = true;
  68.   }
  69. */
  70. else {
  71. foreach ($m[2] as $k => $name) {
  72. $rank = $m[1][$k];
  73. $level = $m[3][$k];
  74. $xp = $m[4][$k];
  75.  
  76. if (!isset($table[$world])) $table[$world] = array();
  77. $table[$world][$rank] = array('name' => $name, 'level' => $level, 'xp' => $xp);
  78. }
  79.  
  80. $output[$world][$page] = true;
  81. }
  82. }
  83. else {
  84. $failure = true;
  85. }
  86.  
  87. if ($failure) {
  88. $now_t = time();
  89.  
  90. if (@$GLOBALS['debug']) { error_log("failure for $world page $page"); }
  91.  
  92. if ($now_t - $start_t >= self::MAXTIME)
  93. $output[$world][$page] = true;
  94. }
  95. }
  96.  
  97. curl_multi_close($mh);
  98.  
  99. if (count($output[$world]) <= self::PAGES)
  100. $failures[$world] = true;
  101. else {
  102. ksort($output[$world]);
  103. unset($failures[$world]);
  104. }
  105. }
  106.  
  107. if (count($failures)) {
  108. $failcnt++;
  109.  
  110. $backoff = min(self::MAXBACKOFF, pow(2, $failcnt));
  111.  
  112. // don't want pow(2, ...) to get too big!
  113. if (pow(2, $failcnt) > self::MAXBACKOFF)
  114. $failcnt--;
  115.  
  116. if (@$GLOBALS['debug']) { error_log("backing off for $backoff seconds. will keep trying for " . (self::MAXTIME - ($now_t - $start_t)) . " seconds"); }
  117. sleep($backoff);
  118. }
  119. else {
  120. foreach ($table as $world => $ranks)
  121. ksort($table[$world]);
  122.  
  123. ksort($table);
  124. break;
  125. }
  126. } while (1);
  127.  
  128. return $table;
  129. }
  130. }


Wiem, że na pączątku pisze:
  1. // usage: $table = spTibExperience::getExperienceTable(array('Antica', 'Pacera'));

lecz po wielu próbach np:
  1. echo $table;

  1. <?php
  2. require("spTibExperience.php");
  3. $table = spTibExperience::getExperienceTable(array('Antica'));
  4. foreach ( $table as $tables => $ilosc )
  5. {
  6. for ( $i = 0; $i < count($ilosc); $i++ )
  7. {
  8. echo $ilosc[$i];
  9. }
  10. }
  11. ?>

  1. <?php
  2. require("spTibExperience.php");
  3. $table = spTibExperience::getExperienceTable(array('Antica'));
  4. echo '<pre>';
  5. var_dump($table);
  6. echo '</pre>';
  7. echo count($table['Antica']);
  8. for($i=0;$i<=count($table['Antica']);$i++){
  9. echo $table['Antica'][$i]['name'];
  10. }
  11. ?>

Tylko ostani kod zwraca:
Cytat
bool(false)

0

Jeśli jest w stanie mi ktoś pomóc będę bardzo wdzięczny. ; )
btw. Kod powinien być ok. Przynajmniej rok temu na pewno działał, a strona z której skrypt pobiera dane jest taka sama.
CvB
Problem rozwiązałem....
Nie miałem włączonej biblioteki
Cytat
extension=php_curl.dll
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.