RETS library in PHP

Installs: 9 211

Dependents: 2

Stars: 191

Watchers: 61

Forks: 122

Open Issues: 24

Language: PHP

2.2 2015-08-10 22:54 UTC


Note: If you're looking for version 1, please see the "1.x" branch. Otherwise, it's highly recommended that you use version 2+.

Latest Stable Version Total Downloads Build Status Documentation Status


PHP client library for interacting with a RETS server to pull real estate listings, photos and other data made available from an MLS system




$log = new \Monolog\Logger('PHRETS');
$log->pushHandler(new \Monolog\Handler\StreamHandler('php://stdout', \Monolog\Logger::DEBUG));

$config = new \PHRETS\Configuration;
$config->setLoginUrl('rets login url here')
        ->setUsername('rets username here')
        ->setPassword('rets password here')

$rets = new \PHRETS\Session($config);

$connect = $rets->Login();

$system = $rets->GetSystemMetadata();

$resources = $system->getResources();
$classes = $resources->first()->getClasses();

$classes = $rets->GetClassesMetadata('Property');

$objects = $rets->GetObject('Property', 'Photo', '00-1669', '*', 1);

$fields = $rets->GetTableMetadata('Property', 'A');

$results = $rets->Search('Property', 'A', '*', ['Limit' => 3, 'Select' => 'LIST_1,LIST_105,LIST_15,LIST_22,LIST_87,LIST_133,LIST_134']);
foreach ($results as $r) {


PHRETS provides PHP developers a way to integrate RETS functionality directly within new or existing code by handling the following aspects for you:

  • Response parsing (XML, HTTP multipart, etc.)
  • Simple variables, arrays and objects returned to the developer
  • RETS communication (over HTTP)
  • HTTP Header management
  • Authentication
  • Session/Cookie management


The easiest way to get started is using Composer to install troydavisson/phrets:

    "require": {
        "troydavisson/phrets": "2.*"

Get Help

The best place to ask for help is in our Google Group. Please leave GitHub's issue tracker for bugs with the library.


In many cases, the capabilities provided by this library are dependent on these features being properly implemented by the RETS server you're accessing. The RETS specification defines how clients and servers communicate, and if a server is doing something unexpected, this library may not work without tweaking some options.



The first step with getting connected to a RETS server is to configure the connection.

$config = new \PHRETS\Configuration;

// optional.  value shown below are the defaults used when not overridden
$config->setRetsVersion('1.7.2'); // see constants from \PHRETS\Versions\RETSVersion
$config->setUserAgentPassword($rets_user_agent_password); // string password, if given
$config->setHttpAuthenticationMethod('digest'); // or 'basic' if required 
$config->setOption('use_post_method', false); // boolean
$config->setOption('disable_follow_location', false); // boolean

Available options are:

  • use_post_method - Always use HTTP POST instead of HTTP GET. Default is false (uses GET)
  • disable_follow_location - Disable the ability to automatically handle redirects sent by the server. Default is false (follow redirects)

As an alternative, you can also load configuration options from an array:

$config = Configuration::load([
    'login_url' => 'http://loginurlhere',
    'username' => 'rets_username',
    'password' => 'rets_password',
    'user_agent' => 'UserAgent/1.0',
    'user_agent_password' => 'user_agent_password_here',
    'rets_version' => '1.8',
    'http_authentication' => 'basic',

Once the configuration has been setup, a RETS session can be started:

$config = Configuration::load([
    see above for what to give here

$rets = new \PHRETS\Session($config);
$bulletin = $rets->Login();

This will make the first request to the RETS server. In addition to general authentication, this step is required to finalize the session's configuration. Within the Login response, RETS servers provide back information needed for all other requests, so this has to be done first.

Grabbing Records

Note: In order to grab records from a RETS server, you need to first know the types of information you're allowed to get and see. This information is provided through the metadata calls supported by a RETS server, but using a RETS metadata viewer service such as can make this process much faster unless you have a specific need for having parseable metadata.

With a known RETS Resource, Class and DMQL query, you can issue requests for records:

$results = $rets->Search($resource, $class, $query);

// or with the additional options (with defaults shown)

$results = $rets->Search(
        'QueryType' => 'DMQL2',
        'Count' => 1, // count and records
        'Format' => 'COMPACT-DECODED',
        'Limit' => 99999999,
        'StandardNames' => 0, // give system names
Processing Results

The result of a $rets->Search() request will return a \PHRETS\Models\Search\Results object which can be used, in many ways, like a regular array. Each item in that array is a \PHRETS\Models\Search\Record object representing a single record returned.

$results = $rets->Search( see above for what to give here );

foreach ($results as $record) {
    echo $record['Address'] . "\n";
    // is the same as:
    echo $record->get('Address') . "\n";

$results can be used in a foreach loop like above, but some additional helper methods exist:

// return an array of the field names to expect with each record

// return the total number of results found (reported by the RETS server)

// return the count of results actually retrieved by PHRETS
$results->getReturnedResultsCount(); // same as: count($results)

// make a RETS GetMetadata call for the metadata for this RETS Resource and Class

// return whether or not the RETS server has more results to give

// return the string of characters to expect as the value of a field the RETS server blocked

// return the first record in the set

// return the last record in the set

// returns an array representing the collected values from the identified field
$all_ids = $results->lists('ListingID');

// export the results in CSV format

// export the results in JSON format

// export the results in a simple array format

Because each $record is an object, some helper methods exist:

$record->isRestricted('Address'); // determine if the RETS server blocked this value
$record->getFields(); // return an array of the field names associated with this record
$record->toArray(); // returns a true PHP array of the given record
$record->toJson(); // returns a JSON encoded string representing the record
$record->getResource(); // returns the RETS Resource responsible for this record
$record->getClass(); // returns the RETS Class responsible for this record
Downloading Media (Photos, Images, Documents, etc.)

The returned value from a $rets->GetObject() call is a \Illuminate\Support\Collection object which allows many common array-like features as well as some helper methods.

$objects = $rets->GetObject($rets_resource, $object_type, $object_keys);

// grab the first object of the set

// grab the last object of the set

// throw out everything but the first 10 objects
$objects = $objects->slice(0, 10);

Each object within that collection is a \PHRETS\Models\Object object with it's own set of helper methods:

$objects = $rets->GetObject( see above documentation );
foreach ($objects as $object) {
    // does this represent some kind of error
    $object->getError(); // returns a \PHRETS\Models\RETSError

    // get the record ID associated with this object

    // get the sequence number of this object relative to the others with the same ContentId

    // get the object's Content-Type value

    // get the description of the object

    // get the sub-description of the object

    // get the object's binary data

    // get the size of the object's data

    // does this object represent the primary object in the set

    // when requesting URLs, access the URL given back

    // use the given URL and make it look like the RETS server gave the object directly