Search code examples
phrets

Property Photo Files with PHRETS v2


My php code, below, attemps to download all the photos for a property listing. It successfully queries the RETS server, and creates a file for each photo, but the file does not seem to be a functional image. (MATRIX requires files to be downloaded, instead of URLs.)

The list of photos below suggests that it successfully queries one listing id (47030752) for all photos that exist, (20 photos in this case). In a web browser, the files appear only as a small white square on a black background: e.g. (https://photos.atlantarealestate-homes.com/photos/PHOTO-47030752-9.jpg). The file size (4) also seems to be very low, as compared to that of a real photo.

du -s PHOTO*
4   PHOTO-47030752-10.jpg
4   PHOTO-47030752-11.jpg
4   PHOTO-47030752-12.jpg
4   PHOTO-47030752-13.jpg
4   PHOTO-47030752-14.jpg
4   PHOTO-47030752-15.jpg
4   PHOTO-47030752-16.jpg
4   PHOTO-47030752-17.jpg
4   PHOTO-47030752-18.jpg
4   PHOTO-47030752-19.jpg
4   PHOTO-47030752-1.jpg
4   PHOTO-47030752-20.jpg
4   PHOTO-47030752-2.jpg
4   PHOTO-47030752-3.jpg
4   PHOTO-47030752-4.jpg
4   PHOTO-47030752-5.jpg
4   PHOTO-47030752-6.jpg
4   PHOTO-47030752-7.jpg
4   PHOTO-47030752-8.jpg
4   PHOTO-47030752-9.jpg

script I'm using:

#!/usr/bin/php

<?php

date_default_timezone_set('this/area');

require_once("composer/vendor/autoload.php");

$config = new \PHRETS\Configuration;

$config->setLoginUrl('https://myurl/login.ashx')
        ->setUsername('myser')
        ->setPassword('mypass')
        ->setRetsVersion('1.7.2');

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

$connect = $rets->Login();

$system = $rets->GetSystemMetadata();

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

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

$host="localhost";
$user="db_user";
$password="db_pass";
$dbname="db_name";
$tablename="db_table";

$link=mysqli_connect ($host, $user, $password, $dbname);

$query="select mlsno, matrix_unique_id, photomodificationtimestamp from fmls_homes left join fmls_images on (matrix_unique_id=mls_no and photonum='1') where photomodificationtimestamp <> last_update or last_update is null limit 1";

print ("$query\n");
$result= mysqli_query ($link, $query);
$num_rows = mysqli_num_rows($result);

print "Fetching Images for $num_rows Homes\n";

while ($Row= mysqli_fetch_array ($result))  {
$matrix_unique_id="$Row[matrix_unique_id]";

$objects = $rets->GetObject('Property', 'LargePhoto', $matrix_unique_id);

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

        $listing = $object->getContentId();
        $number = $object->getObjectId();
        $url = $object->getLocation();
        //$photo = $object->getContent();
        $size = $object->getSize();
        $desc = $object->getContentDescription();

if ($number >= '1') {

file_put_contents("/bigdirs/fmls_pics/PHOTO-{$listing}-{$number}.jpg", "$object->getContent();");

print "$listing - $number - $size $desc\n";

} //end if

} //end foreach

} //end while
mysqli_close ($link);

fclose($f);

php?>
                                                                                                         

Are there any suggested changes to capture photos into the created files? This command creates the photo files:

file_put_contents("/bigdirs/fmls_pics/PHOTO-{$listing}-{$number}.jpg", "$object->getContent();");

There may be some parts of this script that wouldn't work in live production, but are sufficient for testing. This script seems to successfully query for the information needed from the RETS server. The problem is just simply that the actual files created do not seem to be functional photos.

Thanks in Advance! :)


Solution

  • Your code sample is a mix of the official documentation and a usable implementation. The problem is with this line:

    $object->setContent(file_get_contents($object->getLocation()));

    You should completely take that out. That's actually overriding the image you downloaded with nothing before you get a chance to save the contents to a file. With that removed, it should work as expected.