Funkcja pobiera opisy filmu z portalu FilmWeb.pl i zwraca je w tablicy. Funkcja może działać trochę wolno (zależy od łącza itp), bo musi przejść aż przez 2-3 strony. Napisałem ją dla własnych potrzeb (dodatek do mojego katalogu filmów), ale może akurat komuś się przyda... Cachowanie jest dość mało wydajne (po prostu zapisuje opisy w zserializowanej tablicy do pliku), ale w zupełności wystarczy.

Kod:
  1. <?php
  2. /**
  3. * Argumenty:
  4. * string $parTitle - tytuł filmu,
  5. * int $year - rok produkcji,
  6. * string $cache - opcjonalny plik cache (na opisy wcześniej szukanych filmów)
  7. * Zwraca:
  8. * array $plots
  9. **/
  10. function getPlots( $parTitle, $year, $cache = false )
  11. {
  12.    $plots = array();
  13.    
  14.    if( $cache != false )
  15.    {
  16.       if( $data = @file_get_contents( $cache ) )
  17.       {
  18.          $data = unserialize( $data );
  19.  
  20.          if( array_key_exists( $parTitle, $data ) )
  21.          {
  22.             return $data[ $parTitle ];
  23.          }
  24.          else
  25.          {
  26.             foreach( $data as $name => $save )
  27.             {
  28.                if( strcasecmp( $name, $parTitle ) == 0 )
  29.                {
  30.                   return $save;
  31.                }
  32.             }
  33.          }
  34.       }
  35.    }
  36.    
  37.    $title = preg_replace( '/^(the|el|le|a) (.+)/i', '2, 1', $parTitle );
  38.  
  39.    $url1  = 'http://www.filmweb.pl/Find?query=' . urlencode( $title ) . '&category=1&submit=szukaj';
  40.  
  41.    $data  = file( $url1 );
  42.  
  43.    if( count( $data ) > 0 )
  44.    {
  45.       foreach( $data as $n => $line )
  46.       {
  47.          if( ( stripos( $line, $title ) !== false ) and ( strpos( $line, (string)$year ) !== false ) )
  48.          {
  49.             if( preg_match( '/href="([^"]+)"/', $line, $out ) )
  50.             {
  51.                $url2 = $out[ 1 ];
  52.                break;
  53.             }
  54.          }
  55.          if( strpos( $line, 'opisy' ) !== false )
  56.          {
  57.             if( preg_match( '/href="([^"]+)"/', $line, $out ) )
  58.             {
  59.                $url3 = $out[ 1 ];
  60.                break;
  61.             }
  62.          }
  63.       }
  64.  
  65.       if( isset( $url2 ) )
  66.       {
  67.          $data = file( $url2 );
  68.  
  69.          foreach( $data as $n => $line )
  70.          {
  71.             if( strpos( $line, 'opisy' ) !== false )
  72.             {
  73.                if( preg_match( '/href="([^"]+)"/', $line, $out ) )
  74.                {
  75.                   $url3 = $out[ 1 ];
  76.                   break;
  77.                }
  78.             }
  79.          }
  80.       }
  81.  
  82.       if( isset( $url3 ) )
  83.       {
  84.          $data = implode( '', file( $url3 ) );
  85.          
  86.          if( preg_match_all( '/<li><div align="justify">(.+?)</div></li>/', $data, $out ) )
  87.          {
  88.             $plots = $out[ 1 ];
  89.          }
  90.       }
  91.     }
  92.  
  93.    if( count( $plots ) > 0 )
  94.    {
  95.       foreach( $plots as $n => $plot )
  96.       {
  97.          $plots[ $n ] = trim( preg_replace( '/[.+]/', '', str_replace( array( "r", "n", '<br/>' ), '', $plot, $ch ) ) );
  98.       }
  99.       if( $cache != false )
  100.       {
  101.          if( $data = @file_get_contents( $cache ) )
  102.          {
  103.             $data = unserialize( $data );
  104.          
  105.             if( !array_key_exists( $title, $data ) )
  106.             {
  107.                $data[ $parTitle ] = $plots;
  108.             }
  109.          }
  110.          else
  111.          {
  112.             $data = array();
  113.             $data[ $parTitle ] = $plots;
  114.          }
  115.          file_put_contents( $cache, serialize( $data ) );
  116.       }
  117.    }
  118.    return $plots;
  119. }
  120. ?>


Przykład użycia:
  1. <?php
  2. print_r( getPlots( 'Batman Begins', 2005, './plots.dat' ) );
  3. ?>


Fixed!