Skip to content

Connect, download listing data in csv format, disconnect using auto offset feature

Troy Davisson edited this page Mar 5, 2016 · 2 revisions

Note: This page documents capabilities available in the older 1.x version. Please see this repository's README file for the new 2.x version documentation.

<?php

$rets_login_url = "http://rets_login_url";
$rets_username = "rets_username";
$rets_password = "rets_password";

// use http://retsmd.com to help determine the SystemName of the DateTime field which
// designates when a record was last modified
$rets_modtimestamp_field = "LIST_87";

// use http://retsmd.com to help determine the names of the classes you want to pull.
// these might be something like RE_1, RES, RESI, 1, etc.
$property_classes = array("A","B","C");

// DateTime which is used to determine how far back to retrieve records.
// using a really old date so we can get everything
$previous_start_time = "1980-01-01T00:00:00";

//////////////////////////////

require_once("phrets.php");

// start rets connection
$rets = new phRETS;

// only enable this if you know the server supports the optional RETS feature called 'Offset'
$rets->SetParam("offset_support", true);

echo "+ Connecting to {$rets_login_url} as {$rets_username}<br>\n";
$connect = $rets->Connect($rets_login_url, $rets_username, $rets_password);

if ($connect) {
        echo "  + Connected<br>\n";
}
else {
        echo "  + Not connected:<br>\n";
        print_r($rets->Error());
        exit;
}

foreach ($property_classes as $class) {

        echo "+ Property:{$class}<br>\n";

        $file_name = strtolower("property_{$class}.csv");
        $fh = fopen($file_name, "w+");

        $fields_order = array();

        $query = "({$rets_modtimestamp_field}={$previous_start_time}+)";

        // run RETS search
        echo "   + Resource: Property   Class: {$class}   Query: {$query}<br>\n";
        $search = $rets->SearchQuery("Property", $class, $query, array('Limit' => 1000));

        if ($rets->NumRows($search) > 0) {

                // print filename headers as first line
                $fields_order = $rets->SearchGetFields($search);
                fputcsv($fh, $fields_order);

                // process results
                while ($record = $rets->FetchRow($search)) {
                        $this_record = array();
                        foreach ($fields_order as $fo) {
                                $this_record[] = $record[$fo];
                        }
                        fputcsv($fh, $this_record);
                }

        }

        echo "    + Total found: {$rets->TotalRecordsFound($search)}<br>\n";

        $rets->FreeResult($search);

        fclose($fh);

        echo "  - done<br>\n";

}

echo "+ Disconnecting<br>\n";
$rets->Disconnect();