Skip to content

Instantly share code, notes, and snippets.

@parsibox
Created January 19, 2024 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parsibox/c3ae6ac4655971788453bcde81cd5ea9 to your computer and use it in GitHub Desktop.
Save parsibox/c3ae6ac4655971788453bcde81cd5ea9 to your computer and use it in GitHub Desktop.
server ip with api
<?php
// Make a request to ipify API
$apiUrl = 'https://api.ipify.org?format=json';
$response = file_get_contents($apiUrl);
// Check if the request was successful
if ($response !== false) {
// Decode the JSON response
$data = json_decode($response, true);
// Extract and display the external IP address
if (isset($data['ip'])) {
$externalIP = $data['ip'];
echo "External IP Address: $externalIP";
} else {
echo "Unable to retrieve IP address from the API.";
}
} else {
echo "Failed to connect to the API.";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment