schwza Posted June 1, 2009 Share Posted June 1, 2009 Here is my function: 1. <html> 2. <head></head> 3. <body> 4. 5. <?php 6. 7. //////////////////////////////////////////////////////////////////////////////// 8. 9. function clhead($u) { 10. 11. 12. //////////////////////////////////////////////////////////////////////////////// 13. 14. if (strpos($u, '.craigslist.org') !== false) { 15. 16. // retrieve craigslist listing 17. 18. $clpage = file_get_contents($u); 19. 20. // parse title of craigslist listing 21. 22. // $titleStartPos = stripos($clpage, '<h2>') + 4; 23. 24. $titleLength = stripos($clpage, '</h2>') - $titleStartPos; 25. 26. if ($titleLength < 500) { 27. 28. return substr($clpage, $titleStartPos, $titleLength); 29. 30. } 31. 32. } 33. 34. return ''; 35. } 36. 37. 38. function source ($u) 39. { 40. $index = 1; 41. $page=""; 42. $file = fopen($u, "r"); 43. 44. while(!feof($file)) 45. { 46. //read file line by line into variable 47. 48. $page = $page . fgets($file); 49. } 50. 51. fclose ($file); 52. 53. 54. 55. $startpos = stripos($page, "h2"); 56. if ($startpos == false) {echo "no br!";} 57. if ($startpos !== false) 58. { 59. $trimmed = substr($page, $startpos, 25); 60. $startpos = stripos($trimmed, " "); 61. $targetbr = substr($trimmed, $startpos+3,1); 62. echo "targetbr is $targetbr and source is $u"."<br>"; 63. // echo "first line of current page is". $page."<br>"; 64. $startpos = stripos($page, "http://maps.google.com/?q=loc%3A+"); // finding the google map link 65. if (!$startpos) {echo "no map!";} 66. else 67. { 68. if ($startpos > 0) {$startpos += 33;} 69. $adr[$index] = substr($page,$startpos); 70. $endpos = stripos($adr[$index], ">"); 71. $adr[$index]= substr($adr[$index],0, $endpos-1); // now $adr[$index] is just an address 72. { 73. $url[$index] = $u; 74. $index++; 75. } 76. } 77. } 78. $r=array ($adr,$targetbr); 79. return ($r); 80. } 81. 82. 83. function readcl($key, $p) 84. { 85. $home = "http://boston.craigslist.org"; //should be read 86. $searchurl = "http://boston.craigslist.org/search/abo?query=130&minAsk=min&maxAsk=max&bedrooms="; // construct from GET 87. $sourceurl = "http://boston.craigslist.org/sob/abo/1198499240.html"; 88. //$_SERVER['PHP_SELF'] 89. $read = source($sourceurl); 90. $adr = $read[0]; 91. $targetbr = $read[1]; 92. $url[1]="www.home.com"; 93. $lines = file($searchurl); 94. $addString = $lines["109"]; 95. echo "before loop".$addString; 96. $addArray = explode('<a', $addString); 97. $maxaddress = sizeof($addArray)-1; 98. $index = 2; 99. for ($count = 1; $count <= $maxaddress; $count++) 100. { 101. echo "<br>"; 102. $listing = $home . substr($addArray[$count], 7, 24); 103. echo $listing."<br>"; 104. $page=""; 105. $file = fopen($listing, "r"); 106. while(!feof($file)) 107. { 108. //read file line by line into variable 109. $page = $page . fgets($file, 4096); 110. } 111. fclose ($file); 112. $startpos = stripos($page, "h2"); 113. if ($startpos == false) {echo "no br!";} 114. if ($startpos !== false) 115. { 116. $trimmed = substr($page, $startpos, 25); 117. $startpos = stripos($trimmed, " "); 118. $currentbr = substr($trimmed, $startpos+3,1); 119. $unique = true; 120. echo "count is $count and currentbr is $currentbr and targetbr is $targetbr"."<br>"; 121. // echo "first line of current page is". $page."<br>"; 122. if ($currentbr == $targetbr) 123. { 124. echo "equal bedrooms".$count."<br>"; 125. $startpos = stripos($page, "http://maps.google.com/?q=loc%3A+"); // finding the google map link 126. if (!$startpos) {echo "no map!";} 127. else 128. { 129. if ($startpos > 0) {$startpos += 33;} 130. $adr[$index] = substr($page,$startpos); 131. $endpos = stripos($adr[$index], ">"); 132. $adr[$index]= substr($adr[$index],0, $endpos-1); // now $adr[$index] is just an address 133. echo (levenshtein($adr[$index],''))."<br>"; 134. echo "...".$adr[$index]."heyo"; 135. echo strlen($adr[$index]); 136. //if (!(levenshtein($adr[$index],'')==100)) 137. for ($innercount = 1; $innercount <= $index-1; $innercount++) // checking if $adr[$index] is already in arry 138. { 139. echo "innercount is $innercount, index is $index, adr[index] is $adr[$index] and adr[inn] is $adr[$innercount]"; 140. echo levenshtein($adr[$index], $adr[$innercount])."<br>"; 141. if ((levenshtein($adr[$index], $adr[$innercount]) <=1)) 142. { 143. $unique = false; 144. $adr[$index]=""; 145. echo "count is $count and we have a duplicate between index $index and innercount $innercount <br>"; 146. } 147. } 148. if ($unique == true) 149. { 150. // $startpos = stripos($page,"href"); // finding the link to the apartment on the search page 151. // $addString = substr($addString,$startpos+6); 152. // $endpos = stripos($addString, "html"); 153. // $url[$index] = $home.substr($addString,0,$endpos+4); // $url[$index] is now the link an apartment. 154. $url[$index] = $listing; 155. $index++; 156. echo "index is ".($index-1)."<br>"; 157. echo $url[$index-1]."<br>"; 158. echo $adr[$index-1]."<br> <br>"; 159. } 160. } 161. } 162. } 163. } 164. 165. $GMAPKEY = "ABQIAAAA1mZJWVhJ4pgsZrVOByGrUxR8yX16qpxcIw-18X4ZTTjgjJUaxBR60568W3qluII-VMKcJ2rPw7cYHw"; 166. $base_url = "http://maps.google.com/maps/geo?output=csv&key=" . $GMAPKEY; 167. // $request_url = $base_url . "&q=" . urlencode("\"".$adr[1]); 168. // $csv = file_get_contents($request_url); 169. // echo $csv."<br>"; 170. // print_r($url)."<br>"; 171. // print_r($adr)."<br>"; 172. 173. 174. ///////////////// 175. 176. // Initialize delay in geocode speed 177. $minlat=42.3707910; 178. 179. $maxlat=42.3728430; 180. 181. $minlng=-71.1047360; 182. 183. $maxlng="-71.0997530"; 184. 185. $delay = 0; 186. $addresses = 0; 187. // Iterate through the rows, geocoding each address 188. for ($i = 1; $i <= $index-1; $i++) { 189. if ($adr[$i] > '') { 190. // echo "adr ".$i.$adr[$i]; 191. $geocode_pending = true; 192. while ($geocode_pending) { 193. $request_url = $base_url . "&q=" . urlencode("\"".$adr[$i]); 194. $csv = file_get_contents($request_url); 195. if (!$csv) { 196. echo "geocode failed"; 197. $errmsg = "Unable to find address [" . $adr[$i]; 198. return; 199. } 200. $csvSplit = split(",", $csv); 201. $status = $csvSplit[0]; 202. // echo "split and i is ".$i." "; 203. if (strcmp($status, "200") == 0) { 204. // Successful geocode 205. $geocode_pending = false; 206. $coordinates = $xml->Response->Placemark->Point->coordinates; 207. $coordinatesSplit = split(",", $coordinates); 208. // Format: Longitude, Latitude, Altitude 209. // $lat[$i] = $coordinatesSplit[1]; 210. // $lng[$i] = $coordinatesSplit[0]; 211. $lat[$i] = $csvSplit[2]; 212. $lng[$i] = $csvSplit[3]; 213. $addresses++; 214. // convert lat & lng to string for embedding in static map URL 215. $pointStr = $lat[$i] . ',' . $lng[$i]; 216. // echo "i is ".$i."and poinststr is ".$pointStr; 217. // retrieve craigslist heading from posting 218. $txt[$i] = clhead($url[$i]); 219. // update static map URL to include marker for address 220. // if this is the first flag, the formatting is slightly different 221. // echo "about to make imageurl"."<br>"; 222. if ($addresses == 1) { 223. $imageurl = 224. 'http://maps.google.com/staticmap?size=450x450&sensor=false' . 225. '&key=' . GMAPKEY . '&zoom=14¢er=' . $pointStr . 226. '&markers=' . $pointStr . ',red'; 227. } 228. else { 229. $imageurl .= '|' . $pointStr . ',smallred'; 230. } 231. } 232. else if (strcmp($status, "620") == 0) { 233. // sent geocodes too fast 234. echo "geocodes too fast"; 235. $delay += 100000; 236. } 237. else { 238. // failure to geocode 239. $geocode_pending = false; 240. // echo $status; 241. echo "didn't geocode"; 242. $errmsg = "Unable to find address " . $adr[$i]; 243. return; 244. } 245. usleep($delay); 246. } 247. } 248. } 249. 250. // ...successfully geocoded all addresses and we are now ready to do the stuff in will's map.php */ 251. //echo "about to echo imageurl "; 252. //echo $imageurl; 253. 254. $r = array("imageurl" => $imageurl, "lat" => $lat, "lng" => $lng, "index" => $index, "adr" => $adr, 255. "txt" => $txt, "minlat" => $minlat, "maxlat" => $maxlat, "minlng" => $minlng, "maxlng" => $maxlng, "url" => $url, 256. "addresses" => $addresses); 257. return ($r); 258. } 259. 260. 261. ?> 262. </body> 263. </html> 264. 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.