Skip to content

Commit fb32aa2

Browse files
committedFeb 6, 2012
Initial push to GitHub
0 parents  commit fb32aa2

16 files changed

+2140
-0
lines changed
 

‎LICENSE

+674
Large diffs are not rendered by default.

‎README.md

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
flexmls API - version 2
2+
=====================
3+
A PHP wrapper for the flexmls REST API. This version has enough differences from version 1 that upgrading will
4+
require changes to existing code.
5+
6+
7+
Documentation
8+
-------------
9+
For full information on the API, see http://www.flexmls.com/developers
10+
11+
12+
Usage Examples
13+
------------------------
14+
// include the flexmlsAPI core which autoloads other classes as necessary
15+
require_once("lib/Core.php");
16+
17+
// connect using flexmls API authentication
18+
$api = new flexmlsAPI_APIAuth("api_key_goes_here", "api_secret_goes_here");
19+
20+
// identify your application (optional)
21+
$api->SetApplicationName("MyPHPApplication/1.0");
22+
23+
// authenticate
24+
$result = $api->Authenticate();
25+
if ($result === false) {
26+
echo "API Error Code: {$api->last_error_code}<br>\n";
27+
echo "API Error Message: {$api->last_error_mess}<br>\n";
28+
exit;
29+
}
30+
31+
// get your listings
32+
$result = $api->GetMyListings();
33+
34+
// see the included examples.php for more complete usage
35+
36+
37+
Error Codes
38+
---------------------
39+
<table>
40+
<thead>
41+
<tr>
42+
<th>HTTP Code</th>
43+
<th>flexmls API Error Code</th>
44+
<th>Automatic Retry</th>
45+
<th>Description</th>
46+
</tr>
47+
</thead>
48+
<tbody>
49+
<tr>
50+
<td><tt>401</tt></td>
51+
<td><tt>1000</tt></td>
52+
<td><tt>Yes</tt></td>
53+
<td>Invalid API Key and/or Request signed improperly</td>
54+
</tr>
55+
<tr>
56+
<td><tt>401</tt></td>
57+
<td><tt>1010</tt></td>
58+
<td><tt>No</tt></td>
59+
<td>API key is disabled</td>
60+
</tr>
61+
<tr>
62+
<td><tt>403</tt></td>
63+
<td><tt>1015</tt></td>
64+
<td><tt>No</tt></td>
65+
<td><tt>ApiUser</tt> must be supplied, or the provided key does not have access to the supplied user</td>
66+
</tr>
67+
<tr>
68+
<td><tt>401</tt></td>
69+
<td><tt>1020</tt></td>
70+
<td><tt>Yes</tt></td>
71+
<td>Session token has expired</td>
72+
</tr>
73+
<tr>
74+
<td><tt>403</tt></td>
75+
<td><tt>1030</tt></td>
76+
<td><tt>No</tt></td>
77+
<td>SSL required for this type of request</td>
78+
</tr>
79+
<tr>
80+
<td><tt>400</tt></td>
81+
<td><tt>1035</tt></td>
82+
<td><tt>No</tt></td>
83+
<td>POST data not supplied as valid JSON. Issued if the <tt>Content-Type</tt> header is not <tt>application/json/</tt> and/or if the POST data is not in valid JSON format.</td>
84+
</tr>
85+
<tr>
86+
<td><tt>400</tt></td>
87+
<td><tt>1040</tt></td>
88+
<td><tt>No</tt></td>
89+
<td>The <tt>_filter</tt> syntax was invalid or a specified field to search on does not exist</td>
90+
</tr>
91+
<tr>
92+
<td><tt>400</tt></td>
93+
<td><tt>1050</tt></td>
94+
<td><tt>No</tt></td>
95+
<td>(message varies) A required parameter was not provided</td>
96+
</tr>
97+
<tr>
98+
<td><tt>400</tt></td>
99+
<td><tt>1053</tt></td>
100+
<td><tt>No</tt></td>
101+
<td>(message varies) A parameter was provided but does not adhere to constraints</td>
102+
</tr>
103+
<tr>
104+
<td><tt>409</tt></td>
105+
<td><tt>1055</tt></td>
106+
<td><tt>No</tt></td>
107+
<td>(message varies)Issued when a write is requested that will conflict with existing data. For example, adding a new contact with an e-mail that already exists.</td>
108+
</tr>
109+
<tr>
110+
<td><tt>403</tt></td>
111+
<td><tt>1500</tt></td>
112+
<td><tt>No</tt></td>
113+
<td>The resource is not available at the current API key's service level. For example, this error applies if a user attempts to access the IDX Links API via a free API key. </td>
114+
</tr>
115+
<tr>
116+
<td><tt>503</tt></td>
117+
<td><tt>1550</tt></td>
118+
<td><tt>No</tt></td>
119+
<td>Over rate limit</td>
120+
</tbody>
121+
</table>
122+
123+

‎examples.php

+215
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
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" => "apiexample@flexmls.com",
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

‎lib/APIAuth.php

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
class flexmlsAPI_APIAuth extends flexmlsAPI_Core implements flexmlsAPI_AuthInterface {
4+
protected $api_key = null;
5+
protected $api_secret = null;
6+
7+
8+
function __construct($api_key, $api_secret) {
9+
$this->api_key = $api_key;
10+
$this->api_secret = $api_secret;
11+
12+
$this->auth_mode = 'api';
13+
14+
parent::__construct();
15+
}
16+
17+
function is_auth_request($request) {
18+
return ($request['uri'] == '/'. $this->api_version .'/session') ? true : false;
19+
}
20+
21+
22+
function sign_request($request) {
23+
24+
$http_parameters = $request['params'];
25+
$cache_http_parameters = $http_parameters;
26+
27+
$sec_string = "{$this->api_secret}ApiKey{$this->api_key}";
28+
$post_body = "";
29+
30+
if ($request['method'] == "POST" && !empty($request['post_data']) > 0) {
31+
// the request is to post some JSON data back to the API (like adding a contact)
32+
$post_body = $request['post_data'];
33+
}
34+
35+
$is_auth_request = ($request['uri'] == '/'. $this->api_version .'/session') ? true : false;
36+
37+
if ($is_auth_request) {
38+
$http_parameters['ApiKey'] = $this->api_key;
39+
}
40+
else {
41+
42+
if ( empty($this->last_token) ) {
43+
// attempt to pull this from the cache if it's turned on
44+
if ($this->cache) {
45+
$cached_token = $this->cache->get($this->cache_prefix . 'authtoken');
46+
if ($cached_token != null) {
47+
$this->SetAuthToken($cached_token);
48+
}
49+
}
50+
}
51+
52+
$http_parameters['AuthToken'] = $this->last_token;
53+
54+
// since this isn't an authentication request, add the ServicePath to the security string
55+
$sec_string .= "ServicePath" . rawurldecode($request['uri']);
56+
57+
ksort($http_parameters);
58+
59+
// add each of the HTTP query string parameters to the security string
60+
foreach ($http_parameters as $k => $v) {
61+
$sec_string .= $k . $v;
62+
}
63+
}
64+
65+
if (!empty($post_body)) {
66+
// add the post data to the end of the security string if it exists
67+
$sec_string .= $post_body;
68+
}
69+
70+
// calculate the security string as ApiSig
71+
$api_sig = md5($sec_string);
72+
$http_parameters['ApiSig'] = $api_sig;
73+
74+
$request['query_string'] = http_build_query($http_parameters);
75+
$request['cacheable_query_string'] = http_build_query($cache_http_parameters);
76+
}
77+
78+
function SetAuthToken($token) {
79+
$this->last_token = $token;
80+
}
81+
82+
83+
/*
84+
* Authentication
85+
*/
86+
87+
function Authenticate() {
88+
$response = $this->MakeAPICall("POST", "session");
89+
90+
if ($response['success']) {
91+
$this->last_token = $response['results'][0]['AuthToken'];
92+
$this->last_token_expire = $response['results'][0]['Expires'];
93+
94+
if ($this->cache) {
95+
$this->cache->set($this->cache_prefix . 'authtoken', $this->last_token, 86400);
96+
}
97+
98+
if ( is_callable($this->access_change_callback) ) {
99+
call_user_func($this->access_change_callback, 'api', array('auth_token' => $this->last_token) );
100+
}
101+
102+
return true;
103+
}
104+
105+
return false;
106+
}
107+
108+
function ReAuthenticate() {
109+
return $this->Authenticate();
110+
}
111+
112+
113+
114+
}

‎lib/AuthInterface.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
interface flexmlsAPI_AuthInterface {
4+
5+
}

‎lib/CacheInterface.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
interface flexmlsAPI_CacheInterface {
4+
5+
function get($key);
6+
function set($key, $value, $expire);
7+
8+
}

‎lib/Core.php

+657
Large diffs are not rendered by default.

‎lib/CoreTransport.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
class flexmlsAPI_CoreTransport {
4+
5+
6+
function __construct() {
7+
8+
}
9+
10+
11+
12+
}

‎lib/CurlTransport.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
class flexmlsAPI_CurlTransport extends flexmlsAPI_CoreTransport implements flexmlsAPI_TransportInterface {
4+
protected $ch = null;
5+
6+
function __construct() {
7+
// initialize cURL for use later
8+
$this->ch = curl_init();
9+
curl_setopt($this->ch, CURLOPT_HEADER, false);
10+
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
11+
curl_setopt($this->ch, CURLOPT_TIMEOUT, 0);
12+
curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);
13+
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
14+
curl_setopt($this->ch, CURLOPT_ENCODING, "gzip");
15+
}
16+
17+
function __destruct() {
18+
// clean cURL up
19+
curl_close($this->ch);
20+
}
21+
22+
function make_request($request = array()) {
23+
24+
// print_r($request);
25+
26+
$request_headers_flat = "";
27+
foreach ($request['headers'] as $k => $v) {
28+
$request_headers_flat .= "{$k}: {$v}\r\n";
29+
}
30+
31+
$full_url = $request['protocol'] .'://'. $request['host'] . $request['uri'];
32+
if ( !empty($request['query_string']) ) {
33+
$full_url .= '?'. $request['query_string'];
34+
}
35+
36+
curl_setopt($this->ch, CURLOPT_URL, $full_url);
37+
38+
if ($request['method'] == "POST") {
39+
curl_setopt($this->ch, CURLOPT_POST, 1);
40+
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $request['post_data']);
41+
}
42+
else {
43+
curl_setopt($this->ch, CURLOPT_POST, 0);
44+
}
45+
46+
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array(trim($request_headers_flat)));
47+
48+
$response_body = curl_exec($this->ch);
49+
$response_info = curl_getinfo($this->ch);
50+
51+
$response_info['body'] = $response_body;
52+
53+
// print_r($response_info);
54+
55+
return $response_info;
56+
57+
}
58+
59+
}

‎lib/MemcacheCache.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
class flexmlsAPI_MemcacheCache implements flexmlsAPI_CacheInterface {
4+
protected $cache = null;
5+
6+
protected $host = null;
7+
protected $port = null;
8+
9+
10+
function __construct($host = 'localhost', $port = 11211) {
11+
$this->host = $host;
12+
$this->port = $port;
13+
$this->cache = new Memcache;
14+
$this->cache->connect($host, $port);
15+
}
16+
17+
function get($key) {
18+
$value = $this->cache->get($key);
19+
if ($value !== false) {
20+
return $value;
21+
}
22+
return null;
23+
}
24+
25+
function set($key, $value, $expire) {
26+
return $this->cache->set($key, $value, 0, $expire);
27+
}
28+
29+
30+
}

‎lib/MemcachedCache.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
class flexmlsAPI_MemcachedCache implements flexmlsAPI_CacheInterface {
4+
protected $cache = null;
5+
6+
protected $host = null;
7+
protected $port = null;
8+
9+
10+
function __construct($host = 'localhost', $port = 11211) {
11+
$this->host = $host;
12+
$this->port = $port;
13+
$this->cache = new Memcached;
14+
$this->cache->addServer($host, $port);
15+
}
16+
17+
function get($key) {
18+
$value = $this->cache->get($key);
19+
if ($value !== false) {
20+
return $value;
21+
}
22+
return null;
23+
}
24+
25+
function set($key, $value, $expire) {
26+
return $this->cache->set($key, $value, time() + $expire);
27+
}
28+
29+
30+
}

‎lib/MySQLiCache.php

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
class flexmlsAPI_MySQLiCache implements flexmlsAPI_CacheInterface {
4+
protected $cache = null;
5+
6+
protected $hostname = null;
7+
protected $database = null;
8+
protected $username = null;
9+
protected $password = null;
10+
protected $table_name = null;
11+
protected $conn = null;
12+
13+
14+
function __construct($hostname = 'localhost', $database = '', $username = '', $password = '', $table_name = 'api_cache') {
15+
16+
// check if $hostname given is actually an object (pre-existing mysqli connection)
17+
if ( is_object($hostname)) {
18+
$this->conn = $hostname;
19+
}
20+
else {
21+
// make a new connection to the database
22+
$this->conn = new mysqli($hostname, $username, $password, $database);
23+
if ($this->conn->connect_error) {
24+
$this->conn = null;
25+
}
26+
}
27+
28+
$this->table_name = $table_name;
29+
30+
}
31+
32+
function get($key) {
33+
// check if
34+
if (!$this->conn) {
35+
return null;
36+
}
37+
38+
$this->check_gc();
39+
40+
$sql = "SELECT `cache_value`
41+
FROM `". $this->table_name ."`
42+
WHERE `cache_key` = '". $this->quote($key) ."' AND `expiration` > unix_timestamp()
43+
LIMIT 1";
44+
45+
if ($result = $this->conn->query($sql)) {
46+
$row = $result->fetch_assoc();
47+
if ($row !== null) {
48+
return unserialize($row['cache_value']);
49+
}
50+
}
51+
52+
return null;
53+
}
54+
55+
function set($key, $value, $expire) {
56+
if (!$this->conn) {
57+
return null;
58+
}
59+
60+
$this->check_gc();
61+
62+
$value = serialize($value);
63+
64+
$new_expire = time() + $expire;
65+
66+
$sql = "INSERT INTO `". $this->table_name ."` (`cache_key`, `cache_value`, `expiration`)
67+
VALUES ('". $this->quote($key) ."', '". $this->quote($value) ."', ". $this->quote($new_expire) .")
68+
ON DUPLICATE KEY UPDATE `cache_value`='". $this->quote($value) ."', `expiration`=". $this->quote($new_expire);
69+
70+
return $this->conn->query($sql);
71+
}
72+
73+
function quote($value) {
74+
return $this->conn->real_escape_string($value);
75+
}
76+
77+
function check_gc() {
78+
$random_number = rand(1, 1000);
79+
if ($random_number === 1) {
80+
$this->gc();
81+
}
82+
}
83+
84+
function gc() {
85+
return $this->conn->query("DELETE FROM `". $this->table_name ."` WHERE `expiration` < unit_timestamp()");
86+
}
87+
88+
89+
}

‎lib/OAuth.php

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
class flexmlsAPI_OAuth extends flexmlsAPI_Core implements flexmlsAPI_AuthInterface {
4+
protected $force_https = true;
5+
protected $api_client_id = null;
6+
protected $api_client_secret = null;
7+
protected $oauth_redirect_uri = null;
8+
public $oauth_access_token = null;
9+
public $oauth_refresh_token = null;
10+
protected $oauth_grant_code = null;
11+
12+
function __construct($api_client_id, $api_client_secret, $redirect_uri, $access_token = null) {
13+
$this->api_client_id = $api_client_id;
14+
$this->api_client_secret = $api_client_secret;
15+
$this->oauth_redirect_uri = $redirect_uri;
16+
$this->SetAccessToken($access_token);
17+
18+
$this->auth_mode = 'oauth';
19+
20+
parent::__construct();
21+
}
22+
23+
function sign_request($request) {
24+
$this->SetHeader('Authorization', 'OAuth '. $this->last_token);
25+
26+
// reload headers into request
27+
$request['headers'] = $this->headers;
28+
$request['query_string'] = http_build_query($request['params']);
29+
$request['cacheable_query_string'] = $request['query_string'];
30+
}
31+
32+
function is_auth_request($request) {
33+
return ($request['uri'] == '/'. $this->api_version .'/oauth2/grant') ? true : false;
34+
}
35+
36+
function Grant($code, $type = 'authorization_code') {
37+
$body = array(
38+
'client_id' => $this->api_client_id,
39+
'client_secret' => $this->api_client_secret,
40+
'grant_type' => $type,
41+
'redirect_uri' => $this->oauth_redirect_uri
42+
);
43+
44+
if ($type == 'authorization_code') {
45+
$body['code'] = $code;
46+
}
47+
if ($type == 'refresh_token') {
48+
$body['refresh_token'] = $code;
49+
}
50+
51+
$response = $this->MakeAPICall("POST", "oauth2/grant", '0s', array(), json_encode($body) );
52+
53+
if ($response['success'] == true) {
54+
$this->SetAccessToken( $response['results']['access_token'] );
55+
$this->SetRefreshToken( $response['results']['refresh_token'] );
56+
57+
if ( is_callable($this->access_change_callback) ) {
58+
call_user_func($this->access_change_callback, 'oauth', array('access_token' => $this->oauth_access_token, 'refresh_token' => $this->oauth_refresh_token) );
59+
}
60+
61+
return true;
62+
}
63+
else {
64+
return false;
65+
}
66+
67+
}
68+
69+
function SetAccessToken($token) {
70+
$this->oauth_access_token = $token;
71+
$this->last_token = $token;
72+
}
73+
74+
function SetRefreshToken($token) {
75+
$this->oauth_refresh_token = $token;
76+
}
77+
78+
79+
/*
80+
* Authentication
81+
*/
82+
83+
function Authenticate() {
84+
return true;
85+
}
86+
87+
function ReAuthenticate() {
88+
if ( !empty($this->oauth_refresh_token) ) {
89+
return $this->Grant($this->oauth_refresh_token, 'refresh_token');
90+
}
91+
return false;
92+
}
93+
94+
function Ping() {
95+
return $this->return_all_results( $this->MakeAPICall("GET", "my/account") );
96+
}
97+
98+
}

‎lib/TransportInterface.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
interface flexmlsAPI_TransportInterface {
4+
5+
function make_request($req = array());
6+
7+
}

‎lib/WordPressCache.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
class flexmlsAPI_WordPressCache implements flexmlsAPI_CacheInterface {
4+
5+
6+
function get($key) {
7+
$value = get_transient($key);
8+
if ($value !== false) {
9+
return $value;
10+
}
11+
return null;
12+
}
13+
14+
function set($key, $value, $expire) {
15+
return set_transient($key, $value, $expire);
16+
}
17+
18+
19+
}

‎lib/WordPressTransport.php

Whitespace-only changes.

0 commit comments

Comments
 (0)
Please sign in to comment.