Buying Buddy PHP Class and API

This advanced topic discusses how the Buying Buddy PHP class file can be used to interact with our API.

The goal of this method is to provide supporting META data capabilities to help with SEO (for example) on a listing, and also provide field value lists to help with custom forms.

This PHP class provides customizable server pre-rendering of pages to make a website SEO index-able in a straightforward way.
The PHP Class essentially performs the same same function as the Buying Buddy plugin, but with one HUGE difference for SEO, which is that it writes HTML before the page loads. So, without the PHP Class you will not have indexable content.

This PHP class is not a replacement for widgets, it provides function to add SEO support to widgets.

You cannot use this PHP class to run queries and get data to populate your own designs. It is not designed for this purpose AND this will be a violation of your MLS IDX rules as the data license agreements with the MLS do not permit this.

The use of this PHP class is optional. It is ONLY required if you wish to extend the function of the Buying Buddy JavaScript plugin to also provide SEO benefits.

Overview

If you have a PHP website, then this PHP method can work in conjunction with the Buying Buddy JavaScript plugin, enabling the additional following SEO features:

Indexable Listings: The display widgets (List Widget, Gallery Widget - but not results widget) will output HTML code that includes links to listings that can be followed by Search Engines

Long Tail URLs: The structure of the property details page will NOT include a question mark

Details Page Optimization: The Title and Meta Description tags on property details pages will be written to include the property address

Once installed, the PHP Class and API executes and presets values before the HTML is output for a page in the browser.
This method therefore allows you to access and use property-specific PHP variables to set SEO options on a property details page such as Meta Tags, OG Tags and so on.

To use this feature you will need your Buying Buddy API-Authcode, which is available in your Account Settings

Note:
This PHP class is not supported for use on a WordPress Website. It may be technically possible to use this PHP Class and API rather than the Buying Buddy WordPress Plugin, but currently this is not supported.

Requirements

  1. Curl must be enabled on server.
  2. A special "cache" directory must be made available with "write" access.
  3. You must be using the latest version of the Buying Buddy Plugin script - this is available from the Install Widgets pages in your Buying Buddy dashboard.
  4. Make sure you have the following from your Buying Buddy account:
    1. Your Buying Buddy API-Authcode for Programming APIs
      Go to: My Account > Account Settings > Preferences
    2. Your Account Activation Key
      Go to: Website & Widgets > Getting Started - see Key at bottom of page
  5. If you are using the PHP class, then you must apply it to all widgets.

Getting Started

1. Download the API Code ZIP FILE

Menu Widgets > Installation

If your Website is set as a type that supports PHP (e.g. php, joomla etc) then scroll down on the installation page to see the dowload link for the PHP class zip file.

This file includes:

  • MBB folder containing the MyBuyingBuddy.php class file and the cache directory

2. Implementation Instructions

2a. Preparation

All pages must load the latest Buying Buddy Plugin Script (see instructions).
This is available from your Buying Buddy account.
Go to: Websites & Widgets > Plugin Installation

You MUST change the Buying Buddy plugin script seo variable to be "true" as shown below.

var MBB = { seo : "true", data:{ acid : "[ACTIVATION_KEY]" } };

IMPORTANT
The Buying Buddy Plugin script parameter for "seo : true" must be set. This will then utilize the PHP class, which will also mean that every widget must use the PHP Object to get the HTML code.

2b. Add PHP on Pages with Widgets

Include the following lines of PHP on each page where you intend to use a Buying Buddy widget or Buying Buddy functions:

<?php
require 'mbb/MyBuyingBuddy.php';
/* Enter the your API-Authcode for Programming APIs, and Account Activation Key */
$mbbObj = new MyBuyingBuddy( array(
    "api_key" => "[YOUR_API_AUTHCODE]",
    "acid" => "[ACTIVATION_KEY]"
    )
);
?>

2c. Add PHP and HTML for specific Widgets on Each page

When using this PHP class you MUST add Widgets using the following modified approach.

For each widget on a page, you must add:

  1. $widgetParams array for the widget
  2. HTML for the Widget that includes a PHP object to load the HTML
  3. Display widgets should always include a filter in the DIV regardless of any code you may use to filter in PHP

Set variables for SEO as desired using the various PHP objects.

See the Code Reference at the bottom of this article for examples.

Using MBB Objects

For SEO benefits, you can set the values of various META tags using PHP objects. These are:

  • $propertyObj->meta_description
  • $propertyObj->meta_keywords
  • $propertyObj->title
  • $propertyObj->h1
  • $propertyObj->property_address
  • $propertyObj->og_tags

For Example

Head

<meta name="keywords" content="<?php echo $propertyObj->meta_keywords?> ">
<meta name="description" content="<?php echo $propertyObj->meta_description ?> ">

Body

<h1>
    <?php echo $propertyObj->h1?>
</h1> 
<div id="MBBv3_SearchDetails">
    <?php echo $propertyObj->html?>
</div>

Property Details Example (updated 4/17/2024)

<?php 
require_once "mbb/MyBuyingBuddy.php"; /* calls the PHP api */ 
$mbbObj = new MyBuyingBuddy( array(
    "acid" => "[ACTIVATION_KEY]"
    ) 
); /* sets up the Account Activation Key for the PHP environment */ 
$loginObj = $mbbObj->getWidget("MBBv3_LoginPanel", array("filter" => "layout:horizontal") ); 
$propertyObj = $mbbObj->getWidget(
    "MBBv3_SearchDetails", 
    array("property_id" => $mbbObj->getPropertyIdFromURL() 
    ) 
); 
?> 
<!DOCTYPE html> 
<html> 
<head> 
<title></title> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> 
<meta name="description" content="<?php echo $propertyObj->meta_description?>"> 
<!-- Buying Buddy plugin -->
<script src="https://www.mbb2.com/version3/css/theme/acid/[ACTIVATION_KEY]"></script>
<script> 
    var MBB = { seo : "true", data:{ acid : "[ACTIVATION_KEY]" } };
    function mbbMapLoaded(){ MBB.googleMaps = true; };
</script> 
<script src='https://maps.googleapis.com/maps/api/js?callback=mbbMapLoaded&libraries=places&key=[YOUR_GOOGLE_MAPS_API_KEY]'></script> 
<script src="https://d2w6u17ngtanmy.cloudfront.net/scripts/my-buying-buddy.5.0.js.gz"></script> 
<!-- End Buying Buddy plugin. Copyright -->
<?php echo $propertyObj->og_tags?> 
</head> 
<body> 
<h1>
    <?php echo $propertyObj->h1?>
</h1> 
<div id="MBBv3_SearchDetails">
    <?php echo $propertyObj->html?>
</div> 
</body> 
</html>

HTACCESS

This is an example of HTACCESS for setting up a single property details PHP page for all property details

RewriteEngine on 
RewriteRule ^property.*$ property.php [L] 
RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^.*$ - [NC,L] 
RewriteRule ^.*$ /index.php [NC,L]

Form Field Definitions

When you start to use the PHP API you will be able to use the API to load values in forms.

  • field_label : Label for the form field
  • field_nm : Field name to use when building a form / query
  • field_type : Number support as "_min" and "_max".
    E.g. If field_nm = "price", then this would be "price_min" and "price_max"
  • like : {true or false}. If field queries by like
    E.g. city like %denver% would return Denver and Denver West etc...
  • allow_multiple : {true or false}. If field is true it accepts a comma separated list of values.
    E.g. city = denver, arvada, pueblo
  • look_field : Field uses the lookup code.
    E.g. property features Central Air = ac, Deck = db,  then this would be: "property_features=ac, dc" to get all listings with central air and a deck
  • All fields also support "_not" : which means that field / value should NOT exist.
    E.g. property_features=ac AND city_not=denver would show all properties with Central Air that are not in Denver

There are also some special fields

  • shapesearch : Comma separated list of lat and long as string
    E.g. "1.0333333 -24.0000, 1.0234 -23.40040"
  • create_dt : Number of days on market
    E.g. create_dt=10 show listings new in last 10 days
  • price_drop : Number of days since price dropped
    E.g. price_drop=5 show listings where the price dropped in last 5 days
  • listing_num : When this field is specified all other fields are ignored

PHP Code Reference

The following show examples of how to deploy Widgets when using the PHP class.

Specifically, how to get form field data, field look-ups, get details and photos for a single property and how to perform a search via an Array or filter string.

IMPORTANT! If a filter is to be applied in the Widget, you should ensure that the appropriate filter is shown in the widget AND also specified in the $widgetParams array.

List Display Widget

Add this PHP code:

<?php // List Display Widget
$widgetParams = array( 
    "filter" => "mls_id:ca64+city:BORREGO SPRINGS,RAMONA",
    "limit" => 20, // Defaults to 30
    "order" => "create_dt desc", // Defaults to create_dt desc
    "page" => 1 // Defaults to page 1
);
$listObj = $mbbObj->getWidget("MBBv3_FeaturedList",$widgetParams);
?>

Then, on the page, HTML code like this:

<div id="MBBv3_FeaturedList" filter="mls_id:ca64+city:BORREGO SPRINGS,RAMONA">
    <?php echo $listObj->html?>
</div>

Add this PHP Code:

<?php // Gallery Display Widget 
$widgetParams = array( 
    "filter" => 
    "mls_id:ca64+price_min:500000+price_max:555000+city:BORREGO SPRINGS,RAMONA", 
    "limit" => 20, // Defaults to 30 if not set 
    "order" => "price desc", // Defaults to create_dt desc if not set 
    "page" => 1 // Defaults to page 1 if not set 
); 
$galleryObj = $mbbObj->
getWidget("MBBv3_FeaturedGallery",$widgetParams); 
?>

Then, on the page, HTML code like this:

<div id="MBBv3_FeaturedGallery" filter="mls_id:ca64+price_min:500000+price_max:555000+city:BORREGO SPRINGS,RAMONA">
    <?php echo $galleryObj->html?>
</div>

Map Widget

Add this PHP Code:

<?php // Interactive Map Widget 
$widgetParams = array( 
    "filter" => "mls_id:ca64+price_min:500000+price_max:555000", 
); 
$mapObj = $mbbObj->getWidget("MBBv3_InteractiveMap", $widgetParams ); 
?>

Then, on the page, HTML code like this:

<div id="MBBv3_InteractiveMap" filter="mls_id:ca64+price_min:500000+price_max:555000" style="height:600px;">
    <?php echo $mapObj->html?>
</div>

Results widget

Add this PHP Code:

<?php // Results widget 
$resultsObj = $mbbObj->getWidget("MBBv3_ListingResults", array("filter" => "login-panel:false") ); 
?>

Then, on the page, HTML code like this:

<div id="MBBv3_ListingResults" filter="login-panel:false">
    <?php echo $resultsObj->html?>
</div>

If you are passing filters in the URL you need to pass that filter to the getWidget function and set the filter attribute in the HTML as shown below.

<?php
    // Reesults widget
    $resultsObj = $mbbObj->getWidget("MBBv3_ListingResults", array("filter" => $_GET["filter"]) );
?>
<div id="MBBv3_ListingResults" filter="<?php echo $_GET["filter"]; ?>"><?php echo $resultsObj->html?></div>

See SEO Friendly URLs section below too.

GET the HTML for a Property Details widget

Add this PHP Code:

<?php // GET the HTML for a property details widget 
// If you are not using seo friendly URLS replace the getPropertyIdFromUrRL with $_GET["property_id"] 
$propertyObj = $mbbObj->getWidget("MBBv3_SearchDetails", array("property_id" => $mbbObj->getPropertyIdFromURL() ) ); 
?>

Then, on the page, use the variables in HTML code like this:

<meta name="keywords" content="<?php echo $propertyObj->meta_keywords?>" /> 
<meta name="description" content="<?php echo $propertyObj->meta_description?>" /> 
<h1>
    <?php echo $propertyObj->h1?>
</h1> 
<div id="MBBv3_SearchDetails">
    <?php echo $propertyObj->html?>
</div>

Office Roster Widget

See OfficeRoster Widget with PHP for instructions.

SEO Friendly URLs

One of the nice things about php is you can create more search engine friendly URLs.

For example, instead of using a filter in a URL such as ?filter=...
a more search engine friendly slug can be created using a root slug of /city then followed by /denver , or /arvada and so on.
i.e. /city/denver

This approach will allow you to create many different city (for example) URL links, without creating a whole new page for each, or having to add any new code.

In this example, create a filter string using this syntax, and use rewrite rules for everything after /city to go to one page.

NOTE: We recommend using the Featured Gallery widget for this purpose as this will provide better page indexing.

<?php
$filter = "mls_id:ga147+listing_status:active+city:{$city}+limit:10+order:create_dt%20desc";
    // Gallery widget
    $resultsObj = $mbbObj->getWidget("MBBv3_FeaturedGallery", array("filter" => $filter) );
?>
<div id="MBBv3_FeaturedGallery" filter="<?php echo $filter; ?>"><?php echo $resultsObj->html?></div>

Additional API Calls

<?php // ADDITIONAL API CALLS 
// These API calls are used to build custom forms etc.... 
// Get an array of property data based on an array of search parameters 
$queryParams = array( 
    "mls_id" => "ca64",      // REQUIRED 
    "price_min" => 500000, 
    "price_max" => 555000, 
    "city" => "BORREGO SPRINGS,RAMONA", 
    "page" => 1, 
    "sort" => "price-desc" 
); 

// Get an array of property data based on filter string of query parameters 
$resultArray = $mbbObj->getListingDataFromArray( $queryParams ); 

$filter = "mls_id:ca64+price_min:500000+price_max:555000+city:BORREGO SPRINGS,RAMONA+sort:price+page:3"; 
$resultsFilter = $mbbObj->getListingDataFromFilter($filter); 
// Get an array of information for a particular property id 
price => 450000, 
property_type => res, 
city => Ramona 
$propertyInfo = $mbbObj->
getProperty("ca64_110010319"); 
// Get an array of form fields available for a MLS 
$formFields = $mbbObj->getFormFieldList("ca64"); 
// Get an array of look up fields for a particular MLS 
$allLookups = $mbbObj->getAllLookupFields("ca64"); 
// Get an array of look up key value pairs for a particluar form field (property type will return res => Residential, lnd = Land etc...) 
$fieldLookups = $mbbObj->getFieldLookupValues("ca64","property_type"); 
?>
Updated on April 17, 2024

Was this article helpful?

Related Articles

Need Support?
Can't find the answer you're looking for?
Contact Support
Buying Buddy Support