|
| 1 | +<?php |
| 2 | + |
| 3 | +// include the flexmlsAPI core which autoloads other classes as necessary |
| 4 | +require_once("lib/Core.php"); |
| 5 | + |
| 6 | + |
| 7 | +/* |
| 8 | + * authenticate with the API |
| 9 | + * Changed in version 2.0 |
| 10 | + * |
| 11 | + * The newest version of the PHP API client (version 2.0) allows you to select which authentication method |
| 12 | + * you'd like to use against the API. Version 1.0 was limited to the flexmls API authentication method and is |
| 13 | + * now done using: |
| 14 | + * |
| 15 | + * $api = new flexmlsAPI_APIAuth("api_key_goes_here", "api_secret_goes_here"); |
| 16 | + * |
| 17 | + * With version 2.0, you can now authenticate using OAuth2 support. For more details on OAuth2 with the flexmls API, |
| 18 | + * see https://www.flexmls.com/developers/api/api-services/authentication/ |
| 19 | + * |
| 20 | + * $api = new flexmlsAPI_OAuth($client_id, $client_secret, $application_uri); |
| 21 | + * |
| 22 | + * To issue a Grant request with the "code" value provided by the API: |
| 23 | + * |
| 24 | + * $result = $api->Grant($code_value); |
| 25 | + * |
| 26 | + * A successful response will populate 2 new variables: |
| 27 | + * |
| 28 | + * $api->oauth_access_token |
| 29 | + * $api->oauth_refresh_token |
| 30 | + * |
| 31 | + * These values can be saved and re-used in future requests using: |
| 32 | + * |
| 33 | + * $api->SetAccessToken($previous_access_token); |
| 34 | + * $api->SetRefreshToken($previous_refresh_token); |
| 35 | + * |
| 36 | + * Also, for convenience, if the access token has expired and the refresh token is used to automatically have a new access |
| 37 | + * token generated, a hook is available to notify you of the new tokens: |
| 38 | + * |
| 39 | + * $api->SetNewAccessCallback('new_token_given'); |
| 40 | + * |
| 41 | + * which executes a function called "new_token_given" with 2 arguments: |
| 42 | + * |
| 43 | + * * the type of grant which resulted in new tokens. "authorization_code" or "refresh_token" |
| 44 | + * * the values (in array format). "access_token", "refresh_token" and "expires_in" are the 3 array keys |
| 45 | + * |
| 46 | + */ |
| 47 | + |
| 48 | +$api = new flexmlsAPI_APIAuth("api_key_goes_here", "api_secret_goes_here"); |
| 49 | + |
| 50 | +// identify your application (optional) |
| 51 | +$api->SetApplicationName("PHP-API-Code-Examples/1.0"); |
| 52 | + |
| 53 | +// enable developer mode. this points you to a sandbox for development use |
| 54 | +$api->SetDeveloperMode(true); |
| 55 | + |
| 56 | + |
| 57 | +/* |
| 58 | + * enable built-in caching system |
| 59 | + * New in version 2.0 |
| 60 | + * |
| 61 | + * The newest version of the PHP API client (version 2.0) contains a system for enabling a |
| 62 | + * built-in cache. The following options currently exist: |
| 63 | + * |
| 64 | + * * Memcache (Uses http://pecl.php.net/package/memcache) |
| 65 | + * * Memcached (Uses http://pecl.php.net/package/memcached) |
| 66 | + * * WordPress (for use within plugins or themes) |
| 67 | + * * MySQLi for storing the cache in a MySQL database |
| 68 | + * |
| 69 | + * To enable a particular caching system, you must create an instance of the desired object and pass it to |
| 70 | + * the API core. |
| 71 | + * |
| 72 | + * |
| 73 | + * To enable Memcache or Memcached cache support, the host and port (both optional) can be given: |
| 74 | + * |
| 75 | + * $api->SetCache( new flexmlsAPI_MemcacheCache() ); // defaults to localhost and port 11211 |
| 76 | + * or: |
| 77 | + * $api->SetCache( new flexmlsAPI_MemcachedCache('remotehost', 12345) ); // overrides both defaults |
| 78 | + * |
| 79 | + * depending on the Memcached-compliant driver you choose. |
| 80 | + * |
| 81 | + * |
| 82 | + * To enable WordPress caching, no arguments are required: this method uses the set_transient() and get_transient() |
| 83 | + * functions created by WordPress which can be extended by other WP plugins for additional (or modified) functionality. |
| 84 | + * |
| 85 | + * $api->SetCache( new flexmlsAPI_WordPressCache() ); |
| 86 | + * |
| 87 | + * |
| 88 | + * To enable database caching via the MySQLi extension, you can either pass connection details to the class: |
| 89 | + * |
| 90 | + * $api->SetCache( new flexmlsAPI_MySQLiCache($hostname, $database, $username, $password, $table_name)); |
| 91 | + * |
| 92 | + * or you can re-use an existing MySQLi connection by passing the object: |
| 93 | + * |
| 94 | + * $api->SetCache( new flexmlsAPI_MySQLiCache($my_mysqli_object) ); |
| 95 | + * |
| 96 | + * By default, a $table_name of "api_cache" is assumed if none is given. The structure for that table is: |
| 97 | + * |
| 98 | + * CREATE TABLE api_cache ( |
| 99 | + * cache_key VARCHAR(125), |
| 100 | + * cache_value TEXT, |
| 101 | + * expiration INT(10), |
| 102 | + * PRIMARY KEY(cache_key) |
| 103 | + * ) |
| 104 | + * |
| 105 | + */ |
| 106 | + |
| 107 | + |
| 108 | +// authenticate |
| 109 | +$result = $api->Authenticate(); |
| 110 | +if ($result === false) { |
| 111 | + echo "API Error Code: {$api->last_error_code}<br>\n"; |
| 112 | + echo "API Error Message: {$api->last_error_mess}<br>\n"; |
| 113 | + exit; |
| 114 | +} |
| 115 | + |
| 116 | +/* |
| 117 | + * request some basic account and system information |
| 118 | + */ |
| 119 | +$result = $api->GetSystemInfo(); |
| 120 | +// https://www.flexmls.com/developers/api/api-services/system-info/ |
| 121 | +print_r($result); |
| 122 | + |
| 123 | +$result = $api->GetPropertyTypes(); |
| 124 | +// https://www.flexmls.com/developers/api/api-services/property-types/ |
| 125 | +print_r($result); |
| 126 | + |
| 127 | +$result = $api->GetStandardFields(); |
| 128 | +// https://www.flexmls.com/developers/api/api-services/standard-fields/ |
| 129 | +print_r($result); |
| 130 | + |
| 131 | +$result = $api->GetMyAccount(); |
| 132 | +// https://www.flexmls.com/developers/api/api-services/my-account/ |
| 133 | +print_r($result); |
| 134 | + |
| 135 | + |
| 136 | +/* |
| 137 | + * different requests for listings based on context |
| 138 | + */ |
| 139 | + |
| 140 | +$result = $api->GetMyListings(); |
| 141 | +// https://www.flexmls.com/developers/api/api-services/listings/ |
| 142 | +print_r($result); |
| 143 | + |
| 144 | +$result = $api->GetOfficeListings(); |
| 145 | +// https://www.flexmls.com/developers/api/api-services/listings/ |
| 146 | +print_r($result); |
| 147 | + |
| 148 | +$result = $api->GetCompanyListings(); |
| 149 | +// https://www.flexmls.com/developers/api/api-services/listings/ |
| 150 | +print_r($result); |
| 151 | + |
| 152 | +/* |
| 153 | + * request for listings with some parameters. the above listing requests this argument and most of the options within |
| 154 | + */ |
| 155 | +$result = $api->GetListings( |
| 156 | + array( |
| 157 | + '_pagination' => 1, |
| 158 | + '_limit' => 3, |
| 159 | + '_page' => 2, |
| 160 | + '_filter' => "PropertyType Eq 'A'", |
| 161 | + '_expand' => 'PrimaryPhoto' |
| 162 | + ) |
| 163 | +); |
| 164 | +// https://www.flexmls.com/developers/api/api-services/listings/ |
| 165 | +print_r($result); |
| 166 | + |
| 167 | +/* |
| 168 | + * with a particular listing Id known, several additional API calls are available |
| 169 | + */ |
| 170 | + |
| 171 | +$id = "20100912153422758914000000"; // this comes from the Id value in a listing response |
| 172 | + |
| 173 | +$result = $api->GetListingPhotos($id); |
| 174 | +// https://www.flexmls.com/developers/api/api-services/listing-photos/ |
| 175 | +$result = $api->GetListingDocuments($id); |
| 176 | +// https://www.flexmls.com/developers/api/api-services/listing-documents/ |
| 177 | +$result = $api->GetListingOpenHouses($id); |
| 178 | +// https://www.flexmls.com/developers/api/api-services/open-houses/ |
| 179 | +$result = $api->GetListingVideos($id); |
| 180 | +// https://www.flexmls.com/developers/api/api-services/listing-videos/ |
| 181 | +$result = $api->GetListingVirtualTours($id); |
| 182 | +// https://www.flexmls.com/developers/api/api-services/virtual-tours/ |
| 183 | + |
| 184 | + |
| 185 | +/* |
| 186 | + * with a particular object Id known, you can request additional information about that one item |
| 187 | + */ |
| 188 | + |
| 189 | +$photo_id = "20080917142739989238000000"; |
| 190 | + |
| 191 | +$result = $api->GetListingPhoto($id, $photo_id); |
| 192 | +// https://www.flexmls.com/developers/api/api-services/listing-photos/ |
| 193 | + |
| 194 | + |
| 195 | +/* |
| 196 | + * contact management |
| 197 | + * https://www.flexmls.com/developers/api/api-services/contacts/ |
| 198 | + */ |
| 199 | + |
| 200 | +$result = $api->GetContacts(); |
| 201 | + |
| 202 | +$new_contact = array( |
| 203 | + "DisplayName" => "Example Contact", |
| 204 | + "PrimaryEmail" => "[email protected]", |
| 205 | + "PrimaryPhoneNumber" => "888-123-4567", |
| 206 | + "HomeStreetAddress" => "123 S. Main St", |
| 207 | + "HomeLocality" => "Fargo", |
| 208 | + "HomeRegion" => "ND", |
| 209 | + "HomePostalCode" => "58104", |
| 210 | + "Tag" => "Example Group" |
| 211 | +); |
| 212 | + |
| 213 | +// $result = $api->AddContact($new_contact); // creates a new contact |
| 214 | + |
| 215 | +$result = $api->GetContact("20090816141725963238000000"); // get a contact by their Id |
0 commit comments