"unexpected $end" - only happens when I run on web server

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
schwza
Forum Newbie
Posts: 2
Joined: Sun May 31, 2009 9:21 pm

"unexpected $end" - only happens when I run on web server

Post by schwza »

Here is my function:

Code: Select all

<html>
<head></head>
<body>
 
<?php
 
////////////////////////////////////////////////////////////////////////////////
 
function clhead($u) {
 
 
////////////////////////////////////////////////////////////////////////////////
 
  if (strpos($u, '.craigslist.org') !== false) {
 
    // retrieve craigslist listing
 
    $clpage = file_get_contents($u);
 
    // parse title of craigslist listing
 
 //   $titleStartPos = stripos($clpage, '<h2>') + 4;
 
    $titleLength = stripos($clpage, '</h2>') - $titleStartPos;
 
    if ($titleLength < 500) {
 
      return substr($clpage, $titleStartPos, $titleLength);
 
      }
 
    }
 
  return '';
}
 
 
function source ($u)
{
    $index = 1;
    $page="";
    $file = fopen($u, "r");
        
    while(!feof($file)) 
    {
        //read file line by line into variable
 
        $page = $page . fgets($file);
    }
 
    fclose ($file);
 
 
 
    $startpos = stripos($page, "h2");
    if ($startpos == false) {echo "no br!";}
    if ($startpos !== false) 
    {
        $trimmed = substr($page, $startpos, 25);
        $startpos = stripos($trimmed, " ");
        $targetbr = substr($trimmed, $startpos+3,1);
        echo "targetbr is $targetbr and source is $u"."<br>";
        //  echo "first line of current page is". $page."<br>";
        $startpos = stripos($page, "http://maps.google.com/?q=loc%3A+");  // finding the google map link
        if (!$startpos) {echo "no map!";}
        else
        {
            if ($startpos > 0) {$startpos += 33;}
            $adr[$index] = substr($page,$startpos);
            $endpos = stripos($adr[$index], ">");
            $adr[$index]= substr($adr[$index],0, $endpos-1);  // now $adr[$index] is just an address
            {
                $url[$index] = $u;
                $index++;
            }
        }
    }
    $r=array ($adr,$targetbr);
    return ($r);
}
 
 
function readcl($key, $p)
{
    $home = "http://boston.craigslist.org";  //should be read
    $searchurl = "http://boston.craigslist.org/search/abo?query=130&minAsk=min&maxAsk=max&bedrooms=";  // construct from GET
    $sourceurl = "http://boston.craigslist.org/sob/abo/1198499240.html";
    //$_SERVER['PHP_SELF']
    $read = source($sourceurl);
    $adr = $read[0];
    $targetbr = $read[1];
    $url[1]="www.home.com";
    $lines = file($searchurl); 
    $addString = $lines["109"];
    echo "before loop".$addString;
    $addArray = explode('<a', $addString);
    $maxaddress = sizeof($addArray)-1;
    $index = 2;
    for ($count = 1; $count <= $maxaddress; $count++)
    {
        echo "<br>";
        $listing = $home . substr($addArray[$count], 7, 24);
        echo $listing."<br>";
        $page="";
        $file = fopen($listing, "r");
        while(!feof($file)) 
        {
            //read file line by line into variable
            $page = $page . fgets($file, 4096);
        }
        fclose ($file);
        $startpos = stripos($page, "h2");
        if ($startpos == false) {echo "no br!";}
        if ($startpos !== false) 
        {
            $trimmed = substr($page, $startpos, 25);
            $startpos = stripos($trimmed, " ");
            $currentbr = substr($trimmed, $startpos+3,1);
            $unique = true;
            echo "count is $count and currentbr is $currentbr and targetbr is $targetbr"."<br>";
            //  echo "first line of current page is". $page."<br>";
            if ($currentbr == $targetbr)
            {
                echo "equal bedrooms".$count."<br>";
                $startpos = stripos($page, "http://maps.google.com/?q=loc%3A+");  // finding the google map link
                if (!$startpos) {echo "no map!";}
                else
                {
                    if ($startpos > 0) {$startpos += 33;}
                    $adr[$index] = substr($page,$startpos);
                    $endpos = stripos($adr[$index], ">");
                    $adr[$index]= substr($adr[$index],0, $endpos-1);  // now $adr[$index] is just an address
                    echo (levenshtein($adr[$index],''))."<br>";
                    echo "...".$adr[$index]."heyo";
                    echo strlen($adr[$index]);
                    //if (!(levenshtein($adr[$index],'')==100))
                    for ($innercount = 1; $innercount <= $index-1; $innercount++)  // checking if $adr[$index] is already in arry
                    {    
                        echo "innercount is $innercount, index is $index, adr[index] is $adr[$index] and adr[inn] is $adr[$innercount]";
                        echo levenshtein($adr[$index], $adr[$innercount])."<br>";
                        if ((levenshtein($adr[$index], $adr[$innercount]) <=1))
                        { 
                            $unique = false;
                            $adr[$index]="";
                            echo "count is $count and we have a duplicate between index $index and innercount $innercount <br>";
                        }
                    }
                    if ($unique == true)  
                    {
//                      $startpos = stripos($page,"href");  // finding the link to the apartment on the search page
//                      $addString = substr($addString,$startpos+6);
//                      $endpos = stripos($addString, "html");
//                      $url[$index] = $home.substr($addString,0,$endpos+4);  // $url[$index] is now the link an apartment.
                        $url[$index] = $listing;
                        $index++;
                        echo "index is ".($index-1)."<br>";
                        echo $url[$index-1]."<br>";
                        echo $adr[$index-1]."<br> <br>";
                    }   
                }   
            }
        }
    }
 
$GMAPKEY = "ABQIAAAA1mZJWVhJ4pgsZrVOByGrUxR8yX16qpxcIw-18X4ZTTjgjJUaxBR60568W3qluII-VMKcJ2rPw7cYHw"; 
$base_url = "http://maps.google.com/maps/geo?output=csv&key=" . $GMAPKEY;
//  $request_url = $base_url . "&q=" . urlencode("\"".$adr[1]);
//  $csv = file_get_contents($request_url);
//  echo $csv."<br>";
//  print_r($url)."<br>";
//  print_r($adr)."<br>";
    
    
/////////////////
 
      // Initialize delay in geocode speed
        $minlat=42.3707910;
 
  $maxlat=42.3728430;
 
  $minlng=-71.1047360;
 
  $maxlng="-71.0997530";
 
  $delay = 0;
  $addresses = 0;
  // Iterate through the rows, geocoding each address
  for ($i = 1; $i <= $index-1; $i++) {
    if ($adr[$i] > '') {
//  echo "adr ".$i.$adr[$i];
      $geocode_pending = true;
      while ($geocode_pending) {
        $request_url = $base_url . "&q=" . urlencode("\"".$adr[$i]);
        $csv = file_get_contents($request_url);
        if (!$csv) {
          echo "geocode failed";
          $errmsg = "Unable to find address [" . $adr[$i];
          return;
          }
        $csvSplit = split(",", $csv);
        $status = $csvSplit[0];
//      echo "split and i is ".$i." ";
        if (strcmp($status, "200") == 0) {
          // Successful geocode
          $geocode_pending = false;
          $coordinates = $xml->Response->Placemark->Point->coordinates;
          $coordinatesSplit = split(",", $coordinates);
          // Format: Longitude, Latitude, Altitude
//           $lat[$i] = $coordinatesSplit[1];
//           $lng[$i] = $coordinatesSplit[0];
          $lat[$i] = $csvSplit[2];
          $lng[$i] = $csvSplit[3];
          $addresses++;
          // convert lat & lng to string for embedding in static map URL
          $pointStr = $lat[$i] . ',' . $lng[$i];
//        echo "i is ".$i."and poinststr is ".$pointStr;
          // retrieve craigslist heading from posting
          $txt[$i] = clhead($url[$i]);
          // update static map URL to include marker for address
          // if this is the first flag, the formatting is slightly different
//        echo "about to make imageurl"."<br>";
          if ($addresses == 1) {
            $imageurl = 
                'http://maps.google.com/staticmap?size=450x450&sensor=false' .
                '&key=' . GMAPKEY . '&zoom=14&center=' . $pointStr .
                '&markers=' . $pointStr . ',red';
            }
          else {
            $imageurl .= '|' . $pointStr . ',smallred';
            }
          }
        else if (strcmp($status, "620") == 0) {
          // sent geocodes too fast
          echo "geocodes too fast";
          $delay += 100000;
          }
        else {
          // failure to geocode
          $geocode_pending = false;
//        echo $status;
          echo "didn't geocode";
          $errmsg = "Unable to find address " . $adr[$i];
          return;
          }
        usleep($delay);
        }
      }
    }
    
  // ...successfully geocoded all addresses and we are now ready to do the stuff in will's map.php */
//echo "about to echo imageurl ";
//echo $imageurl;
 
$r = array("imageurl" => $imageurl, "lat" => $lat, "lng" => $lng, "index" => $index, "adr" => $adr, 
"txt" => $txt, "minlat" => $minlat, "maxlat" => $maxlat, "minlng" => $minlng, "maxlng" => $maxlng, "url" => $url,
"addresses" => $addresses);
return ($r);
}
  
  
  ?>
</body>
</html>
 
When I run a program that calls the function locally on my WampServer, everything works perfectly. When I run a program that calls the function on my web server, I get the following error message: "Parse error: syntax error, unexpected $end in /home/content/w/a/l/waltersc/html/mapincluded.com/clhead.php on line 17". First of all, the error is not on line 17 of my code as it is displayed with Notepad++. It is on line 44 in Notepad++, which is:

while(!feof($file))

I know this is the problem because if I comment out that line then the program compiles correctly (but obviously doesn't work quite right). I have run several different programs that used the function, and they all work locally but get the same error on the web server, so it's not a problem with the program that uses the function.

I'm pretty new to this and I've never come across this problem before. Help?
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: "unexpected $end" - only happens when I run on web server

Post by mikemike »

Perhaps a missing closing brace? Try matching them all up in your IDE
schwza
Forum Newbie
Posts: 2
Joined: Sun May 31, 2009 9:21 pm

Re: "unexpected $end" - only happens when I run on web server

Post by schwza »

mikemike wrote:Perhaps a missing closing brace? Try matching them all up in your IDE
I don't think that's the problem. If there were a missing closing brace then a) the program would not run correctly on my local machine and b) removing the line "while(!feof($file))" would not allow the program to run. Also I matched up all of the braces.
Post Reply