Advertisement
Guest User

rets query

a guest
Jun 26th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. <?PHP
  2.  
  3. /* Script Variables */
  4. // Lots of output, saves requests to a local file.
  5. $debugMode = false;
  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 = "-2 years";
  8.  
  9. /* RETS Variables */
  10. require("PHRets_CREA.php");
  11. $RETS = new PHRets();
  12. $RETSURL = "http://data.crea.ca/Login.svc/Login";
  13. $RETSUsername = "";
  14. $RETSPassword = "";
  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", "/var/web/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, '*');
  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. /* @TODO SAVE THIS PHOTO TO YOUR PHOTOS FOLDER
  68. * Easiest option:
  69. * file_put_contents($destination, $photoData);
  70. * http://php.net/function.file-put-contents
  71. */
  72.  
  73. $count++;
  74. }
  75.  
  76. if($debugMode)
  77. error_log("Downloaded ".$count." Images For '".$listingID."'");
  78. }
  79. elseif($debugMode)
  80. error_log("No Images For '".$listingID."'");
  81.  
  82. // For good measure.
  83. if(isset($photos)) $photos = null;
  84. if(isset($photo)) $photo = null;
  85. }
  86.  
  87. /* NOTES
  88. * With CREA, You have to ask the RETS server for a list of IDs.
  89. * Once you have these IDs, you can query for 100 listings at a time
  90. * Example Procedure:
  91. * 1. Get IDs (500 Returned)
  92. * 2. Get Listing Data (1-100)
  93. * 3. Get Listing Data (101-200)
  94. * 4. (etc)
  95. * 5. (etc)
  96. * 6. Get Listing Data (401-500)
  97. *
  98. * Each time you get Listing Data, you want to save this data and then download it's images...
  99. */
  100.  
  101. error_log("-----GETTING ALL ID's-----");
  102. $DBML = "(LastUpdated=" . date('Y-m-d', strtotime($TimeBackPull)) . ")";
  103. $params = array("Limit" => 1, "Format" => "STANDARD-XML", "Count" => 1);
  104. $results = $RETS->SearchQuery("Property", "Property", $DBML, $params);
  105. $totalAvailable = $results["Count"];
  106. error_log("-----".$totalAvailable." Found-----");
  107. if(empty($totalAvailable) || $totalAvailable == 0)
  108. error_log(print_r($RETS->GetLastServerResponse(), true));
  109. for($i = 0; $i < ceil($totalAvailable / $RETS_LimitPerQuery); $i++)
  110. {
  111. $startOffset = $i*$RETS_LimitPerQuery;
  112.  
  113. error_log("-----Get IDs For ".$startOffset." to ".($startOffset + $RETS_LimitPerQuery).". Mem: ".round(memory_get_usage()/(1024*1024), 1)."MB-----");
  114. $params = array("Limit" => $RETS_LimitPerQuery, "Format" => "STANDARD-XML", "Count" => 1, "Offset" => $startOffset);
  115. $results = $RETS->SearchQuery("Property", "Property", $DBML, $params);
  116. foreach($results["Properties"] as $listing)
  117. {
  118. $listingID = $listing["@attributes"]["ID"];
  119. if($debugMode) error_log($listingID);
  120.  
  121. /* @TODO Handle $listing array. Save to Database? */
  122.  
  123. /* @TODO Uncomment this line to begin saving images. Refer to function at top of file */
  124. //downloadPhotos($listingID);
  125. }
  126. }
  127.  
  128. $RETS->Disconnect();
  129.  
  130. /* This script, by default, will output something like this:
  131.  
  132. Connecting to RETS as '[YOUR RETS USERNAME]'...
  133. -----GETTING ALL ID's-----
  134. -----81069 Found-----
  135. -----Get IDs For 0 to 100. Mem: 0.7MB-----
  136. -----Get IDs For 100 to 200. Mem: 3.7MB-----
  137. -----Get IDs For 200 to 300. Mem: 4.4MB-----
  138. -----Get IDs For 300 to 400. Mem: 4.9MB-----
  139. -----Get IDs For 400 to 500. Mem: 3.4MB-----
  140. */
  141.  
  142. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement