Created
October 16, 2018 18:01
-
-
Save andig/10df6f4d703c28d64dd9508f71e4ddbc to your computer and use it in GitHub Desktop.
What's my IP, Fritz!Box?
This file contains 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 | |
/* | |
* PHP-only solution for obtaining external IP from Fritz!Box | |
* | |
* Based on https://stackoverflow.com/questions/5647461/how-do-i-send-a-post-request-with-php#6609181 | |
* brought to you by [email protected] | |
*/ | |
// curl -s "http://fritz.box:49000/igdupnp/control/WANIPConn1" -H "Content-Type: text/xml; charset="utf-8"" -H "SoapAction:urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress" -d "@external_ip.xml" | grep -Eo "\<[[:digit:]]{1,3}(\.[[:digit:]]{1,3}){3}\>" | |
$tr64 = <<<EOD | |
<?xml version="1.0" encoding="utf-8" ?> | |
<s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> | |
<s:Body> | |
<u:GetExternalIPAddress xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1" /> | |
</s:Body> | |
</s:Envelope> | |
EOD; | |
$headers = <<<EOD | |
Content-Type: text/xml; charset="utf-8" | |
SoapAction:urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress | |
EOD; | |
$url = 'http://fritz.box:49000/igdupnp/control/WANIPConn1'; | |
// use key 'http' even if you send the request to https://... | |
$options = array( | |
'http' => array( | |
'header' => $headers, | |
'method' => 'POST', | |
'content' => $tr64 | |
) | |
); | |
$context = stream_context_create($options); | |
$result = file_get_contents($url, false, $context); | |
if ($result === FALSE || !preg_match("/ExternalIPAddress>([0-9.]+)</", $result, $m)) { | |
var_dump($result); | |
} | |
else { | |
echo $m[1]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment