2018年11月24日土曜日

グローバルIPアドレスの国を確認して返すWebAPI

先日、グローバルIPの国を確認するサイトを作成しました。
グローバルIPアドレスの国を確認するサイトを作りました!

今回はこれを少しカスタマイズして、WebAPI版を作成してみました。

アクセスポイントは以下のアドレスになります。
https://api.suama.onl/v1/ip-country.php

で、ipaddrをGETで渡す必要があります。

例えば、正しいグローバルIPを入れた場合は以下のようになります。

【実行例】
curl https://api.suama.onl/v1/ip-country.php?ipaddr=1.1.1.1 |jq

【実行結果】
{
  "message": "OK:The search target IP address is a global IP address, registration information existed!",
  "code": "200",
  "result": {
    "ip": "1.1.1.1",
    "hostname": "one.one.one.one",
    "countrycode": "AU",
    "countryname_ja": "オーストラリア",
    "countryname_en": "Australia",
    "RIR": "apnic",
    "status": "assigned",
    "date": "2011/08/11"
  }
}

正常に国を取得できないIPアドレスを指定した場合、以下のような結果になります。

【実行例】
curl https://api.suama.onl/v1/ip-country.php?ipaddr=192.168.1.1 |jq

【実行結果】
{
  "message": "NG:Search target IP address is private IP address!",
  "code": "400",
  "result": {
    "ip": "192.168.1.1"
  }
}

入力がなかったり、IPアドレス以外の入力の場合、以下のようになります。

【実行例】
curl https://api.suama.onl/v1/ip-country.php

【実行結果】
{
  "message": "NG:No input!",
  "code": "400"
}

そろそろAPIについて纏めたページを作らないとダメですね。。。

2018年11月13日火曜日

自分のグローバルIPアドレス(接続元IP)を返すWebAPI

最近APIで色々処理できるものが増えてきたのですが、プログラムの実行環境も色々なパターンがあるので、自分のグローバルIPを確認できるAPIがあると便利ではないか、と思いました。でもまあ、こういうちょっと便利なものって、大抵誰かが作っているんですよね。調べてみると、やはりありました。

こちらの記事に纏まっています。
グローバルIPをcurlで確認

以下、抜粋です。
グローバルIPをcurlで確認 
・httpbin.org/ip
$ curl httpbin.org/ip
{
  "origin": "121.102.14.91"
・inet-ip.info
$ curl inet-ip.info
121.102.14.91 
・ifconfig.me
$ curl ifconfig.me
121.102.14.91
でも折角なので、2つほど作ってみました。

IPアドレスを単純に返すだけのもの
$ curl https://api.suama.onl/v1/ip.php
1.1.1.1

json形式でIPアドレスとホスト名を返すもの
$ curl https://api.suama.onl/v1/ip-hostname.php |jq
{
  "ip": "1.1.1.1",
  "hostname": " one.one.one.one"
}

ご自由にご利用ください!