1. Go to this page and download the library: Download weijiajia/ip-address 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/ */
weijiajia / ip-address example snippets
use Weijiajia\IpConnector;
use Weijiajia\Requests\PconLineRequest;
$connector = new IpConnector();
// 可选: 设置日志
$logger = new YourLoggerImplementation(); // 替换为您的实际日志实现
$connector->withLogger($logger);
//可选:设置超时
$connector->config()->add('timeout',30);
$request = new PconLineRequest('your ip address');
$response = $connector->send($request);
$ipInfo = $response->dto();
echo $ipInfo->getCity(); // 输出城市信息
echo $ipInfo->getAddr(); // 输出详细地址
use Weijiajia\IpConnector;
use Weijiajia\Requests\Ip138Request;
$connector = new IpConnector();
$request = new Ip138Request($token, 'your ip address');
$response = $connector->send($request);
$ipInfo = $response->dto();
echo $ipInfo->getCity(); // 输出城市信息
echo $ipInfo->getAddr(); // 输出详细地址
use Weijiajia\IpConnector;
use Weijiajia\Requests\PconLineRequest;
use Weijiajia\Exceptions\IpLookupException;
$connector = new IpConnector();
try {
$request = new PconLineRequest('invalid_ip');
$response = $connector->send($request);
$ipInfo = $response->dto();
} catch (IpLookupException $e) {
echo "IP 查询失败: " . $e->getMessage();
} catch (\Exception $e) {
echo "发生错误: " . $e->getMessage();
}
namespace Weijiajia\Requests;
use Saloon\Enums\Method;
use Saloon\Http\Request;
class NewProviderRequest extends Request
{
protected Method $method = Method::GET;
public function __construct(protected string $ip)
{
}
public function resolveEndpoint(): string
{
return 'https://api.newprovider.com/ip-lookup';
}
// 实现必要的方法...
public function createDtoFromResponse(Response $response): IpResponse
{
$json = $response->json();
return new IpResponse([
'city' => $response->json('city'),
'addr' => $response->json('addr'),
'ip' => $response->json('ip'),
...
]);
}
}
// 使用新的 IP 地理位置服务提供商
$connector = new IpConnector();
$request = new NewProviderRequest('your ip address');
$response = $connector->send($request);
$ipInfo = $response->dto();
echo $ipInfo->getCity(); // 输出城市信息
echo $ipInfo->getAddr(); // 输出详细地址
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.