83 lines
2.5 KiB
PHP
83 lines
2.5 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Http\Controllers;
|
||
|
|
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use GuzzleHttp\Client;
|
||
|
|
use Illuminate\Support\Facades\Log;
|
||
|
|
|
||
|
|
class IPGeolocationController extends Controller
|
||
|
|
{
|
||
|
|
protected $client;
|
||
|
|
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
$this->client = new Client([
|
||
|
|
'timeout' => 10,
|
||
|
|
'verify' => false
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function index()
|
||
|
|
{
|
||
|
|
return view('ip-geolocation.index');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function lookup(Request $request)
|
||
|
|
{
|
||
|
|
Log::info('IP Lookup request received', ['ip' => $request->ip]);
|
||
|
|
|
||
|
|
$request->validate([
|
||
|
|
'ip' => 'required|ip'
|
||
|
|
]);
|
||
|
|
|
||
|
|
try {
|
||
|
|
Log::info('Making request to IP-API', ['ip' => $request->ip]);
|
||
|
|
|
||
|
|
$response = $this->client->get("http://ip-api.com/json/{$request->ip}");
|
||
|
|
|
||
|
|
Log::info('IP-API response received', [
|
||
|
|
'status' => $response->getStatusCode(),
|
||
|
|
'body' => (string) $response->getBody()
|
||
|
|
]);
|
||
|
|
|
||
|
|
if ($response->getStatusCode() === 200) {
|
||
|
|
$data = json_decode($response->getBody(), true);
|
||
|
|
|
||
|
|
if ($data['status'] === 'success') {
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'data' => [
|
||
|
|
'country' => $data['country'],
|
||
|
|
'city' => $data['city'],
|
||
|
|
'region' => $data['regionName'],
|
||
|
|
'timezone' => $data['timezone'],
|
||
|
|
'isp' => $data['isp'],
|
||
|
|
'org' => $data['org'],
|
||
|
|
'as' => $data['as'],
|
||
|
|
'latitude' => $data['lat'],
|
||
|
|
'longitude' => $data['lon'],
|
||
|
|
'zip' => $data['zip']
|
||
|
|
]
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'success' => false,
|
||
|
|
'message' => 'Could not find location information for this IP.'
|
||
|
|
], 404);
|
||
|
|
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
Log::error('IP Lookup error', [
|
||
|
|
'ip' => $request->ip,
|
||
|
|
'error' => $e->getMessage()
|
||
|
|
]);
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'success' => false,
|
||
|
|
'message' => 'An error occurred while looking up the IP: ' . $e->getMessage()
|
||
|
|
], 500);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|