Skip to content

Instantly share code, notes, and snippets.

@restyler
Created November 1, 2020 09:03
Show Gist options
  • Save restyler/d4282ee68e72605bac743f89594fd9b9 to your computer and use it in GitHub Desktop.
Save restyler/d4282ee68e72605bac743f89594fd9b9 to your computer and use it in GitHub Desktop.
Realtor.com API get recently listed properties for sale
<?php
// get your rapidapi key for API access on https://rapidapi.com/restyler/api/realtor-com1
define('RAPIDAPI_KEY', 'YOUR-KEY');
if (RAPIDAPI_KEY == 'YOUR-KEY') throw new \Exception('Get your API key before using the script!');
$curl = curl_init();
$search_params = [
'search' => '19720',
'year_built_min' => '1980',
'year_built_max' => '2020',
'list_date_min' => '2020-09-01',
'sort_list_date' => 'desc'
];
curl_setopt_array($curl, array(
CURLOPT_URL => "https://realtor-com1.p.rapidapi.com/v1/buy?" . http_build_query($search_params),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"x-rapidapi-host: realtor-com1.p.rapidapi.com",
"x-rapidapi-key: " . RAPIDAPI_KEY
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$res_json = json_decode($response);
if (is_array($res_json->data->home_search->results)) {
foreach ($res_json->data->home_search->results as $property) {
echo 'Url: ' . $property->href . "\n";
echo 'List price: ' . $property->list_price . "\n";
echo 'Listing id: ' . $property->listing_id . "\n"; // there is also $property_id
echo 'List date:' . $property->list_date . "\n";
echo 'Location: ' . print_r($property->location, 1) . "\n";
}
} else {
echo 'Bad or empty response:' . $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment