Getting the public IP in PHP — no dependencies, no API key
PHP is still one of the most widely deployed server-side languages, running a significant share of the web's backend code. If you're building a PHP application that needs the public IP address — for geolocation, DDNS, diagnostics, or country detection — this article covers the common patterns using IPPubblico.org: free, no key, HTTPS, JSON and plain text endpoints.
Use case 1 — Your server's own public IP (one-liner)
The simplest case: a PHP script that needs to know its own public IP.
<?php






