Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] Pobieranie danych z RSS - optymalizacja
Forum PHP.pl > Forum > Przedszkole
in5ane
Elo, pomógłby mi ktoś zoptymalizować poniższy kod? Chodzi o to, że on pobiera dane z RSS i je wyświetla, tylko problem w tym, że tak obciążał serwer, że usługodawca kazał mi się wynieść z serwera.

Kod:
  1. <?php
  2. $site = "http://www.ttrworldtour.com/ranking/mens-ranking-list.html?type=116&tx_txresults_pi1%5BfeedId%5D=53";
  3. $fp = @fopen($site,"r");
  4. while(!feof($fp)) $raw .= @fgets($fp, 196);
  5. fclose($fp);
  6.    echo '<p style="margin: 0 15px 0 15px;"><a href="http://www.ttrworldtour.com/ranking/mens-ranking-list.html" target="_top"><strong>TTR TOP 5 MEN</strong></a></p>';
  7. if( eregi("<item>(.*)</item>", $raw, $rawitems ) ) {
  8. $items = explode("<item>", $rawitems[0]);
  9.    
  10. for( $i = 0; $i < count($items)-1; $i++ ) {
  11.  eregi("<title>(.*)</title>",$items[$i+1], $title );
  12.  eregi("<link>(.*)</link>",$items[$i+1], $link );
  13.  eregi("<categorie>(.*)</categorie>",$items[$i+1], $cat);
  14.  echo "<p style=\"margin: 0 15px 0 15px;\"><a href='".$link[1]."' target='_top'>".$title[1]."</a> </p>".$cat[1];
  15. }
  16. }
  17.  
  18. echo '<br /><br />';
  19.  
  20. $sites = "http://www.ttrworldtour.com/ranking/womens-ranking-list.html?type=117&tx_txresults_pi1%5BfeedId%5D=53";
  21. $fps = @fopen($sites,"r");
  22. while(!feof($fps)) $raws .= @fgets($fps, 196);
  23. fclose($fps);
  24.    echo '<p style="margin: 0 15px 0 15px;"><a href="http://www.ttrworldtour.com/ranking/womens-ranking-list.html" target="_top"><strong>TTR TOP 5 WOMEN</strong></a></p>';
  25. if( eregi("<item>(.*)</item>", $raws, $rawitemss ) ) {
  26. $itemss = explode("<item>", $rawitemss[0]);
  27.  
  28. for( $i = 0; $i < count($itemss)-1; $i++ ) {
  29.  eregi("<title>(.*)</title>",$itemss[$i+1], $titles );
  30.  eregi("<link>(.*)</link>",$itemss[$i+1], $links );
  31.  eregi("<categorie>(.*)</categorie>",$itemss[$i+1], $cats);
  32.  echo "<p style=\"margin: 0 15px 0 15px;\"><a href='".$links[1]."' target='_top'>".$titles[1]."</a> </p>".$cats[1];
  33. }
  34. }
  35. ?>


Z góry dziękuję za pomoc.
gox
  1. <?php
  2. $site = "http://www.ttrworldtour.com/ranking/mens-ranking-list.html?type=116&tx_txresults_pi1%5BfeedId%5D=53";
  3. $data = @file_get_contents($site) or die('blad polaczenia');
  4. ?>
  5. <p style="margin: 0 15px 0 15px;"><a href="http://www.ttrworldtour.com/ranking/mens-ranking-list.html" target="_top"><strong>TTR TOP 5 MEN</strong></a></p>
  6. <?php
  7.    preg_match_all('#<item>(.*?)</item>#', $data, $items);
  8.    $items = $items[0];
  9.    $output = array();
  10.    $count = count($items);
  11.    <?php for($i=0; $i<$items; $i++): ?>
  12.    <?php
  13.        preg_match('#<title>(.*?)</title>#', $items[$i], $title);
  14.        $output[$i]['title'] = $title[1];
  15.        
  16.        preg_match('#<link>(.*?)</link>#', $items[$i], $link);
  17.        $output[$i]['link'] = $link[1];
  18.        
  19.        preg_match('#<categorie>(.*?)</<categorie>#', $items[$i], $categorie);
  20.        $output[$i]['categorie'] = $categorie[1];
  21.    ?>
  22.    <p style="margin: 0 15px 0 15px;"><a href="<?php echo $link[1];?>" target='_top'><?php echo $title[1];?></a></p><?php echo $categorie[1];?>
  23.    <?php endfor;?>
  24.        
  25.        <?
  26.            // teraz robisz sobie file_put_contents('db.txt', serialize($output));
  27.            // i zamiast laczyc sie co z zew. serwerem co chwila, robisz to np. raz na 5 minut
  28.            // a przez reszte czasu czytasz dane z pliku 'db.txt'
  29.            
  30.        ?>
  31.        
  32.        <?
  33.            
  34.            // drugiej czesci skryptu nie podaje bo jest praktycznie identyczna
  35.        
  36.        ?>
Noddi
Lepiej skorzystać na przykład z simplexml" title="Zobacz w manualu PHP" target="_manual, prościej i wygodniej:

  1. <?php
  2.  
  3. $xml = @simplexml_load_file("http://www.ttrworldtour.com/ranking/mens-ranking-list.html?type=116&tx_txresults_pi1%5BfeedId%5D=53");
  4.  
  5. foreach ($xml as $channel)
  6. {
  7.    foreach($channel as $item)
  8.    {
  9.        echo $item->title;
  10.    }
  11. }
  12.  
  13. ?>
gox
Scigamy sie? smile.gif
gox
The SimpleXML extension requires PHP 5.
in5ane
@gox:

Mógłbyś mi to objaśnić?
Kod
       <?
           // teraz robisz sobie file_put_contents('db.txt', serialize($output));
           // i zamiast laczyc sie co z zew. serwerem co chwila, robisz to np. raz na 5 minut
           // a przez reszte czasu czytasz dane z pliku 'db.txt'
          
       ?>

Raczej nigdy nic nie robiłem w czytaniu/zapisywaniu itp. z plikami.

P.S.: I na jakiej niby to zasadzie? No to każdemu skrypt będzie albo najpierw zapisywał i dopiero czytał, albo zawsze czytał. Nie rozumiem tego w ogóle.
gox
Ostrzegam ze kodzik nie jest zdebugowany tongue.gif

  1. <?php
  2. $site = "http://www.ttrworldtour.com/ranking/mens-ranking-list.html?type=116&tx_txresults_pi1%5BfeedId%5D=53";
  3. $dbpath = './db/';
  4. ?>
  5. <p style="margin: 0 15px 0 15px;"><a href="http://www.ttrworldtour.com/ranking/mens-ranking-list.html" target="_top"><strong>TTR TOP 5 MEN</strong></a></p>
  6. <?php
  7.    if ( ! is_dir($db_path))
  8.        @mkdir($dbpath, 0777) or die('nie moge utworzyc bazy danych, zrob to recznie tongue.gif');
  9.    
  10.    list($dbfile) = glob("{$dbpath}*");
  11.    
  12.    if ( is_file($dbfile) AND intval($dbfile) > ( time() - 60) ) {
  13.        $output = @unserialize(@file_get_contents($dbfile)) or die('nie moge odczytac pliq');
  14.    }
  15.    else {
  16.        preg_match_all('#<item>(.*?)</item>#', $data, $items);
  17.        $items = $items[0];
  18.        $output = array();
  19.        $count = count($items);
  20.        for($i=0; $i<$items; $i++) {
  21.            preg_match('#<title>(.*?)</title>#', $items[$i], $title);
  22.            $output[$i]['title'] = $title[1];
  23.            preg_match('#<link>(.*?)</link>#', $items[$i], $link);
  24.            $output[$i]['link'] = $link[1];
  25.            preg_match('#<categorie>(.*?)</<categorie>#', $items[$i], $categorie);
  26.            $output[$i]['categorie'] = $categorie[1];
  27.        }
  28.        @unlink($dbfile) or die('nie moge usunac pliq');
  29.        @file_put_contents($dbpath.time(), serialize($output)) or die('nie moge zapisac pliq');
  30.    }
  31.    
  32.    <?php foreach ( $output as $o )
  33.        <p style="margin: 0 15px 0 15px;"><a href="<?php echo $o['link'];?>" target='_top'><?php echo $o['title'];?></a></p><?php echo $o['categorie'];?>
  34.    <?php endforeach; ?>
bim2
http://www.ajaxray.com/blog/downloads/feedparser
i przykład:
  1. <?php
  2. include('FeedParser.php');
  3. $Parser     = new FeedParser();
  4.  
  5. $Parser->parse('http://www.sitepoint.com/rss.php');
  6.  
  7. $channels      = $Parser->getChannels();    
  8. $items         = $Parser->getItems();        
  9. ?>
  10.  
  11. <html xmlns="http://www.w3.org/1999/xhtml">
  12. <head>
  13.    <title>Testing the PHP Universal Feed Parser</title>
  14.    <style type="text/css">
  15.    body{
  16.        padding: 0px;
  17.        margin: 50px 150px;
  18.        border: 1px solid #ddd;
  19.        font-family: verdana, arial;
  20.    }
  21.    
  22.    h1#title {
  23.        background-color: #eee;
  24.        border-bottom : 1px solid #ddd;
  25.        margin: 0px 0px 15px;
  26.        padding:10px;
  27.        text-align: center;
  28.    }
  29.    h1#title a{
  30.        font-size: 18px;
  31.    }
  32.    
  33.    p, a{
  34.        padding: 5px;
  35.        margin: 5px;
  36.        font-size: 12px;
  37.    }
  38.    p#description{
  39.        font-size: 13px;
  40.        font-weight: bold;
  41.    }
  42.    
  43.    p.feed-description{
  44.        border-bottom : 1px solid #ddd;
  45.    }
  46.    
  47.    p#info{
  48.        background-color:#EDF001;
  49.        color: 1px solid #f6afab;
  50.        padding: 5px;
  51.        text-align: center;
  52.        color: #444;
  53.    }
  54.    </style>
  55. </head>
  56. <body>
  57.    <h1 id="title"><a href="<?php echo $channels['LINK']; ?>"><?php echo $channels['TITLE']; ?></a></h1>
  58.    <p id="description"><?php echo $channels['DESCRIPTION']; ?> </p>
  59.    
  60.    <?php foreach($items as $item): ?>
  61.        <a class="feed-title" href="<?php echo $item['LINK']; ?>"><?php echo $item['TITLE']; ?></a>
  62.        <p class="feed-description"><?php echo $item['DESCRIPTION']; ?></p>
  63.    <?php endforeach;?>
  64.    
  65.    <p id='info'>
  66.        This page is an example of using
  67.        <a href="http://www.ajaxray.com/blog/2008/05/02/php-universal-feed-parser-lightweight-php-class-for-parsing-rss-and-atom-feeds/">
  68.            PHP Universal Feed Parser
  69.        </a>.
  70.    </p>
  71. </body>
  72. </html>
gox
XML Parser, CURL
to wszystki wyglada jadnie jak sie czyta kod,
jest tylko jeden problem
taki skrypt jest kilka-kilkanascie-KILKADZIESIAT razy wolniejszy
niz inny wykonany na nizszych funkcjach,
jesli na prawde mowimy o optymalizacji, to jest to zla droga,
Nie idzie ta droga! smile.gif
in5ane
@gox: zdebugowałem ten kod, no ale wyskakuje jeszcze error o takiej treści:
Kod
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 45 bytes) in ... on line 26


  1. <?php
  2. $start=microtime();
  3. $site = "http://www.ttrworldtour.com/ranking/mens-ranking-list.html?type=116&tx_txresults_pi1%5BfeedId%5D=53";
  4. $dbpath = './db/';
  5. ?>
  6. <p style="margin: 0 15px 0 15px;"><a href="http://www.ttrworldtour.com/ranking/mens-ranking-list.html" target="_top"><strong>TTR TOP 5 MEN</strong></a></p>
  7. <?php
  8.   if ( ! is_dir($db_path))
  9.       @mkdir($dbpath, 0777) or die('nie moge utworzyc bazy danych, zrob to recznie tongue.gif');
  10.  
  11.   list($dbfile) = glob("{$dbpath}*");
  12.  
  13.   if ( is_file($dbfile) AND intval($dbfile) > ( time() - 60) ) {
  14.       $output = @unserialize(@file_get_contents($dbfile)) or die('nie moge odczytac pliq');
  15.   } else {
  16.       preg_match_all('#<item>(.*?)</item>#', $data, $items);
  17.       $items = $items[0];
  18.       $output = array();
  19.       $count = count($items);
  20.       for($i=0; $i<$items; $i++) {
  21.           preg_match('#<title>(.*?)</title>#', $items[$i], $title);
  22.           $output[$i]['title'] = $title[1];
  23.           preg_match('#<link>(.*?)</link>#', $items[$i], $link);
  24.           $output[$i]['link'] = $link[1];
  25.           preg_match('#<categorie>(.*?)</<categorie>#', $items[$i], $categorie);
  26.           $output[$i]['categorie'] = $categorie[1];
  27.       }
  28.       @unlink($dbfile) or die('nie moge usunac pliq');
  29.       @file_put_contents($dbpath.time(), serialize($output)) or die('nie moge zapisac pliq');
  30.   }
  31. ?>
  32.  
  33.   <?php foreach ($output as $o) { ?>
  34.       <p style="margin: 0 15px 0 15px;"><a href="<?php echo $o['link']; ?>" target='_top'><?php echo $o['title']; ?></a></p><?php echo $o['categorie']; ?>
  35.   <?php } ?>
  36.   <br /><br /><br /><br />
  37. <?php
  38. echo round(microtime()-$start, 6);
  39. ?>
gox
cos kiepsciutensko zdebugowales w takim razie :0

  1. <?php
  2. $start=microtime();
  3. $site = "http://www.ttrworldtour.com/ranking/mens-ranking-list.html?type=116&tx_txresults_pi1%5BfeedId%5D=53";
  4. $dbpath = './db/';
  5. $storing_time = 10;
  6. ?>
  7. <p style="margin: 0 15px 0 15px;"><a href="http://www.ttrworldtour.com/ranking/mens-ranking-list.html" target="_top"><strong>TTR TOP 5 MEN</strong></a></p>
  8. <?php
  9.  if ( ! is_dir($dbpath))
  10.      @mkdir($dbpath, 0777) or die('nie moge utworzyc bazy danych, zrob to recznie tongue.gif');
  11.  
  12.  list($dbfile) = glob("{$dbpath}*");
  13.  
  14.  if ( is_file($dbfile) AND ( time() - preg_replace('#[^0-9]#', '', $dbfile) ) < $storing_time )  {
  15.        echo 'back';
  16.      $output = @unserialize(@file_get_contents($dbfile)) or die('nie moge odczytac pliq');
  17.  } else {
  18.        $data = @file_get_contents($site) or die('blad polaczenia');
  19.      preg_match_all('#<item>(.*?)</item>#s', $data, $items);
  20.      $items = $items[0];
  21.      $output = array();
  22.      $count = count($items);
  23.      if ( $count < 1) die('pusto');
  24.      for($i=0; $i<$count; $i++) {
  25.          preg_match('#<title>(.*?)</title>#s', $items[$i], $title);
  26.          $output[$i]['title'] = $title[1];
  27.          preg_match('#<link>(.*?)</link>#s', $items[$i], $link);
  28.          $output[$i]['link'] = $link[1];
  29.          preg_match('#<description>(.*?)</<description>#s', $items[$i], $description);
  30.          $output[$i]['description'] = $description[1];
  31.      }
  32.      @unlink($dbfile);// or die('nie moge usunac pliq');
  33.      @file_put_contents($dbpath.time(), serialize($output)) or die('nie moge zapisac pliq');
  34.  }
  35. ?>
  36.  
  37.  <?php foreach ($output as $o) { ?>
  38.      <p style="margin: 0 15px 0 15px;"><a href="<?php echo $o['link']; ?>" target='_top'><?php echo $o['title']; ?></a></p><?php echo $o['description']; ?>
  39.  <?php } ?>
  40.  <br /><br /><br /><br />
  41. <?php
  42. echo round(microtime()-$start, 6);
  43. ?>
in5ane
Sprawdziłem czas generowania obu skryptów (twój gox oraz ten mój z pierwszego postu) i czasy są takie same mniej więcej, czy to oznacza, że tak samo będą obciążały serwer czy nie?
gox
Podobne? znaczy się, oba maja zero przed przecinkiem tongue.gif

Jesli mozesz, to wrzuc na forum to co Ci napisal admin.
Wywrozyc mozna ze chodzilo o zapetlanie sie skryptu i tymsamym pozeranie czasu procesora.
Wielokrotnie uruchomiony proces może powodowac spadek wydajnosci systemu.
Polec administratorowi wydzielanie zasobów dla kazdego usera,
będzie mógł w ów czas przestawić ząbek pochylenia swojego administracyjnego fotela o jeden do tyłu tongue.gif

Moge zagwarantowac, ze moj skrypt nie bedzie powodowal przeciazenia serwera ze wzgledu na nieoptymalne napisanie.
oczywiscie moze to zrobisz jeszcze wydajniej, ale z pewnoscia problemy z przeciazeniem serwera od tego nie znikną.

Moja rzezba cashuje wyniki na 10 sek. co oszczedzi troche transferu i spowoduje szybsze ladowanie się strony.
in5ane
OK, dzięki ;-)
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.