Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [funkcja] Pobieranie informacji o filmie..., Rozwinięcie funkcji getPlots()
crash
post 29.01.2006, 12:11:46
Post #1





Grupa: Przyjaciele php.pl
Postów: 2 196
Pomógł: 2
Dołączył: 17.01.2004
Skąd: Sosnowiec

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


Funkcja przyjmuje tytuł filmu i rok produkcji (ewentualnie ścieżkę do pliku cache) pobiera dane o filmie i zwraca je w formie tablicy.

  1. <?php
  2. /**
  3.  * Funkcja pobierająca dane z podanego adresu,
  4.  * można zastąpić zwykłą file(), ale użycie CURL'a
  5.  * jest o wiele bardziej efektywne...
  6.  *
  7.  * @author crash : crash (at) os (dot) pl
  8.  */
  9. function getData( $url, $type = 'array' )
  10. {
  11.    $data = '';
  12.    
  13.    if( !extension_loaded( 'curl' ) )
  14.    {
  15.       @dl( 'curl' );
  16.    }
  17.  
  18.    if( extension_loaded( 'curl' ) )
  19.    {
  20.       $curl = curl_init();
  21.  
  22.       curl_setopt( $curl, CURLOPT_URL, $url );
  23.       curl_setopt( $curl, CURLOPT_HEADER, false );
  24.       curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
  25.       curl_setopt( $curl, CURLOPT_FORBID_REUSE, true );
  26.       curl_setopt( $curl, CURLOPT_FRESH_CONNECT, true );
  27.       curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
  28.       curl_setopt( $curl, CURLOPT_TIMEOUT, 25 );
  29.  
  30.       $data = curl_exec( $curl );
  31.       
  32.       curl_close( $curl );
  33.    }
  34.    else
  35.    {
  36.       $data = file( $url );
  37.    }
  38.    
  39.    if( ( $type == 'array' ) and !is_array( $data ) )
  40.    {
  41.       $data = explode( "\n", $data );
  42.    }
  43.    
  44.    return $data;
  45. }
  46.  
  47. /**
  48.  * Argumenty:
  49.  * string $parTitle - tytuł filmu,
  50.  *    int $year     - rok produkcji,
  51.  * string $cache    - opcjonalna ścieżka do pliku cache
  52.  *
  53.  * Zwraca:
  54.  * array $info
  55.  *
  56.  * @author crash : crash (at) os (dot) pl
  57.  */
  58. function getMovieInfo( $parTitle, $year, $cache = false )
  59. {
  60.    $info = array();
  61.    
  62.    if( $cache != false )
  63.    {
  64.       if( $data = @file_get_contents( $cache ) )
  65.       {
  66.          $data = unserialize( $data );
  67.  
  68.          if( array_key_exists( $parTitle, $data ) )
  69.          {
  70.             return $data[ $parTitle ];
  71.          }
  72.          else
  73.          {
  74.             foreach( $data as $name => $save )
  75.             {
  76.                if( strcasecmp( $name, $parTitle ) == 0 )
  77.                {
  78.                   return $save;
  79.                }
  80.             }
  81.          }
  82.       }
  83.    }
  84.    
  85.    $title  = preg_replace( '/^(the|el|le|a) (.+)/i', '\\2, \\1', $parTitle );
  86.    $url1   = 'http://www.filmweb.pl/Find?query=' . urlencode( $title ) . '&category=1&submit=szukaj';
  87.    $data   = getData( $url1 );
  88.    $inPage = false;
  89.  
  90.    if( count( $data ) > 0 )
  91.    {
  92.       foreach( $data as $n => $line )
  93.       {
  94.          if( ( stripos( $line, '<title>' ) !== false ) and ( strpos( $line, 'Filmweb.pl' ) === false ) )
  95.          {
  96.             $inPage = true;
  97.             break;
  98.          }
  99.          if( ( stripos( $line, $title ) !== false ) and ( strpos( $line, (string)$year ) !== false ) )
  100.          {
  101.             if( preg_match( '/href="([^"]+)"/', $line, $out ) )
  102.             {
  103.                $url2 = $out[ 1 ];
  104.                break;
  105.             }
  106.          }
  107.       }
  108.  
  109.       if( isset( $url2 ) or $inPage == true )
  110.       {
  111.          $data = implode( '', ( $inPage != true ) ? getData( $url2 ) : $data );
  112.          
  113.          if( preg_match( '#<div class="tyt">([^<]+)<span class="styt">\(?([^<\(\)]+)#', $data, $out ) )
  114.          {
  115.             $info[ 'title' ][ 'polish' ]   = trim( $out[ 1 ] );
  116.             $info[ 'title' ][ 'original' ] = ( $out[ 2 ] != $year ) ? trim( preg_replace( '/^AKA /', '', html_entity_decode( $out[ 2 ] ) ) ) : $info[ 'title' ][ 'polish' ];
  117.  
  118.             $info[ 'year' ] = $year;
  119.          }
  120.          
  121.          if( preg_match_all( '#(country|genre).id=[0-9]+">([^<]+)#', $data, $out ) )
  122.          {
  123.             foreach( $out[ 1 ] as $key => $type )
  124.             {
  125.                $info[ $type ][] = $out[ 2 ][ $key ];
  126.             }
  127.          }
  128.          
  129.          if( preg_match_all( '#<b>([^<]+)</b> \((Polska|Świat)\)#', $data, $out ) )
  130.          {
  131.             $types = array(
  132.                             'Polska' => 'poland',
  133.                             'Świat'  => 'world'
  134.                           );
  135.                           
  136.             foreach( $out[ 1 ] as $key => $type )
  137.             {
  138.                $info[ 'premiere' ][ $types[ $out[ 2 ][ $key ] ] ] = isset( $out[ 1 ][ $key ] ) ? strtotime( $out[ 1 ][ $key ] ) : 0;
  139.             }
  140.          }
  141.          
  142.          if( preg_match( '#czas trwania: ([0-9]+)#', $data, $out ) )
  143.          {
  144.             $info[ 'time' ] = $out[ 1 ];
  145.          }
  146.          
  147.          if( preg_match_all( '#(reżyseria|scenariusz|zdjęcia|muzyka)\t+?<a class="n" title="(.+?)- filmografia#', $data, $out ) )
  148.          {
  149.             $names = array(
  150.                             'reżyseria'  => 'director',
  151.                             'scenariusz' => 'screenplay',
  152.                             'zdjęcia'    => 'photos',
  153.                             'muzyka'     => 'music'
  154.                           );
  155.                           
  156.             foreach( $out[ 1 ] as $key => $type )
  157.             {
  158.                $info[ 'people' ][ $names[ $type ] ] = $out[ 2 ][ $key ];
  159.             }
  160.          }
  161.          
  162.          if( preg_match_all( '#<td width="194" align="right"><a class="n" title="(.+?)- filmografia.+?<td width="194" align="left">([^<]+)</td>#s', $data, $out ) )
  163.          {
  164.             foreach( $out[ 1 ] as $key => $who )
  165.             {
  166.                $info[ 'cast' ][ html_entity_decode( $who ) ] = html_entity_decode( $out[ 2 ][ $key ] );
  167.             }
  168.          }
  169.       }
  170.    }
  171.  
  172.    if( count( $info ) > 0 )
  173.    {
  174.       if( $cache != false )
  175.       {
  176.          if( $data = @file_get_contents( $cache ) )
  177.          {
  178.             $data = unserialize( $data );
  179.          
  180.             if( !array_key_exists( $parTitle, $data ) or ( count( $data[ $parTitle ] ) < count( $info ) ) )
  181.             {
  182.                $data[ $parTitle ] = $info;
  183.             }
  184. if( preg_match( '#src="(http://gfx.filmweb.pl/f/[^/]+/po.[^.]+.jpg|http://gfx.filmweb.pl/f/[^/]+/[^.]+.jpg)"#', $data, $out ) )
  185. {
  186.  $info[ 'poster' ] = $out[ 1 ];
  187. }
  188.          }
  189.          else
  190.          {
  191.             $data = array();
  192.             $data[ $parTitle ] = $info;
  193.          }
  194.  
  195.          file_put_contents( $cache, serialize( $data ) );
  196.       }
  197.    }
  198.  
  199.    return $info;
  200. }
  201. ?>


Przykład użycia:
  1. <?php
  2. print_r( getMovieInfo( 'Crash', 2004 ) );
  3. ?>


Wynik:
Kod
Array
(
   [title] => Array
       (
           [polish] => Miasto gniewu
           [original] => Crash
       )

   [year] => 2004
   [country] => Array
       (
           [0] => Niemcy
           [1] => USA
       )

   [genre] => Array
       (
           [0] => Dramat
           [1] => Obyczajowy
       )

   [premiere] => Array
       (
           [poland] => 1121983200
           [world] => 1094767200
       )

   [time] => 113
   [people] => Array
       (
           [director] => Paul Haggis
           [screenplay] => Paul Haggis
           [photos] => James Muro
           [music] => Mark Isham
       )

   [cast] => Array
       (
           [Sandra Bullock] => Jean
           [Don Cheadle] => Graham
           [Matt Dillon (I)] => Oficer Ryan
           [Jennifer Esposito] => Ria
           [William Fichtner] => Flanagan
           [Brendan Fraser] => Rick
           [Terrence Howard] => Cameron
           [Ludacris] => Anthony
           [Thandie Newton] => Christine
           [Ryan Phillippe] => Oficer Hanson
           [Larenz Tate] => Peter
           [Tony Danza] => Fred
           [Keith David (I)] => Porucznik Dixon
           [Shaun Toub] => Farhad
           [Loretta Devine] => Shaniqua
       )

    [poster] => http://gfx.filmweb.pl/f/107996/po.6990411.jpg
)


Updated!


--------------------
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
rama
post 19.08.2006, 22:36:16
Post #2





Grupa: Zarejestrowani
Postów: 50
Pomógł: 1
Dołączył: 25.02.2006
Skąd: Trójmiasto

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


To zależy co chcesz zrobić z tymi wartościami ( tablicą ), bo jeśli wyrzucić do przeglądarki ( np. echo/print ), to możesz użyć funkcji foreach winksmiley.jpg

  1. <?php
  2. $res = getMovieInfo( 'Crash', 2004 );
  3.  
  4. foreach( $res['cast'] as $klucz => $wartosc ) 
  5. {
  6.  // Jak ma wyświetlić, czyli...
  7.  print $klucz . 'jako' . $wartosc;
  8. }
  9. ?>
i w rezultacie zwraca Ci coś takiego
  1. Sandra Bullock jako Jean
  2. Don Cheadle jako Graham
  3. Matt Dillon (I) jako Oficer Ryan


PS Tablice przypisujesz tak samo jak zmienną, czyli
  1. <?php
  2. $cast = $res['cast'];
  3. ?>
A później odwołujesz się do zawartości tej tablicy tak jak wyżej opisałem winksmiley.jpg

Ten post edytował rama 19.08.2006, 22:40:21
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 Wersja Lo-Fi Aktualny czas: 27.06.2025 - 12:33