1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* WP-Zillow-API (https://www.zillow.com/howto/api/APIOverview.htm) |
4
|
|
|
* |
5
|
|
|
* @package WP-Zillow-API |
6
|
|
|
*/ |
7
|
|
|
/* |
8
|
|
|
* Plugin Name: WP Zillow API |
9
|
|
|
* Plugin URI: https://github.com/wp-api-libraries/wp-zillow-api |
10
|
|
|
* Description: Perform API requests to Zillow in WordPress. |
11
|
|
|
* Author: WP API Libraries |
12
|
|
|
* Version: 1.0.2 |
13
|
|
|
* Author URI: https://wp-api-libraries.com |
14
|
|
|
* GitHub Plugin URI: https://github.com/wp-api-libraries/wp-zillow-api |
15
|
|
|
* GitHub Branch: master |
16
|
|
|
*/ |
17
|
|
|
/* Exit if accessed directly. */ |
18
|
|
|
if ( ! defined( 'ABSPATH' ) ) { exit; } |
19
|
|
|
/* Check if class exists. */ |
20
|
|
|
if ( ! class_exists( 'ZillowAPI' ) ) { |
21
|
|
|
/** |
22
|
|
|
* Zillow API Class. |
23
|
|
|
*/ |
24
|
|
|
class ZillowAPI { |
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Zillow API Key (aka ZWSID). |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
static private $zws_id; |
31
|
|
|
/** |
32
|
|
|
* Return format. XML or JSON. |
33
|
|
|
* |
34
|
|
|
* @var [string |
35
|
|
|
*/ |
36
|
|
|
static private $output; |
37
|
|
|
/** |
38
|
|
|
* Zillow BaseAPI Endpoint |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
* @access protected |
42
|
|
|
*/ |
43
|
|
|
protected $base_uri = 'https://www.zillow.com/webservice/'; |
44
|
|
|
/** |
45
|
|
|
* Construct. |
46
|
|
|
* |
47
|
|
|
* @access public |
48
|
|
|
* @param mixed $zws_id ZWSID. |
49
|
|
|
* @param mixed $output Output. |
50
|
|
|
* @return void |
|
|
|
|
51
|
|
|
*/ |
52
|
|
|
public function __construct( $zws_id, $output = 'json' ) { |
53
|
|
|
static::$zws_id = $zws_id; |
|
|
|
|
54
|
|
|
static::$output = $output; |
|
|
|
|
55
|
|
|
} |
56
|
|
|
/** |
57
|
|
|
* Fetch the request from the API. |
58
|
|
|
* |
59
|
|
|
* @access private |
60
|
|
|
* @param mixed $request Request URL. |
61
|
|
|
* @return $body Body. |
|
|
|
|
62
|
|
|
*/ |
63
|
|
View Code Duplication |
private function fetch( $request ) { |
|
|
|
|
64
|
|
|
$response = wp_remote_get( $request ); |
65
|
|
|
$code = wp_remote_retrieve_response_code( $response ); |
66
|
|
|
if ( 200 !== $code ) { |
67
|
|
|
return new WP_Error( 'response-error', sprintf( __( 'Server response code: %d', 're-pro' ), $code ) ); |
68
|
|
|
} |
69
|
|
|
$body = wp_remote_retrieve_body( $response ); |
70
|
|
|
return json_decode( $body ); |
71
|
|
|
} |
72
|
|
|
/** |
73
|
|
|
* Get Zillow Reviews (https://www.zillow.com/howto/api/ReviewsAPI.htm) |
74
|
|
|
* |
75
|
|
|
* @access public |
76
|
|
|
* @param mixed $screenname Screenname. |
77
|
|
|
* @param mixed $email (default: null) Email. |
78
|
|
|
* @param string $count (default: '3') Count. |
79
|
|
|
* @param mixed $returnTeamMemberReviews (default: null) Return Team Member Reviews. |
80
|
|
|
* @return Request. |
|
|
|
|
81
|
|
|
*/ |
82
|
|
|
function get_reviews( $screenname, $email = null, $count = '3', $returnTeamMemberReviews = null ) { |
|
|
|
|
83
|
|
|
if ( empty( $screenname ) ) { |
84
|
|
|
return new WP_Error( 'required-fields', __( 'Required fields are empty.', 're-pro' ) ); |
85
|
|
|
} |
86
|
|
|
$request = $this->base_uri . '/ProReviews.htm?zws-id=' . static::$zws_id . '&screenname=' . $screenname . '&output=json'; |
|
|
|
|
87
|
|
|
return $this->fetch( $request ); |
88
|
|
|
} |
89
|
|
|
/** |
90
|
|
|
* Get Mortage Rate Summary (https://www.zillow.com/howto/api/GetRateSummary.htm) |
91
|
|
|
* |
92
|
|
|
* @access public |
93
|
|
|
* @param mixed $state (default: null) State. |
94
|
|
|
* @param string $callback (default: '') Callback. |
95
|
|
|
* @return Request. |
|
|
|
|
96
|
|
|
*/ |
97
|
|
|
function get_rate_summary( $state = null, $callback = '' ) { |
|
|
|
|
98
|
|
|
$request = $this->base_uri . '/GetRateSummary.htm?zws-id=' . static::$zws_id . '&output=json'; |
|
|
|
|
99
|
|
|
return $this->fetch( $request ); |
100
|
|
|
} |
101
|
|
|
/** |
102
|
|
|
* Get Monthly Payments (https://www.zillow.com/howto/api/GetMonthlyPayments.htm) |
103
|
|
|
* |
104
|
|
|
* @access public |
105
|
|
|
* @param mixed $price Price. |
106
|
|
|
* @param mixed $down (default: null) Down. |
107
|
|
|
* @param mixed $dollarsdown (default: null) DollarsDown. |
108
|
|
|
* @param mixed $zip (default: null) Zip. |
109
|
|
|
* @param mixed $output (default: null) Output. |
110
|
|
|
* @param mixed $callback (default: null) Callback. |
111
|
|
|
* @return Request. |
|
|
|
|
112
|
|
|
*/ |
113
|
|
|
function get_monthly_payments( $price, $down = null, $dollarsdown = null, $zip = null, $output = null, $callback = null ) { |
|
|
|
|
114
|
|
|
if ( empty( $price ) ) { |
115
|
|
|
return new WP_Error( 'required-fields', __( 'Required fields are empty.', 're-pro' ) ); |
116
|
|
|
} |
117
|
|
|
$request = $this->base_uri . '/GetMonthlyPayments.htm?zws-id=' . static::$zws_id . '&output=json' . '&price=' . $price; |
|
|
|
|
118
|
|
|
return $this->fetch( $request ); |
119
|
|
|
} |
120
|
|
|
/** |
121
|
|
|
* Get Deep Search Results. |
122
|
|
|
* |
123
|
|
|
* @access public |
124
|
|
|
* @param mixed $address Address. |
125
|
|
|
* @param mixed $citystatezip City/State/Zip. |
126
|
|
|
* @param mixed $rentzestimate (default: null) Rent Zestimate. |
127
|
|
|
* @return Request. |
|
|
|
|
128
|
|
|
*/ |
129
|
|
|
function get_deep_search_results( $address, $citystatezip, $rentzestimate = null ) { |
|
|
|
|
130
|
|
|
if ( empty( $address ) ) { |
131
|
|
|
return new WP_Error( 'required-fields', __( 'Required fields are empty.', 're-pro' ) ); |
132
|
|
|
} |
133
|
|
|
$request = $this->base_uri . '/GetMonthlyPayments.htm?zws-id=' . static::$zws_id . '&address=' . $address . '&citystatezip=' . $citystatezip; |
|
|
|
|
134
|
|
|
$xml = simplexml_load_file( $request ); |
135
|
|
|
$json = wp_json_encode( $xml ); |
136
|
|
|
$deep_results = json_decode( $json, true ); |
137
|
|
|
return $deep_results; |
138
|
|
|
} |
139
|
|
|
/** |
140
|
|
|
* Get Deep Comps. |
141
|
|
|
* |
142
|
|
|
* @access public |
143
|
|
|
* @param mixed $zpid ZPID. |
144
|
|
|
* @param string $count (default: '5') Count. |
145
|
|
|
* @param bool $rentzestimate (default: false) Rent Zestimate. |
146
|
|
|
* @return Request. |
|
|
|
|
147
|
|
|
*/ |
148
|
|
View Code Duplication |
function get_deep_comps( $zpid, $count = '5', $rentzestimate = false ) { |
|
|
|
|
149
|
|
|
if ( empty( $zpid ) ) { |
150
|
|
|
return new WP_Error( 'required-fields', __( 'Required fields are empty.', 're-pro' ) ); |
151
|
|
|
} |
152
|
|
|
$request = $this->base_uri . '/GetDeepComps.htm?zws-id=' . static::$zws_id . '&zpid=' . $zpid . '&count=' . $count; |
|
|
|
|
153
|
|
|
$xml = simplexml_load_file( $request ); |
154
|
|
|
$json = wp_json_encode( $xml ); |
155
|
|
|
$deep_comps = json_decode( $json, true ); |
156
|
|
|
return $deep_comps; |
157
|
|
|
} |
158
|
|
|
/** |
159
|
|
|
* Get Updated Property Details. |
160
|
|
|
* |
161
|
|
|
* @access public |
162
|
|
|
* @param mixed $zpid ZPID. |
163
|
|
|
* @return Request. |
|
|
|
|
164
|
|
|
*/ |
165
|
|
|
function get_updated_property_details( $zpid ) { |
|
|
|
|
166
|
|
|
if ( empty( $zpid ) ) { |
167
|
|
|
return new WP_Error( 'required-fields', __( 'Required fields are empty.', 're-pro' ) ); |
168
|
|
|
} |
169
|
|
|
$request = $this->base_uri . '/GetUpdatedPropertyDetails.htm?zws-id=' . static::$zws_id . '&zpid=' . $zpid; |
|
|
|
|
170
|
|
|
$xml = simplexml_load_file( $request ); |
171
|
|
|
$json = wp_json_encode( $xml ); |
172
|
|
|
$prop_details = json_decode( $json, true ); |
173
|
|
|
return $prop_details; |
174
|
|
|
} |
175
|
|
|
/** |
176
|
|
|
* Get Search Results. |
177
|
|
|
* |
178
|
|
|
* @access public |
179
|
|
|
* @param mixed $address Address. |
180
|
|
|
* @param mixed $citystatezip City/State/Zip. |
181
|
|
|
* @param bool $rentzestimate (default: false) Rent Zestimate. |
182
|
|
|
* @return Request. |
|
|
|
|
183
|
|
|
*/ |
184
|
|
|
function get_search_results( $address, $citystatezip, $rentzestimate = false ) { |
|
|
|
|
185
|
|
|
if ( empty( $address ) && empty( $citystatezip ) ) { |
186
|
|
|
return new WP_Error( 'required-fields', __( 'Required fields are empty.', 're-pro' ) ); |
187
|
|
|
} |
188
|
|
|
$request = $this->base_uri . '/GetUpdatedPropertyDetails.htm?zws-id=' . static::$zws_id . '&address=' . $address . '&citystatezip=' . $citystatezip; |
|
|
|
|
189
|
|
|
$xml = simplexml_load_file( $request ); |
190
|
|
|
$json = wp_json_encode( $xml ); |
191
|
|
|
$search_results = json_decode( $json, true ); |
192
|
|
|
return $search_results; |
193
|
|
|
} |
194
|
|
|
/** |
195
|
|
|
* Get Zillow Zestimate. |
196
|
|
|
* http://wern-ancheta.com/blog/2014/03/20/getting-started-with-zillow-api/ |
197
|
|
|
* https://github.com/letsgetrandy/wp-zestimate |
198
|
|
|
* |
199
|
|
|
* @access public |
200
|
|
|
* @param mixed $zpid ZPID. |
201
|
|
|
* @return Request. |
|
|
|
|
202
|
|
|
*/ |
203
|
|
|
function get_zestimate( $zpid ) { |
|
|
|
|
204
|
|
|
if ( empty( $zpid ) ) { |
205
|
|
|
return new WP_Error( 'required-fields', __( 'Required fields are empty.', 're-pro' ) ); |
206
|
|
|
} |
207
|
|
|
$request = $this->base_uri . '/GetZestimate.htm?zws-id=' . static::$zws_id . '&zpid=' . $zpid; |
|
|
|
|
208
|
|
|
$xml = simplexml_load_file( $request ); |
209
|
|
|
$json = wp_json_encode( $xml ); |
210
|
|
|
$zestimate = json_decode( $json, true ); |
211
|
|
|
return $zestimate; |
212
|
|
|
} |
213
|
|
|
/** |
214
|
|
|
* Get Chart. |
215
|
|
|
* |
216
|
|
|
* @access public |
217
|
|
|
* @param mixed $zpid ZPID. |
218
|
|
|
* @param mixed $unit_type Unit Type. |
219
|
|
|
* @param string $width (default: '600') Width. |
220
|
|
|
* @param string $height (default: '300') Height. |
221
|
|
|
* @param string $chart_duration (default: '1year') Chart Duration. |
222
|
|
|
* @return Request. |
|
|
|
|
223
|
|
|
*/ |
224
|
|
View Code Duplication |
function get_chart( $zpid, $unit_type, $width = '600', $height = '300', $chart_duration = '1year' ) { |
|
|
|
|
225
|
|
|
if ( empty( $zpid ) && empty( $unit_type ) ) { |
226
|
|
|
return new WP_Error( 'required-fields', __( 'Required fields are empty.', 're-pro' ) ); |
227
|
|
|
} |
228
|
|
|
$request = $this->base_uri . '/GetChart.htm?zws-id=' . static::$zws_id . '&unit-type=' . $unit_type . '&zpid=' . $zpid; |
|
|
|
|
229
|
|
|
$xml = simplexml_load_file( $request ); |
230
|
|
|
$json = wp_json_encode( $xml ); |
231
|
|
|
$chart = json_decode( $json, true ); |
232
|
|
|
return $chart; |
233
|
|
|
} |
234
|
|
|
/** |
235
|
|
|
* Get Comps. |
236
|
|
|
* |
237
|
|
|
* @access public |
238
|
|
|
* @param mixed $zpid ZPID. |
239
|
|
|
* @param mixed $count Count. |
240
|
|
|
* @param bool $rentzestimate (default: false) Rent Zestimate. |
241
|
|
|
* @return Request. |
|
|
|
|
242
|
|
|
*/ |
243
|
|
|
function get_comps( $zpid, $count, $rentzestimate = false ) { |
|
|
|
|
244
|
|
|
if ( empty( $zpid ) && empty( $count ) ) { |
245
|
|
|
return new WP_Error( 'required-fields', __( 'Required fields are empty.', 're-pro' ) ); |
246
|
|
|
} |
247
|
|
|
$request = $this->base_uri . '/GetComps.htm?zws-id=' . static::$zws_id . '&zpid=' . $zpid . '&count=' . $count; |
|
|
|
|
248
|
|
|
$xml = simplexml_load_file( $request ); |
249
|
|
|
$json = wp_json_encode( $xml ); |
250
|
|
|
$comps = json_decode( $json, true ); |
251
|
|
|
return $comps; |
252
|
|
|
} |
253
|
|
|
/** |
254
|
|
|
* Get the ZPID from a Zillow Property Url. |
255
|
|
|
* |
256
|
|
|
* @access public |
257
|
|
|
* @param mixed $url URL. |
258
|
|
|
* @return Request. |
|
|
|
|
259
|
|
|
*/ |
260
|
|
View Code Duplication |
function get_zpid_from_url( $url ) { |
|
|
|
|
261
|
|
|
if ( empty( $url ) ) { |
262
|
|
|
return new WP_Error( 'required-fields', __( 'Please provide a URL.', 're-pro' ) ); |
263
|
|
|
} |
264
|
|
|
preg_match( '!\d+_zpid!', $url, $matches ); |
265
|
|
|
$final_match = preg_replace( '/_zpid/', '', $matches ); |
266
|
|
|
return $final_match['0']; |
267
|
|
|
} |
268
|
|
|
/** |
269
|
|
|
* Get Agent/Team Screenname from URL. |
270
|
|
|
* |
271
|
|
|
* @access public |
272
|
|
|
* @param mixed $url URL. |
273
|
|
|
* @return void |
274
|
|
|
*/ |
275
|
|
View Code Duplication |
function get_agent_screenname_from_url( $url ) { |
|
|
|
|
276
|
|
|
if ( empty( $url ) ) { |
277
|
|
|
return new WP_Error( 'required-fields', __( 'Please provide a URL.', 're-pro' ) ); |
278
|
|
|
} |
279
|
|
|
$final_match = preg_replace( '/\/(\d+)$/', '', explode('/', $url) ); |
280
|
|
|
return $final_match[4]; |
281
|
|
|
} |
282
|
|
|
/** |
283
|
|
|
* Response code message for GetSearchResults. |
284
|
|
|
* |
285
|
|
|
* @param [String] $code : Response code to get message from. |
|
|
|
|
286
|
|
|
* @return [String] : Message corresponding to response code sent in. |
|
|
|
|
287
|
|
|
*/ |
288
|
|
|
public function response_code_msg( $code = '' ) { |
289
|
|
|
switch ( $code ) { |
290
|
|
|
case 0: |
291
|
|
|
$msg = __( 'Request successfully processed.', 're-pro' ); |
292
|
|
|
break; |
293
|
|
|
case 1: |
294
|
|
|
$msg = __( 'Service error-there was a server-side error while processing the request.', 're-pro' ); |
295
|
|
|
break; |
296
|
|
|
case 2: |
297
|
|
|
$msg = __( 'The specified ZWSID parameter was invalid or not specified in the request.', 're-pro' ); |
298
|
|
|
break; |
299
|
|
|
case 3: |
300
|
|
|
$msg = __( 'Web services are currently unavailable.', 're-pro' ); |
301
|
|
|
break; |
302
|
|
|
case 4: |
303
|
|
|
$msg = __( 'The API call is currently unavailable.', 're-pro' ); |
304
|
|
|
break; |
305
|
|
|
case 500: |
306
|
|
|
$msg = __( 'Invalid or missing address parameter.', 're-pro' ); |
307
|
|
|
break; |
308
|
|
|
case 501: |
309
|
|
|
$msg = __( 'Invalid or missing city, state, zip parameter.', 're-pro' ); |
310
|
|
|
break; |
311
|
|
|
case 502: |
312
|
|
|
$msg = __( 'No results found.', 're-pro' ); |
313
|
|
|
break; |
314
|
|
|
case 503: |
315
|
|
|
$msg = __( 'Failed to resolve city, state or ZIP code.', 're-pro' ); |
316
|
|
|
break; |
317
|
|
|
case 504: |
318
|
|
|
$msg = __( 'No coverage for specified area.', 're-pro' ); |
319
|
|
|
break; |
320
|
|
|
case 505: |
321
|
|
|
$msg = __( 'Timeout.', 're-pro' ); |
322
|
|
|
break; |
323
|
|
|
case 506: |
324
|
|
|
$msg = __( 'Address string too long.', 're-pro' ); |
325
|
|
|
break; |
326
|
|
|
case 507: |
327
|
|
|
$msg = __( 'No exact match found.', 're-pro' ); |
328
|
|
|
break; |
329
|
|
|
default: |
330
|
|
|
$msg = __( 'Sorry, response code is unknown.' ); |
331
|
|
|
break; |
332
|
|
|
} |
333
|
|
|
return $msg; |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.