PHP RETS 库

2.6.4 2023-07-11 16:38 UTC

README

注意:如果您在寻找版本 1,请查看 "1.x" 分支。否则,强烈建议您使用版本 2+。

Latest Stable Version Total Downloads Build Status Documentation Status

ScreenShot

PHRETS

PHP 客户端库,用于与 RETS 服务器交互以拉取房地产列表、照片和其他数据,这些数据由 MLS 系统提供

<?php

date_default_timezone_set('America/New_York');

require_once("vendor/autoload.php");

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

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

// If you're using Monolog already for logging, you can pass that logging instance to PHRETS for some additional
// insight into what PHRETS is doing.
//
// $log = new \Monolog\Logger('PHRETS');
// $log->pushHandler(new \Monolog\Handler\StreamHandler('php://stdout', \Monolog\Logger::DEBUG));
// $rets->setLogger($log);

$connect = $rets->Login();

$system = $rets->GetSystemMetadata();
var_dump($system);

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

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

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

$fields = $rets->GetTableMetadata('Property', 'A');
var_dump($fields[0]);

$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) {
    var_dump($r);
}

简介

PHRETS 为 PHP 开发者提供了一种方法,可以直接在新或现有代码中集成 RETS 功能,为您处理以下方面:

  • 响应解析(XML、HTTP multipart 等)
  • 返回给开发者的简单变量、数组和对象
  • RETS 通信(通过 HTTP)
  • HTTP 头管理
  • 身份验证
  • 会话/cookie 管理
  • 支持 PHP 5.6、7.0、7.1 和 7.2

安装

开始使用最简单的方法是使用 Composer 安装 troydavisson/phrets

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

获取帮助

寻求帮助的最佳场所是我们 Slack 频道 或我们的 Google Group。请将 GitHub 的问题跟踪器用于库的bug。

免责声明

在许多情况下,此库提供的能力取决于您访问的 RETS 服务器是否正确实现了这些功能。RETS 规范定义了客户端和服务器之间的通信方式,如果服务器执行了意外的操作,则此库可能需要在调整一些选项后才能正常工作。

文档

配置

连接到 RETS 服务器的第一步是配置连接。

$config = new \PHRETS\Configuration;
$config->setLoginUrl($rets_login_url);
$config->setUsername($rets_username);
$config->setPassword($rets_password);

// optional.  value shown below are the defaults used when not overridden
$config->setRetsVersion('1.7.2'); // see constants from \PHRETS\Versions\RETSVersion
$config->setUserAgent('PHRETS/2.0');
$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

可用选项包括:

  • use_post_method - 总是使用 HTTP POST 而不是 HTTP GET。默认值为 false(使用 GET)
  • disable_follow_location - 禁用自动处理由服务器发送的重定向的能力。默认值为 false(跟随重定向)

作为替代,您还可以从数组加载配置选项

$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',
]);

连接

配置设置完成后,可以启动 RETS 会话

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

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

登录

$bulletin = $rets->Login();

这将向 RETS 服务器发送第一个请求。除了一般认证外,此步骤还要求最终确定会话的配置。在登录响应中,RETS 服务器提供了所有其他请求所需的信息,因此必须首先执行此操作。

获取记录

注意:为了从 RETS 服务器获取记录,您首先需要知道您允许获取和查看的信息类型。这些信息通过 RETS 服务器支持的元数据调用提供,但使用 RETS 元数据查看器服务(如 RETSMD.com)可以使此过程更快,除非您有解析元数据的具体需求。

有了已知的 RETS 资源、类和 DMQL 查询,您可以发出请求以获取记录

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

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

$results = $rets->Search(
    $resource,
    $class,
    $query,
    [
        'QueryType' => 'DMQL2',
        'Count' => 1, // count and records
        'Format' => 'COMPACT-DECODED',
        'Limit' => 99999999,
        'StandardNames' => 0, // give system names
    ]
);

处理结果

一个 $rets->Search() 请求的结果将返回一个 \PHRETS\Models\Search\Results 对象,它可以像常规数组一样被使用。该数组中的每个元素都是一个表示单个返回记录的 \PHRETS\Models\Search\Record 对象。

$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 进行 foreach 循环,但还存在一些额外的辅助方法。

// return an array of the field names to expect with each record
$results->getHeaders();

// return the total number of results found (reported by the RETS server)
$results->getTotalResultsCount();

// 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
$results->getMetadata();

// return whether or not the RETS server has more results to give
$results->isMaxRowsReached();

// return the string of characters to expect as the value of a field the RETS server blocked
$results->getRestrictedIndicator();

// return the first record in the set
$results->first();

// return the last record in the set
$results->last();

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

// export the results in CSV format
$results->toCSV();

// export the results in JSON format
$results->toJSON();

// export the results in a simple array format
$results->toArray();

由于每个 $record 是一个对象,因此存在一些辅助方法。

$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

下载媒体(照片、图像、文档等)。

$rets->GetObject() 调用返回的值是一个 \Illuminate\Support\Collection 对象,它允许使用许多常见的类似数组的功能以及一些辅助方法。

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

// grab the first object of the set
$objects->first();

// grab the last object of the set
$objects->last();

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

该集合中的每个对象都是一个具有自己一组辅助方法的 \PHRETS\Models\BaseObject 对象。

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

    // get the record ID associated with this object
    $object->getContentId();

    // get the sequence number of this object relative to the others with the same ContentId
    $object->getObjectId();

    // get the object's Content-Type value
    $object->getContentType();

    // get the description of the object
    $object->getContentDescription();

    // get the sub-description of the object
    $object->getContentSubDescription();

    // get the object's binary data
    $object->getContent();

    // get the size of the object's data
    $object->getSize();

    // does this object represent the primary object in the set
    $object->isPreferred();

    // when requesting URLs, access the URL given back
    $object->getLocation();

    // use the given URL and make it look like the RETS server gave the object directly
    $object->setContent(file_get_contents($object->getLocation()));
}