Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 KB | None | 0 0
  1. <?PHP
  2.  
  3. /* Script Variables */
  4. // Lots of output, saves requests to a local file.
  5. $debugMode = true;
  6. // Initially, you should set this to something like "-2 years". Once you have all day, change this to "-48 hours" or so to pull incremental data
  7. $TimeBackPull = "-48 hours";
  8.  
  9. /* RETS Variables */
  10. require("PHRets_CREA.php");
  11. $RETS = new PHRets();
  12. $RETSURL = "http://data.crea.ca/Login.svc/Login";
  13. $RETSUsername = "aZ3mHDsE3VbW6YLOHduxTZgE";
  14. $RETSPassword = "Fw6peQzvs07yTsi2Hu6wdPyk";
  15. $RETS->Connect($RETSURL, $RETSUsername, $RETSPassword);
  16. $RETS->AddHeader("RETS-Version", "RETS/1.7.2");
  17. $RETS->AddHeader('Accept', '/');
  18. $RETS->SetParam('compression_enabled', true);
  19. $RETS_PhotoSize = "LargePhoto";
  20. $RETS_LimitPerQuery = 100;
  21. if($debugMode /* DEBUG OUTPUT */)
  22. {
  23. //$RETS->SetParam("catch_last_response", true);
  24. $RETS->SetParam("debug_file", "CREA_Anthony.txt");
  25. $RETS->SetParam("debug_mode", true);
  26. }
  27.  
  28. function downloadPhotos($listingID)
  29. {
  30. global $RETS, $RETS_PhotoSize, $debugMode;
  31.  
  32. if(!$downloadPhotos)
  33. {
  34. if($debugMode) error_log("Not Downloading Photos");
  35. return;
  36. }
  37.  
  38. $photos = $RETS->GetObject("Property", $RETS_PhotoSize, $listingID, '*', 0);
  39.  
  40. if(!is_array($photos))
  41. {
  42. if($debugMode) error_log("Cannot Locate Photos");
  43. return;
  44. }
  45.  
  46. if(count($photos) > 0)
  47. {
  48. $count = 0;
  49. foreach($photos as $photo)
  50. {
  51. if(
  52. (!isset($photo['Content-ID']) || !isset($photo['Object-ID']))
  53. ||
  54. (is_null($photo['Content-ID']) || is_null($photo['Object-ID']))
  55. ||
  56. ($photo['Content-ID'] == 'null' || $photo['Object-ID'] == 'null')
  57. )
  58. {
  59. continue;
  60. }
  61.  
  62. $listing = $photo['Content-ID'];
  63. $number = $photo['Object-ID'];
  64. $destination = $listingID."_".$number.".jpg";
  65. $photoData = $photo['Data'];
  66.  
  67. //My code
  68. file_put_contents($destination, $photoData);
  69.  
  70. /* @TODO SAVE THIS PHOTO TO YOUR PHOTOS FOLDER
  71. * Easiest option:
  72. * file_put_contents($destination, $photoData);
  73. * http://php.net/function.file-put-contents
  74. */
  75.  
  76. $count++;
  77. }
  78.  
  79. if($debugMode)
  80. error_log("Downloaded ".$count." Images For '".$listingID."'");
  81. }
  82. elseif($debugMode)
  83. error_log("No Images For '".$listingID."'");
  84.  
  85. // For good measure.
  86. if(isset($photos)) $photos = null;
  87. if(isset($photo)) $photo = null;
  88. }
  89.  
  90. /* NOTES
  91. * With CREA, You have to ask the RETS server for a list of IDs.
  92. * Once you have these IDs, you can query for 100 listings at a time
  93. * Example Procedure:
  94. * 1. Get IDs (500 Returned)
  95. * 2. Get Listing Data (1-100)
  96. * 3. Get Listing Data (101-200)
  97. * 4. (etc)
  98. * 5. (etc)
  99. * 6. Get Listing Data (401-500)
  100. *
  101. * Each time you get Listing Data, you want to save this data and then download it's images...
  102. */
  103.  
  104. error_log("-----GETTING ALL ID's-----");
  105. $DBML = "(LastUpdated=" . date('Y-m-d', strtotime($TimeBackPull)) . ")";
  106. $params = array("Limit" => 1, "Format" => "STANDARD-XML", "Count" => 1);
  107. $results = $RETS->SearchQuery("Property", "Property", $DBML, $params);
  108. $totalAvailable = $results["Count"];
  109. error_log("-----".$totalAvailable." Found-----");
  110. if(empty($totalAvailable) || $totalAvailable == 0)
  111. error_log(print_r($RETS->GetLastServerResponse(), true));
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. $servername = "****";
  120. $username = "****";
  121. $password = "****";
  122. $dbname = "****";
  123.  
  124. // Create connection
  125. $conn = new mysqli($servername, $username, $password, $dbname);
  126. // Check connection
  127. if ($conn->connect_error) {
  128. die("Connection failed: " . $conn->connect_error);
  129. }
  130.  
  131.  
  132. for($i = 0; $i < ceil($totalAvailable / $RETS_LimitPerQuery); $i++)
  133. {
  134. $startOffset = $i*$RETS_LimitPerQuery;
  135.  
  136. error_log("-----Get IDs For ".$startOffset." to ".($startOffset + $RETS_LimitPerQuery).". Mem: ".round(memory_get_usage()/(1024*1024), 1)."MB-----");
  137. $params = array("Limit" => $RETS_LimitPerQuery, "Format" => "STANDARD-XML", "Count" => 1, "Offset" => $startOffset);
  138. $results = $RETS->SearchQuery("Property", "Property", $DBML, $params);
  139.  
  140.  
  141.  
  142. foreach($results["Properties"] as $listing)
  143. {
  144.  
  145.  
  146. $address = $listing['Address']['StreetAddress'];
  147. $price = $listing['Price'];
  148. $bedrooms = $listing['Building']['BedroomsTotal'];
  149. $bath = $listing['Building']['BathroomTotal'];
  150. $sqft = $listing['Building']['SizeInterior'];
  151. $sqft_land = $listing['SizeTotalText'];
  152. if($sqft_land == 'Array' ){$sqft_land='';}
  153. $constructed = $listing['Building']['ConstructedDate'];
  154. $stories = $listing['Building']['StoriesTotal'];
  155. $type = $listing['PropertyType'];
  156. $city = $listing['Address']['City'];
  157. $province = $listing['Address']['Province'];
  158. $postal = $listing['Address']['PostalCode'];
  159. $building_amenities = $listing['Building']['Amenities'];
  160. $land_amenities = $listing['Land']['Amenities'];
  161. $ammenities_near_by = $listing['AmmenitiesNearBy'];
  162. $last_updated = $listing['@attributes']['LastUpdated'];
  163.  
  164. $images = $listing['Photo']['PropertyPhoto'];
  165.  
  166.  
  167.  
  168.  
  169.  
  170. $listingID = $listing["@attributes"]["ID"];
  171. if($debugMode) error_log($listingID);
  172.  
  173. $sql = "INSERT INTO listings (StreetAddress, Price, BedroomsTotal, BathroomTotal, SizeInterior, SizeTotalText, ListingID, ConstructedDate, StoriesTotal, PropertyType, City, Province, PostalCode, BuildingAmenities, LandAmenities, AmmenitiesNearBy, LastUpdated) VALUES ('$address', '$price', '$bedrooms', '$bath', '$sqft', '$sqft_land', '$listingID', '$constructed', '$stories', '$type', '$city', '$province', '$postal', '$building_amenities', '$land_amenities', '$ammenities_near_by', '$last_updated')";
  174.  
  175. if ($conn->query($sql) === TRUE) {
  176. // echo "New record created successfully";
  177. }
  178.  
  179. foreach($images as $img){
  180. $image = $listingID.'_'.$img['SequenceId'].'.jpg';
  181. $sql = "INSERT INTO photos (filename, listing_id) VALUES ('$image', '$listingID')";
  182. if ($conn->query($sql) === TRUE) {
  183. // echo "New record created successfully";
  184. }
  185. }
  186.  
  187. downloadPhotos($listingID);
  188. }
  189. }
  190.  
  191.  
  192.  
  193. $conn->close();
  194.  
  195.  
  196.  
  197. $RETS->Disconnect();
  198.  
  199. /* This script, by default, will output something like this:
  200.  
  201. Connecting to RETS as '[YOUR RETS USERNAME]'...
  202. -----GETTING ALL ID's-----
  203. -----81069 Found-----
  204. -----Get IDs For 0 to 100. Mem: 0.7MB-----
  205. -----Get IDs For 100 to 200. Mem: 3.7MB-----
  206. -----Get IDs For 200 to 300. Mem: 4.4MB-----
  207. -----Get IDs For 300 to 400. Mem: 4.9MB-----
  208. -----Get IDs For 400 to 500. Mem: 3.4MB-----
  209. */
  210.  
  211. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement