PHRets: Using PHP to download real estate listing photos

AME picture AME · Jun 7, 2013 · Viewed 7.2k times · Source

Am a trying to create a PHP (PHrets) script that downloads all real estate listing information from a specific area and saves all of the listings data (CSV file and photos) on my web server.

Note: A single listing may have up to 20 photos.

I am using PHrets to retrieve MLS listing data and it works great for creating a CSV of data. However, I would like to modify this code to loop through each listing's photos and download them onto my web server with the following name convention: MLSID-PHOTOID.

The error below is coming from the GetObject loop at the end of the code: ERROR Message: HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.

Thanks in advance!

    <?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();

Answer

Andrew Briggs picture Andrew Briggs · Jun 9, 2013

I modified your while loop that processes each record to also get the photos for that record. I also tested this on a RETS server.

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);    
}

One thing I found is you have ListingID on this line:

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

But then in your search query you refer to ListingID as L_ListingID. Maybe the above line should have 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));