るいすのブログ

オタクエンジニアの雑記

PHPでwhoisをJSONで返すAPI「whoisAPI」を作った


暑い

なんで作ったかというとgithubに慣れるためなんて言えない。 whois API って地味に提供してるサービスはあるのですがフリーだと500reqまでだとか使い勝手が悪かった。

 

使い方

サーバードキュメントルートに設置してくれればいいです。

[bash] whoisAPI ├── api.php ├── count.txt └── get.php [/bash]

 

受け取り側はPOSTなので投げるときもPOSTでお願いします。

<?php
$url = "http://xzy.pw/whoisAPI/api.php";
$data = array(
    "ip" => "36.2.145.190",
);
$options = array(
    'http' => array(
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$result = json_decode($result);
var_dump($result);

 

こんな感じにJSONで返ってきます。

object(stdClass)#1 (4) {
  ["ip"]=>
  string(12) "36.2.145.190"
  ["description"]=>
  string(28) "ARTERIA Networks Corporation"
  ["country"]=>
  string(2) "JP"
  ["inetnum"]=>
  string(23) "36.2.0.0 - 36.3.255.255"
}

投げたIP、descr、国名、IPの範囲が返ってきます。

リクエスト先は http://xzy.pw/whoisAPI/api.php githubにも置いておきます whoisAPI