1

エラーが表示されます:

注意: 未定義の変数: 17 行目の C:\wamp\www\includes\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;
}
?>
4

4 に答える 4

6

存在しない変数にコンテンツを追加しています。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
于 2010-03-23T16:19:21.533 に答える
3

変数がまだ存在しないときに変数に何かを追加しようとすると$content、当然エラーが発生します。

17行目で置き換え$content.=てみてください。$content=

于 2010-03-23T16:19:19.657 に答える
3

エラーを受け取っていません。存在しない変数に何かを連結しようとしたため、通知を受け取っています。17 行目のドットを削除するか、17行目の前に.=置きます。$content = ''

于 2010-03-23T16:20:13.700 に答える
1

他の人が言ったこととは別に、あなたのコードには注意が必要な別の問題があります。preg_match関数 から値を返す前に、 の戻り値をチェックしていませんget_match。次のようにする必要があります。

if(preg_match($regex,$content,$matches))
  return $matches[1];
else
  // return some default
于 2010-03-23T16:20:10.177 に答える