PHP code example of germania-kg / client-location

1. Go to this page and download the library: Download germania-kg/client-location 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/ */

    

germania-kg / client-location example snippets



use Germania\ClientIpLocation\ClientIpLocationMiddleware;
use Psr\Log\LogLevel;

$fn = function($ip) { return "Berlin"; };
$logger = new Monolog(...);

$middleware = new ClientIpLocationMiddleware($fn, $logger);
$middleware = new ClientIpLocationMiddleware($fn, $logger, LogLevel::ERROR);

$middleware->setClientIpAttributeName("client-ip")
           ->setClientLocationAttributeName("client-location");


use Germania\ClientIpLocation\HttpClientLocationCreator;
use Psr\Http\Client\ClientInterface;
use Nyholm\Psr7\Factory\Psr17Factory;

$api = "https://api.geocoder.test/location?ip={{ip}}";
$http_client = new \Http\Adapter\Guzzle6\Client( new \GuzzleHttp\Client );
$request_factory = new Psr17Factory;

$creator = new HttpClientLocationCreator($api, $psr18_client, $request_factory);

use Psr\Http\Message\ResponseInterface;

$decoder = function(ResponseInterface $response) {
  return json_decode($response->getBody(), "assoc");
};

// Pass with constructor
$creator = new HttpClientLocationCreator($api, $psr18_client, $request_factory, $decoder);

// ...or 
$creator->setResponseDecoder( $decoder );

$creator->setDefaultLocation("Berlin");

$logger = new Monolog(...);

// Pass with constructor

$creator = new HttpClientLocationCreator($api, $psr18_client, $request_factory, $decoder, $logger);

// ...or 
$creator->setLogger( $logger );