40 protected function sendRequest($ipAddress, $userId, $licenseKey)
44 $httpClient->setAuthorization($userId, $licenseKey);
46 $httpRes = $httpClient->get($this->
getEndpoint() . $ipAddress .
'?pretty');
48 $errors = $httpClient->getError();
50 if (!$httpRes && !empty($errors))
54 foreach($errors as $errorCode => $errMes)
55 $strError .= $errorCode.
": ".$errMes;
57 $result->addError(
new Error($strError));
61 $status = $httpClient->getStatus();
64 $result->addError(
new Error(
'Http status: '.$status));
66 $arRes = json_decode($httpRes,
true);
72 $result->setData($arRes);
76 $result->addError(
new Error(
'['.$arRes[
'code'].
'] '.$arRes[
'error']));
81 $result->addError(
new Error(
'Can\'t decode json result'));
118 $dataResult =
new Result();
119 $geoData =
new Data();
121 $geoData->lang = ($lang <>
'' ? $lang :
'en');
123 if($this->config[
'USER_ID'] ==
'' || $this->config[
'LICENSE_KEY'] ==
'')
129 $res = $this->
sendRequest($ip, $this->config[
'USER_ID'], $this->config[
'LICENSE_KEY']);
131 if($res->isSuccess())
133 $lang = $geoData->lang;
135 $data = $res->getData();
137 if (strtolower(SITE_CHARSET) !=
'utf-8')
139 $data = Encoding::convertEncoding($data,
'UTF-8', SITE_CHARSET);
142 $geoData->ipNetwork = $data[
'traits'][
'network'] ??
null;
144 $geoData->continentCode = $data[
'continent'][
'code'] ??
null;
145 $geoData->continentName = $data[
'continent'][
'names'][$lang] ?? $data[
'continent'][
'name'] ??
null;
147 $geoData->countryCode = $data[
'country'][
'iso_code'] ??
null;
148 $geoData->countryName = $data[
'country'][
'names'][$lang] ?? $data[
'country'][
'name'] ??
null;
150 $geoData->regionCode = $data[
'subdivisions'][0][
'iso_code'] ??
null;
151 $geoData->regionGeonameId = $data[
'subdivisions'][0][
'geoname_id'] ??
null;
152 $geoData->regionName = $data[
'subdivisions'][0][
'names'][$lang] ?? $data[
'subdivisions'][0][
'name'] ??
null;
154 if ($geoData->regionGeonameId)
156 $geoData->geonames[$geoData->regionGeonameId] = $res->getData()[
'subdivisions'][0][
'names'] ?? [];
159 $geoData->subRegionCode = $data[
'subdivisions'][1][
'iso_code'] ??
null;
160 $geoData->subRegionGeonameId = $data[
'subdivisions'][1][
'geoname_id'] ??
null;
161 $geoData->subRegionName = $data[
'subdivisions'][1][
'names'][$lang] ?? $data[
'subdivisions'][1][
'name'] ??
null;
163 if ($geoData->subRegionGeonameId)
165 $geoData->geonames[$geoData->subRegionGeonameId] = $res->getData()[
'subdivisions'][1][
'names'] ?? [];
168 $geoData->cityName = $data[
'city'][
'names'][$lang] ?? $data[
'city'][
'name'] ??
null;
169 $geoData->cityGeonameId = $data[
'city'][
'geoname_id'] ??
null;
171 if ($geoData->cityGeonameId)
173 $geoData->geonames[$geoData->cityGeonameId] = $res->getData()[
'city'][
'names'] ?? [];
176 $geoData->latitude = $data[
'location'][
'latitude'] ??
null;
177 $geoData->longitude = $data[
'location'][
'longitude'] ??
null;
178 $geoData->timezone = $data[
'location'][
'time_zone'] ??
null;
180 $geoData->zipCode = $data[
'postal'][
'code'] ??
null;
182 $geoData->ispName = $data[
'traits'][
'isp'] ??
null;
183 $geoData->organizationName = $data[
'traits'][
'organization'] ??
null;
184 $geoData->asn = $data[
'traits'][
'autonomous_system_number'] ??
null;
185 $geoData->asnOrganizationName = $data[
'traits'][
'autonomous_system_organization'] ??
null;
189 $dataResult->addErrors($res->getErrors());
192 $dataResult->setGeoData($geoData);