Created
August 5, 2011 09:41
-
-
Save MasterEx/1127215 to your computer and use it in GitHub Desktop.
Motivated by this: http://stackoverflow.com/questions/6953833/using-googleviewer-for-displaying-documents-in-the-localhost/6954278#6954278 here is how you can take your public ip in php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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