PHRets:使用PHP下载房地产列表照片

时间:2013-06-07 18:17:06

标签: php rets

尝试创建一个PHP(PHrets)脚本,从特定区域下载所有房地产列表信息,并将所有列表数据(CSV文件和照片)保存在我的Web服务器上。

注意:单个商家信息最多可包含20张照片。

我正在使用PHrets检索MLS列表数据,它非常适合创建数据CSV。但是,我想修改此代码以遍历每个列表的照片,并使用以下名称约定将其下载到我的Web服务器上:MLSID-PHOTOID。

以下错误来自代码末尾的GetObject循环: 错误消息:HTTP错误500(内部服务器错误):服务器尝试完成请求时遇到意外情况。

提前致谢!

    <?php

$rets_login_url = "http://maxebrdi.rets.fnismls.com/Rets/FNISRETS.aspx/MAXEBRDI/login?&rets-version=rets/1.5";
$rets_username = "MyUser";
$rets_password = "MyPass";

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

// 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.
//"RE_1"
$property_classes = array("LR_5");

// DateTime which is used to determine how far back to retrieve records.
// using a really old date so we can get everything
$listing_status = 1;

$listing_city = "OAKLAND";

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

require_once("phrets.php");

// start rets connection
$rets = new phRETS;

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+");

        $maxrows = true;
        $offset = 1;
        $limit = 1000;
        $fields_order = array();

        while ($maxrows) {

                $query = "({$rets_status_field}={$listing_status}),({$rets_city_field}={$listing_city})";

                // run RETS search
                echo "   + Query: {$query}  Limit: {$limit}  Offset: {$offset}<br>\n";
                $search = $rets->SearchQuery("Property", $class, $query, array("Limit" => $limit, "Offset" => $offset, "Format" => "COMPACT", "Select" => "L_ListingID,L_Class,L_Type_,L_Status,L_AskingPrice,L_Keyword2,L_Keyword3,L_Keyword4,L_SquareFeet,L_Remarks,L_Address,L_City,L_State,LO1_OrganizationName,LA1_AgentLicenseID,LA1_UserFirstName,LA1_UserLastName,L_PictureCount", "Count" => 1));

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

                        if ($offset == 1) {
                                // 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);
                        }

                        $offset = ($offset + $rets->NumRows());

                }

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

                $rets->FreeResult($search);
        }

        fclose($fh);
        echo "  - done<br>\n";

}

/*
//This code needs to be fixed. Not sure how to include the listing array correctly.
$photos = $rets->GetObject("Property", "Photo", $record[ListingID], "*", 1);
foreach ($photos as $photo) {
        $listing = $photo['Content-ID'];
        $number = $photo['Object-ID'];

        if ($photo['Success'] == true) {
                echo "{$listing}'s #{$number} photo is at {$photo['Location']}\n";
        file_put_contents("photos/{$photo['Content-ID']}-{$photo['Object-ID']}.jpg",
        $photo['Data']);                
        }
        else {
        echo "Error ({$photo['Content-ID']}-{$photo['Object-ID']}
        }
               }

//ERROR Message: HTTP Error 500 (Internal Server Error): An unexpected condition was //encountered while the server was attempting to fulfill the request.  
*/                              
echo "+ Disconnecting<br>\n";
$rets->Disconnect();

2 个答案:

答案 0 :(得分:3)

我修改了处理每条记录的while循环,以获取该记录的照片。我还在RETS服务器上对此进行了测试。

while ($record = $rets->FetchRow($search)) {
        $this_record = array();
        foreach ($fields_order as $fo) {

        if ($fo == 'L_ListingID') {

            $photos = $rets->GetObject("Property", "Photo", $record[$fo], "*", 1);

            foreach ($photos as $photo) {

                if ($photo['Success'] == true) {

                     file_put_contents("photos/{$photo['Content-ID']}-{$photo['Object-ID']}.jpg", $photo['Data']);   

                }   
            }
        }
      $this_record[] = $record[$fo];
    }                           
fputcsv($fh, $this_record);    
}

我发现有一件事就是你在这一行上有了ListingID:

$photos = $rets->GetObject("Property", "Photo", $record[ListingID], "*", 1);

但是在搜索查询中,您将ListingID称为L_ListingID。也许上面的行应该有L_ListingID。

$search = $rets->SearchQuery("Property", $class, $query, array("Limit" => $limit, "Offset" => $offset, "Format" => "COMPACT", "Select" => "L_ListingID,L_Class,L_Type_,L_Status,L_AskingPrice,L_Keyword2,L_Keyword3,L_Keyword4,L_SquareFeet,L_Remarks,L_Address,L_City,L_State,LO1_OrganizationName,LA1_AgentLicenseID,LA1_UserFirstName,LA1_UserLastName,L_PictureCount", "Count" => 1));

答案 1 :(得分:0)

如果GetObject的最后一个参数是&#34; 0&#34; :

  

$ photos = $ rets-&gt; GetObject(&#34; Property&#34;,&#34; Photo&#34;,$ record [$ fo],&#34; *&#34;,1) ;

在这种情况下&#34; 1&#34;是为了检索位置。一些服务器被关闭并返回我认为的实际图像 - 这将需要一种不同的方法。