php
php copied to clipboard
Retrieving your public IPv6 address
I am trying to retrieve my public IPv6 address but the getDetails() function, with $ip_address paramater set to null, only returns the IPv4 address. i have tried changing the const API_URL of the IPinfo class to 'https://v6.ipinfo.io' but somehow that leads to an curl error 7 which i do not understand. because if i use curl directly it works.
curl ipinfo.io/?token=mytoken
returns json with ip as ipv4 address
curl ipinfo.io/ip/?token=mytoken
returns string ip as ipv4 address
curl v6.ipinfo.io/ip/?token=mytoken
returns string ip as ipv6 address
curl https://v6.ipinfo.io/ip/?token=mytoken
returns string ip as ipv6 address
any idea why the curl error?
I know that this is an old issue, and perhaps is resolved. If not I could give the following suggestion:
- Verify that you run the
curlfrom the command line from the same machine that you run thephpcode from. - Verify PHP cURL IPv6 Support Check if your PHP cURL installation supports IPv6:
<?php
$curl_version = curl_version();
echo "cURL Version: " . $curl_version['version'] . "\n";
echo "Features: " . $curl_version['features'] . "\n";
echo "IPv6 Support: " . ($curl_version['features'] & CURL_VERSION_IPV6 ? "Yes" : "No") . "\n";
?>
- Test Direct cURL Request in PHP
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://v6.ipinfo.io/ip?token=your_token_here");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6); // Force IPv6
$result = curl_exec($ch);
if ($result === false) {
echo "cURL Error: " . curl_error($ch) . " (Code: " . curl_errno($ch) . ")\n";
} else {
echo "Result: " . $result . "\n";
}
curl_close($ch);
?>