We recommending using the PHRETS library by troydavisson, found for free on GitHub here: https://github.com/troydavisson/PHRETS
Sample.php
<?php
// Using "PHRETS" by troydavisson: https://github.com/troydavisson/PHRETS
require_once('vendor/autoload.php');
// Login to MLS IDX API
$config = new \PHRETS\Configuration;
$config->setLoginUrl('https://sample.mlsidxapi.com/Login.svc/Login')
->setUsername('aQgXjDxarQcxFqqIrmfeTvuk')
->setPassword('JNTHKsuoXySgTZZWJGZkipSP')
->setHttpAuthenticationMethod('basic')
->setRetsVersion('1.7.2');
$rets = new \PHRETS\Session($config);
$rets->Login();
function getSearchResultsArray($dbml, $limit = 100, $offset = 0, $format = 'STANDARD-XML') {
global $rets;
if ($offset) $results = $rets->Search('Property', 'Property', $dbml, [ 'Count' => 1, 'Format' => $format, 'Limit' => $limit, 'Offset' => $offset ]);
else $results = $rets->Search('Property', 'Property', $dbml, [ 'Count' => 1, 'Format' => $format, 'Limit' => $limit ]);
$parser = new \PHRETS\Parsers\XML;
$xml = $parser->parse($rets->getLastResponse());
// Convert to PHP array
return json_decode(json_encode($xml, true), JSON_PRETTY_PRINT);
}
function addResultToDB($listing) {
// Save results to your database
//...
}
function addAllResultsToDB($results) {
$addedCount = 0;
$propertyDetails = $results['RETS-RESPONSE']['PropertyDetails'];
// NOTE: You must make your own "addResultToDB" function
// Only one result, so force into array
if (!$propertyDetails[0]) $addedCount += addResultToDB($propertyDetails);
// Add a bunch at once
else foreach($propertyDetails as $listing) $addedCount += addResultToDB($listing);
return $addedCount;
}
// Search the last 5 hours
$dbml = "(LastUpdated=" . date('Y-m-d\TH:i:sP', strtotime('-5 Hours')) . ")";
// Set to Zulu time
$dbml = str_replace('+00:00', 'Z', $dbml);
$limit = 100;
$offset = 0;
$totalRecords = 0;
$totalRecordsAdded = 0;
$totalRecordsReturned = 0;
do {
// Get results in simple PHP array
$results = getSearchResultsArray($dbml, $limit, $offset);
$totalRecords = $results['RETS-RESPONSE']['Pagination']['TotalRecords'];
// Discontinue now if no records found
if (!$totalRecords) break;
// Save results to your database
$totalRecordsAdded += addAllResultsToDB($results);
$recordsReturned = $results['RETS-RESPONSE']['Pagination']['RecordsReturned'];
$totalRecordsReturned += $recordsReturned;
$offset = $results['RETS-RESPONSE']['Pagination']['Offset'] + $recordsReturned;
} while ($totalRecords > $totalRecordsAdded);