<?php
 class vB_Utility_Geoip_Ipstack extends vB_Utility_Geoip { private $urlLoader; private $key; private $useSecure = false; public function __construct($data) { $this->urlLoader = $data['urlLoader']; $this->key = $data['key']; } public function getLocation($address) { $data = $this->getIpData($address); $data = json_decode($data, true); if (!$data) { throw new Exception('Invalid response from IP Stack'); } if (isset($data['error'])) { throw new Exception('IPStack error: ' . $data['error']['info']); } return $data['country_code'] . ':' . $data['region_code']; } public function getIpData($address) { if (!$this->key) { throw new Exception('Application key not provided for IP Stack request'); } $query = [ 'access_key' => $this->key, 'fields' => 'country_code,region_code', ]; $url = 'http'; if ($this->useSecure) { $url .= 's'; } $url .= '://api.ipstack.com/' . urlencode($address) . '?' . http_build_query($query); $data = $this->urlLoader->get($url); return $data['body']; } } 