Skip to content

Instantly share code, notes, and snippets.

@MasterEx
Created August 5, 2011 09:41
Show Gist options
  • Save MasterEx/1127215 to your computer and use it in GitHub Desktop.
Save MasterEx/1127215 to your computer and use it in GitHub Desktop.
<?php
/**
* Retrieve public ip with PHP cURL
*/
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'http://whatismyip.org/');
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
$public_ip = curl_exec($curl_handle);
curl_close($curl_handle);
echo "my ip address is ".$public_ip;
/**
* It works as long as whatsmyip.org is up and running.
* We get the public ip address even if we call the script
* from it's public ip, localhost, 127.0.0.* or it's lan ip.
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment