PHPackages                             knotsphp/publicip - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. knotsphp/publicip

ActiveLibrary[API Development](/categories/api)

knotsphp/publicip
=================

Fast and reliable way to get your public IP address with PHP

v1.2.0(1y ago)476.0k↑50%3MITPHPPHP ^8.3CI passing

Since Nov 17Pushed 1y ago1 watchersCompare

[ Source](https://github.com/knotsphp/publicip)[ Packagist](https://packagist.org/packages/knotsphp/publicip)[ GitHub Sponsors](https://github.com/SRWieZ)[ RSS](/packages/knotsphp-publicip/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (5)Versions (10)Used By (0)

PublicIP
========

[](#publicip)

[![Latest Stable Version](https://camo.githubusercontent.com/a5516e90fa408700b0bf3a36f509a20e470cfab99b11bbca0daea274ecd735bf/68747470733a2f2f706f7365722e707567782e6f72672f6b6e6f74737068702f7075626c696369702f76)](https://packagist.org/packages/knotsphp/publicip)[![Total Downloads](https://camo.githubusercontent.com/672096e588101b4355870b6b0a90df2e2ab75e312963eb111163e795f1703e23/68747470733a2f2f706f7365722e707567782e6f72672f6b6e6f74737068702f7075626c696369702f646f776e6c6f616473)](https://packagist.org/packages/knotsphp/publicip)[![Latest Unstable Version](https://camo.githubusercontent.com/d5f339857f0c9fa8819674fc45d8b4b3181c699287ed78b2d839127b4ff6a124/68747470733a2f2f706f7365722e707567782e6f72672f6b6e6f74737068702f7075626c696369702f762f756e737461626c65)](https://packagist.org/packages/knotsphp/publicip)[![License](https://camo.githubusercontent.com/9fcea14fc29685c9c37b7999b28a6f69754e9d2ee451b42ae475209500474095/68747470733a2f2f706f7365722e707567782e6f72672f6b6e6f74737068702f7075626c696369702f6c6963656e7365)](https://packagist.org/packages/knotsphp/publicip)[![PHP Version Require](https://camo.githubusercontent.com/1d92af8035638499f759b9cdf7d27fa77238791d01462d8cd43d7e5671f85568/68747470733a2f2f706f7365722e707567782e6f72672f6b6e6f74737068702f7075626c696369702f726571756972652f706870)](https://packagist.org/packages/knotsphp/publicip)[![GitHub Workflow Status (with event)](https://camo.githubusercontent.com/170155ddcec49d347b37fa8b4e49a061a4e66f265ecaee3568f5eee93351b984/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6b6e6f74737068702f7075626c696369702f746573742e796d6c3f6c6162656c3d5465737473)](https://github.com/knotsphp/publicip/actions/workflows/test.yml)

A simple PHP library to get your public IP address reliably and fast.

This library uses `dig` or HTTP requests to obtain the public IP address of the current machine by utilizing publicly available whoami services.

It comes with an opinionated default configuration to use the **fastest** and most **reliable** fetchers and providers. However, it also includes a flexible API that allows you to use different fetchers and different providers.

🚀 Installation
--------------

[](#-installation)

```
composer require knotsphp/publicip
```

📚 Usage
-------

[](#-usage)

Easiest way to get the public IP address of the current machine is to use the `PublicIP::get()` method.

```
use KnotsPHP\PublicIP\Finders\{ PublicIP, PublicIPv4, PublicIPv6 };

$ipv4 = PublicIPv4::get(); // returns your IPv4
$ipv6 = PublicIPv6::get(); // returns your IPv6
$ipv4or6 = PublicIP::get(); // returns either IPv4 or IPv6
```

These methods return the IP address as a `string` or `null` if the fetcher fails. No exceptions are thrown.

If you want to use a specific fetcher, or a specific provider, you can use the `PublicIPv4::finder()->fetch()` method.

```
use KnotsPHP\PublicIP\Enums\DnsProvider;
use KnotsPHP\PublicIP\Fetchers\DigFetcher;
use KnotsPHP\PublicIP\Finders\PublicIPv4;

$ipv4 = PublicIPv4::finder()
    ->addFetcher((new DigFetcher())
        ->from(DnsProvider::OpenDNS)))
    ->fetch();
```

This method gives you more control, but you will need to manage exceptions on your own.

### Advanced Usage

[](#advanced-usage)

You can also use a `Fetcher` directly to get the IP address.

```
use KnotsPHP\PublicIP\Enums\DnsProvider;
use KnotsPHP\PublicIP\Enums\IpVersion;
use KnotsPHP\PublicIP\Fetchers\DigFetcher;

$ipv4 = (new DigFetcher)->from(DnsProvider::Cloudflare)->fetch(IpVersion::v4);
```

Note that this returns null instead of throwing an exception if the fetcher fails.

📚 Use in command line
---------------------

[](#-use-in-command-line)

You can also use this library in the command line by using the `publicip` command.

It's recommended to install the library globally to use it in the command line.

```
composer global require knotsphp/publicip
```

Then you can use the `publicip` command to get the public IP address of the current machine.

```
# In your project directory
vendor/bin/publicip

# Globally installed
publicip

# To get the IPv4 address
publicip --ipv4
publicip -4

# To get the IPv6 address
publicip --ipv6
publicip -6
```

🏃 Performance
-------------

[](#-performance)

If you are sure that your machine has `dig` installed, you can speed up the process by setting the `isSupported`property to `true`.

It works both ways; if you are sure that your machine does not have `dig` installed, you can set it to `false` to prevent unnecessary checks.

```
use KnotsPHP\PublicIP\Fetcher\DigFetcher;

DigFetcher::$isSupported = true;
```

If you use the `CurlFetcher` or `FileGetContentsFetcher`, you can set the `forceHTTP` property to `true` to use HTTP instead of HTTPS. Some whoami services do not support HTTPS anyway as they are meant to be used in scripts like this `curl ifconfig.co`.

```
use KnotsPHP\PublicIP\Fetcher\CurlFetcher;
use KnotsPHP\PublicIP\Fetchers\FileGetContentsFetcher;

CurlFetcher::$forceHTTP = true;
FileGetContentsFetcher::$forceHTTP = true;
```

📖 Documentation
---------------

[](#-documentation)

`dig` provider list

IP4or6IPv4IPv6Reliable?Cloudflare✅✅✅✅Google✅✅OpenDNS✅✅✅⚠️ says they have blocked France &amp; Portugal but seems to work in France in my testingsAkamai⚠️⚠️⚠️⚠️ last digits of the IP can be wrongHTTP whoami provider list

IPv4IPv6IP4or6Noteifconfig.co✅✅✅Fastipify.org✅✅✅Open-sourceifconfig.me✅✅✅icanhazip.com✅✅✅cloudflare.com✅✅✅Use more dataipinfo.io✅⚠️ only IPv4amazonaws.com✅⚠️ only IPv4📋 TODO
------

[](#-todo)

Contributions are welcome!

- use Symfony ExecutableFinder to find dig
- use Symfony Process to run dig
- PSR-18 HTTP fetcher with a way to choose psr compatible client so other tools can monitor outgoing requests

🤝 Contributing
--------------

[](#-contributing)

Clone the project and run `composer update` to install the dependencies.

Before pushing your changes, run `composer qa`.

This will run [pint](http://github.com/laravel/pint) (code style), [phpstan](http://github.com/phpstan/phpstan) (static analysis), and [pest](http://github.com/pestphp/pest) (tests).

📦 Alternatives
--------------

[](#-alternatives)

If you are not pleased with this library, check out the following alternatives:

-

You can also contribute to this library by opening an issue or a pull request.

👥 Credits
---------

[](#-credits)

PublicIP was created by Eser DENIZ.

📝 License
---------

[](#-license)

PublicIP is licensed under the MIT License. See LICENSE for more information.

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance43

Moderate activity, may be stable

Popularity36

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~7 days

Total

4

Last Release

525d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/735894a4f6e39aa85e3d136abc9cf0be92da593d88731382384185293cbfe965?d=identicon)[SRWieZ](/maintainers/SRWieZ)

---

Top Contributors

[![SRWieZ](https://avatars.githubusercontent.com/u/1408020?v=4)](https://github.com/SRWieZ "SRWieZ (44 commits)")

---

Tags

apidigdnsphpwhoami

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/knotsphp-publicip/health.svg)

```
[![Health](https://phpackages.com/badges/knotsphp-publicip/health.svg)](https://phpackages.com/packages/knotsphp-publicip)
```

###  Alternatives

[stripe/stripe-php

Stripe PHP Library

4.0k143.3M480](/packages/stripe-stripe-php)[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M272](/packages/twilio-sdk)[facebook/php-business-sdk

PHP SDK for Facebook Business

90821.9M34](/packages/facebook-php-business-sdk)[meilisearch/meilisearch-php

PHP wrapper for the Meilisearch API

74513.7M114](/packages/meilisearch-meilisearch-php)[google/gax

Google API Core for PHP

265103.1M454](/packages/google-gax)[google/common-protos

Google API Common Protos for PHP

173103.7M50](/packages/google-common-protos)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
