I'm using version 2 of PHRETS and while looping through Listing ID's I need to download images. Not all listings have images. In the previous version 1 there was an option for if ($photo['Success'] == true)
Here is my php code in a function. I've noted that getContentType() will display application/xml; charset=utf-8 for listings that do not have images and image/jpeg for ones that do. Also, $photos->first()->getObjectId(); will be blank for listings with no images. Given this info I presume I can write a simple conditional to check. But my question is, is there any official way to do this?
function downloadImages($ListingKey,$rets){
$photos = $rets->GetObject('Property','Photo',$ListingKey);
echo $photos->first()->getContentId();
echo $photos->first()->getObjectId();
echo $photos->first()->getContentType();
echo $photos->first()->getContentDescription();
echo $photos->first()->getContentSubDescription();
echo $photos->first()->getSize();
echo $photos->first()->isPreferred();
echo $photos->first()->getLocation();
}
This is my current conditional check
function downloadImages($listingKey,$rets){
$photos = $rets->GetObject('Property','Photo',$listingKey);
if($photos->first()->getContentType()=='image/jpeg'){
mkdir('/home/server/public_html/img/'.$listingKey,0777);
foreach ($photos as $p) {
file_put_contents('/home/server/public_html/img/'.$listingKey.'/'.$p->getContentId().'-'.$p->getObjectId().'.jpg',$p->getContent());
}
}
}
See if your server supports getobject's
Location
parameter. Check the docs for how to pass location into the GetObject method here: https://github.com/troydavisson/PHRETS/wiki/GetObject. Sometimes you can pass in location=1 and get back the image count along with URLs to any images. Also, sometimes the MLS will have a NumberOfPhotos field with the listing data although I've seen cases where that field won't be current. Lastly, some systems have a media resource you can query just like the listing/property resource using the mls id of the listing. Unfortunately, it depends on what features the backend RETS server provides. There's nothing wrong with what you're doing but these other methods might be more efficient.EDIT: Here's the data dictionary field for number of photos: https://ddwiki.reso.org/display/DDW17/PhotosCount+Field