views:

670

answers:

2

I am recieving the following error:

Notice indefined offset 1: in C:\wamp\www\includes\imdbgrabber.php line 36

the code is for getting information from IMDB. The link is posted to the page using ajax on another page, I have tested that i am getting the correct response using echo $url

    <?php

$url = $_GET['link'];

echo $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);
$rating = get_match('/<div class="starbar-meta">(.*)<\/div>/isU',$imdb_content);

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



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


//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;
}
?>

  <!--start infobox-->
    <div class="info"> 
  <span>
  <?php echo '<strong>'.$name.'</strong>' ?>
  </span>

 <!-- <img src="http://1.bp.blogspot.com/_mySxtRcQIag/S6deHcoChaI/AAAAAAAAObc/Z1Xg3aB_wkU/s200/rising_sun.jpg" /> -->
  <div class="plot">
  <?php echo ''.substr($plot,0,strpos($plot,'<a')).'</div>' ?>
  </div>

  <div class="runtime">
  <?php echo'<strong>Run Time</strong><br />'.$run_time.'</div>' ?>
  </div>
<div class="releasedate">
<?php echo '<strong>Release Date</strong><br />'.substr($release_date,0,strpos($release_date,'<a')).'</div>' ?>
</div>
<div class="director">
<?php echo '<strong>Director</strong><br />'.$director.'' ?>
</div>
  <div class="rating">
  <?php echo '<strong>Rating</strong><br />'.$rating.'' ?>
  </div>
  </div>
  <!--end infobox-->
+1  A: 

If preg_match did not find a match, $matches is an empty array. So you should check if preg_match found an match before accessing $matches[0], for example:

function get_match($regex,$content)
{
    if (preg_match($regex,$content,$matches)) {
        return $matches[0];
    } else {
        return null;
    }
}
Gumbo
That fixed that error. But I still can't work out why it won't show the movie info when I use: $url = $_GET['link'];and only shows it when I use:$url = 'http://www.imdb.com/title/tt0367882/';I have tested that I am getting the correct data from the variable with echo and I am but it doesn't work.
A: 

This error indicates that you have an error on line 36. On line 36, you are indexing into an array, offset 0. The error says that the index is undefined. Therefore, $matches must not be an array.

greg