Funkcja przyjmuje tytuł filmu i rok produkcji (ewentualnie ścieżkę do pliku cache) pobiera dane o filmie i zwraca je w formie tablicy.
<?php
/**
* Funkcja pobierająca dane z podanego adresu,
* można zastąpić zwykłą file(), ale użycie CURL'a
* jest o wiele bardziej efektywne...
*
* @author crash : crash (at) os (dot) pl
*/
function getData( $url, $type = 'array' )
{
$data = '';
{
}
{
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, $url );
curl_setopt( $curl, CURLOPT_HEADER, false );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $curl, CURLOPT_FORBID_REUSE, true );
curl_setopt( $curl, CURLOPT_FRESH_CONNECT, true );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_TIMEOUT, 25 );
$data = curl_exec( $curl );
curl_close( $curl );
}
else
{
}
if( ( $type == 'array' ) and
!is_array( $data ) ) {
}
return $data;
}
/**
* Argumenty:
* string $parTitle - tytuł filmu,
* int $year - rok produkcji,
* string $cache - opcjonalna ścieżka do pliku cache
*
* Zwraca:
* array $info
*
* @author crash : crash (at) os (dot) pl
*/
function getMovieInfo( $parTitle, $year, $cache = false )
{
if( $cache != false )
{
{
{
return $data[ $parTitle ];
}
else
{
foreach( $data as $name => $save )
{
{
return $save;
}
}
}
}
}
$title = preg_replace( '/^(the|el|le|a) (.+)/i', '\\2, \\1', $parTitle ); $url1 = 'http://www.filmweb.pl/Find?query=' . urlencode( $title ) . '&category=1&submit=szukaj'; $data = getData( $url1 );
$inPage = false;
{
foreach( $data as $n => $line )
{
if( ( stripos
( $line, '<title>' ) !== false ) and
( strpos( $line, 'Filmweb.pl' ) === false ) ) {
$inPage = true;
break;
}
if( ( stripos
( $line, $title ) !== false ) and
( strpos( $line, (string
)$year ) !== false ) ) {
if( preg_match( '/href="([^"]+)"/', $line, $out ) ) {
$url2 = $out[ 1 ];
break;
}
}
}
if( isset( $url2 ) or
$inPage == true ) {
$data = implode( '', ( $inPage != true ) ? getData
( $url2 ) : $data );
if( preg_match( '#<div class="tyt">([^<]+)<span class="styt">\(?([^<\(\)]+)#', $data, $out ) ) {
$info[ 'title' ][ 'polish' ] = trim( $out[ 1
] );
$info[ 'year' ] = $year;
}
if( preg_match_all( '#(country|genre).id=[0-9]+">([^<]+)#', $data, $out ) ) {
foreach( $out[ 1 ] as $key => $type )
{
$info[ $type ][] = $out[ 2 ][ $key ];
}
}
if( preg_match_all( '#<b>([^<]+)</b> \((Polska|Świat)\)#', $data, $out ) ) {
'Polska' => 'poland',
'Świat' => 'world'
);
foreach( $out[ 1 ] as $key => $type )
{
$info[ 'premiere' ][ $types[ $out[ 2
][ $key ] ] ] = isset( $out[ 1
][ $key ] ) ?
strtotime( $out[ 1
][ $key ] ) : 0; }
}
if( preg_match( '#czas trwania: ([0-9]+)#', $data, $out ) ) {
$info[ 'time' ] = $out[ 1 ];
}
if( preg_match_all( '#(reżyseria|scenariusz|zdjęcia|muzyka)\t+?<a class="n" title="(.+?)- filmografia#', $data, $out ) ) {
'reżyseria' => 'director',
'scenariusz' => 'screenplay',
'zdjęcia' => 'photos',
'muzyka' => 'music'
);
foreach( $out[ 1 ] as $key => $type )
{
$info[ 'people' ][ $names[ $type ] ] = $out[ 2 ][ $key ];
}
}
if( preg_match_all( '#<td width="194" align="right"><a class="n" title="(.+?)- filmografia.+?<td width="194" align="left">([^<]+)</td>#s', $data, $out ) ) {
foreach( $out[ 1 ] as $key => $who )
{
}
}
}
}
{
if( $cache != false )
{
{
{
$data[ $parTitle ] = $info;
}
if( preg_match( '#src="(http://gfx.filmweb.pl/f/[^/]+/po.[^.]+.jpg|http://gfx.filmweb.pl/f/[^/]+/[^.]+.jpg)"#', $data, $out ) ) {
$info[ 'poster' ] = $out[ 1 ];
}
}
else
{
$data[ $parTitle ] = $info;
}
file_put_contents
( $cache, serialize( $data ) ); }
}
return $info;
}
?>
Przykład użycia:
<?php
print_r( getMovieInfo
( 'Crash', 2004
) ); ?>
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!