官方網址:
https://www.ipify.org/
API說明:
API URI | Response Type | Sample Output (IPv4) |
https://api.ipify.org | text | 98.207.254.136 |
https://api.ipify.org?format=json | json | {“ip”:”98.207.254.136″} |
https://api.ipify.org?format=jsonp | jsonp | callback({“ip”:”98.207.254.136″}); |
https://api.ipify.org?format=jsonp&callback=getip | jsonp | getip({“ip”:”98.207.254.136″}); |
使用範例:
Bash
#!/bin/bash
ip=$(curl -s https://api.ipify.org)
echo "My public IP address is: $ip"
Python
# This example requires the requests library be installed. You can learn more
# about the Requests library here: http://docs.python-requests.org/en/latest/
from requests import get
ip = get('https://api.ipify.org').text
print 'My public IP address is:', ip
Ruby
require "net/http"
ip = Net::HTTP.get(URI("https://api.ipify.org"))
puts "My public IP Address is: " + ip
PHP
<?php
$ip = file_get_contents('https://api.ipify.org');
echo "My public IP address is: " . $ip;
?>
Perl
use strict;
use warnings;
use LWP::UserAgent;
my $ua = new LWP::UserAgent();
my $ip = $ua->get('https://api.ipify.org')->content;
print 'My public IP address is: '. $ip;
VB.NET
Dim client as New System.Net.WebClient
Dim ip as String = client.DownloadString("https://api.ipify.org")
NodeJS
var http = require('http');
http.get({'host': 'api.ipify.org', 'port': 80, 'path': '/'}, function(resp) {
resp.on('data', function(ip) {
console.log("My public IP address is: " + ip);
});
});
Javascript
<script type="application/javascript">
function getIP(json) {
document.write("My public IP address is: ", json.ip);
}
</script>
<script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"></script>
jQuery
<script type="application/javascript">
$(function() {
$.getJSON("https://api.ipify.org?format=jsonp&callback=?",
function(json) {
document.write("My public IP address is: ", json.ip);
}
);
});
</script>
C#
using System;
using System.Net;
namespace Ipify.Examples {
class Program {
public static void Main (string[] args) {
WebClient webClient = new WebClient();
string publicIp = webClient.DownloadString("https://api.ipify.org");
Console.WriteLine("My public IP Address is: {0}", publicIp);
}
}
}
Arduino
if (client.connect("api.ipify.org", 80)) {
Serial.println("connected");
client.println("GET / HTTP/1.0");
client.println("Host: api.ipify.org");
client.println();
} else {
Serial.println("connection failed");
}
而且還有好心人幫忙多包了一層Object, 讓程式更好用:
ex:
https://github.com/rdegges/python-ipify
To install
ipify
, simply run:$ pip install ipify
This will install the latest version of the library automatically.
Usage
Using this library is very simple. Here's a simple example:
>>> from ipify import get_ip
>>> ip = get_ip()
>>> ip
u'96.41.136.144'
沒有留言:
張貼留言