2010-03-23 116 views
1

我recieving一個錯誤:IMDB抓取PHP

說明:未定義可變\瓦帕\ WWW \包括在第17行

\ imdbgrabber.php當使用這個代碼:

<?php 
//url 
$url = 'http://www.imdb.com/title/tt0367882/'; 

//get the page content 
$imdb_content = get_data($url); 

//parse for product name 
$name = get_match('/<title>(.*)<\/title>/isU',$imdb_content); 
$director = strip_tags(get_match('/<h5[^>]*>Director:<\/h5>(.*)<\/div>/isU',$imdb_content)); 
$plot = get_match('/<h5[^>]*>Plot:<\/h5>(.*)<\/div>/isU',$imdb_content); 
$release_date = get_match('/<h5[^>]*>Release Date:<\/h5>(.*)<\/div>/isU',$imdb_content); 
$mpaa = get_match('/<a href="\/mpaa">MPAA<\/a>:<\/h5>(.*)<\/div>/isU',$imdb_content); 
$run_time = get_match('/Runtime:<\/h5>(.*)<\/div>/isU',$imdb_content); 

//build content 


line 17 --> $content.= '<h2>Film</h2><p>'.$name.'</p>'; 
    $content.= '<h2>Director</h2><p>'.$director.'</p>'; 
    $content.= '<h2>Plot</h2><p>'.substr($plot,0,strpos($plot,'<a')).'</p>'; 
    $content.= '<h2>Release Date</h2><p>'.substr($release_date,0,strpos($release_date,'<a')).'</p>'; 
    $content.= '<h2>MPAA</h2><p>'.$mpaa.'</p>'; 
    $content.= '<h2>Run Time</h2><p>'.$run_time.'</p>'; 
    $content.= '<h2>Full Details</h2><p><a href="'.$url.'" rel="nofollow">'.$url.'</a></p>'; 

    echo $content; 

//gets the match content 
function get_match($regex,$content) 
{ 
    preg_match($regex,$content,$matches); 
    return $matches[1]; 
} 

//gets the data from a URL 
function get_data($url) 
{ 
    $ch = curl_init(); 
    $timeout = 5; 
    curl_setopt($ch,CURLOPT_URL,$url); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); 
    $data = curl_exec($ch); 
    curl_close($ch); 
    return $data; 
} 
?> 
+0

http://www.imdb.com/interfaces – 2010-03-23 17:15:58

+0

我最近爲此做了一個類,可能會感興趣:https:// github。 COM/aramkocharyan/IMDB式鏟運機 – 2011-09-15 13:41:59

回答

6

要附加內容不存在的變量與$content=更換$content.=。更改行17的分配:

$content = '<h2>Film</h2><p>'.$name.'</p>'; 

你也可以改變的代碼段爲以下,這是稍微整潔:

$content = '<h2>Film</h2><p>'.$name.'</p>' 
     . '<h2>Director</h2><p>'.$director.'</p>' 
     . '<h2>Plot</h2><p>'.substr($plot,0,strpos($plot,'<a')).'</p>' 
     // etc 
3

您正嘗試在變量$content中添加某些東西,但它不存在時,這自然會觸發錯誤。

嘗試在17行

1

除了別人怎麼說,是另外一個問題你需要注意的代碼。在返回函數get_match的值之前,您沒有檢查preg_match的返回值。你應該這樣做:

if(preg_match($regex,$content,$matches)) 
    return $matches[1]; 
else 
    // return some default 
3

你沒有收到一個錯誤,你收到一個通知,因爲你試圖將某些東西連接到一個不存在的變量。從第17行刪除.=的點或在行17之前放$content = ''