File: //var/www/elite/coordParser/CommimParser.php
<?php
namespace coordParser;
use stdClass;
class CommimParser
{
const URL = 'https://commim.spb.ru/Web/api/RealEstate/Realty';
private function request(int $page = 0)
{
$data = [
'page' => $page,
'pageSize' => 20,
'filter' => new stdClass(),
'sortColumns' => [],
];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, self::URL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'User-Agent: PostmanRuntime/7.34.0',
'Content-Type: application/json',
]);
$response = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Ошибка cURL: ' . curl_error($curl);
}
curl_close($curl);
return $response;
}
public function getData(int $page = 0)
{
return json_decode($this->request($page));
}
public function parseData(): array
{
$table = [];
$data = $this->getData();
$count = $data->filtered;
$offset = 20;
print_r('Найдено: ' . $count . PHP_EOL);
$totalPages = round($count / $offset);
print_r('Всего страниц: ' . $totalPages . PHP_EOL);
$page = 0;
$items = $data->items;
do {
print_r('Страница: ' . $page . '/' . $totalPages . PHP_EOL);
foreach ($items as $item) {
$row = [];
foreach ($item as $key => $value) {
$row[] = $value ?? ' ';
}
$table[] = $row;
}
$data = $this->getData(++$page);
$items = $data->items;
} while (!empty($items));
return $table;
}
}