PHP code example of yzh52521 / think-geoip
1. Go to this page and download the library: Download yzh52521/think-geoip library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yzh52521 / think-geoip example snippets
geoip($ip = null);
\yzh52521\GeoIP\Location {
#attributes:array [
'ip' => '232.223.11.11',
'iso_code' => 'US',
'country' => 'United States',
'city' => 'New Haven',
'state' => 'CT',
'state_name' => 'Connecticut',
'postal_code' => '06510',
'lat' => 41.28,
'lon' => -72.88,
'timezone' => 'America/New_York',
'continent' => 'NA',
'currency' => 'USD',
'default' => false,
]
}
php think geoip:publish
php think geoip:update
php think geoip:clear
namespace App\GeoIP\Services;
use Exception;
use yzh52521\GeoIP\support\HttpClient;
use yzh52521\GeoIP\services\AbstractService;
class FooBar extends AbstractService
{
/**
* Http client instance.
*
* @var HttpClient
*/
protected $client;
/**
* The "booting" method of the service.
*
* @return void
*/
public function boot()
{
$this->client = new HttpClient([
'base_uri' => 'http://api.foobar-ip-to-location.com/',
'query' => [
'some_option' => $this->config('some_option'),
],
]);
}
/**
* {@inheritdoc}
*/
public function locate($ip)
{
// Get data from client
$data = $this->client->get('find', [
'ip' => $ip,
]);
// Verify server response
if ($this->client->getErrors() !== null) {
throw new Exception('Request failed (' . $this->client->getErrors() . ')');
}
// Parse body content
$json = json_decode($data[0]);
return [
'ip' => $ip,
'iso_code' => $json->iso_code,
'country' => $json->country,
'city' => $json->city,
'state' => $json->state,
'state_name' => $json->state_name,
'postal_code' => $json->postal_code,
'lat' => $json->lat,
'lon' => $json->lon,
'timezone' => $json->timezone,
'continent' => $json->continent,
];
}
/**
* Update function for service.
*
* @return string
*/
public function update()
{
// Optional artisan command line update method
}
}