Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] Pobieranie danych z RSS - optymalizacja
in5ane
post 14.11.2008, 22:45:51
Post #1





Grupa: Zarejestrowani
Postów: 1 335
Pomógł: 34
Dołączył: 9.11.2005
Skąd: Wrocław

Ostrzeżenie: (10%)
X----


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.


--------------------
> > > Tworzenie stron < < <
Go to the top of the page
+Quote Post
-gox-
post 14.11.2008, 23:01:52
Post #2





Goście







  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.        ?>
Go to the top of the page
+Quote Post
Noddi
post 14.11.2008, 23:10:51
Post #3





Grupa: Zarejestrowani
Postów: 45
Pomógł: 7
Dołączył: 25.12.2004
Skąd: Kraków

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


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. ?>


--------------------
"Yes, experience matters. No, one year repeated fifteen times does not count."
bartoszlugowski.com
Go to the top of the page
+Quote Post
-gox-
post 14.11.2008, 23:12:43
Post #4





Goście







Scigamy sie? smile.gif
Go to the top of the page
+Quote Post
-gox-
post 14.11.2008, 23:13:57
Post #5





Goście







The SimpleXML extension requires PHP 5.
Go to the top of the page
+Quote Post
in5ane
post 14.11.2008, 23:39:52
Post #6





Grupa: Zarejestrowani
Postów: 1 335
Pomógł: 34
Dołączył: 9.11.2005
Skąd: Wrocław

Ostrzeżenie: (10%)
X----


@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.

Ten post edytował in5ane 14.11.2008, 23:41:34


--------------------
> > > Tworzenie stron < < <
Go to the top of the page
+Quote Post
-gox-
post 14.11.2008, 23:53:03
Post #7





Goście







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; ?>
Go to the top of the page
+Quote Post
bim2
post 15.11.2008, 01:09:24
Post #8





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

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


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>


--------------------
Go to the top of the page
+Quote Post
-gox-
post 15.11.2008, 01:46:17
Post #9





Goście







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
Go to the top of the page
+Quote Post
in5ane
post 15.11.2008, 12:21:52
Post #10





Grupa: Zarejestrowani
Postów: 1 335
Pomógł: 34
Dołączył: 9.11.2005
Skąd: Wrocław

Ostrzeżenie: (10%)
X----


@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. ?>


--------------------
> > > Tworzenie stron < < <
Go to the top of the page
+Quote Post
-gox-
post 15.11.2008, 13:03:54
Post #11





Goście







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. ?>
Go to the top of the page
+Quote Post
in5ane
post 15.11.2008, 16:04:55
Post #12





Grupa: Zarejestrowani
Postów: 1 335
Pomógł: 34
Dołączył: 9.11.2005
Skąd: Wrocław

Ostrzeżenie: (10%)
X----


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?


--------------------
> > > Tworzenie stron < < <
Go to the top of the page
+Quote Post
-gox-
post 15.11.2008, 16:57:44
Post #13





Goście







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.
Go to the top of the page
+Quote Post
in5ane
post 15.11.2008, 18:37:56
Post #14





Grupa: Zarejestrowani
Postów: 1 335
Pomógł: 34
Dołączył: 9.11.2005
Skąd: Wrocław

Ostrzeżenie: (10%)
X----


OK, dzięki ;-)


--------------------
> > > Tworzenie stron < < <
Go to the top of the page
+Quote Post

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 Wersja Lo-Fi Aktualny czas: 25.05.2025 - 08:27