cpanel で国名、都市名を表示するにはどうすればよいですか?

cpanel で国名、都市名を表示するにはどうすればよいですか?

私は Cpanel を使用しています。ログイン ページに国名と都市名を表示したいと思います。たとえば、次のようになります。

 Your IP address is :  xxx.xxx.xxx.xxx
 Your Country name is: USA 
 Your City name is:  Seattle

上記の情報については、次の PHP コードで IP アドレスを表示できます。

<?php echo $_SERVER['REMOTE_ADDR']; ?>

国名や都市名などの他のユーザー情報を表示するにはどうすればよいですか?

やり方を順を追って教えてください。

答え1

おそらく、ジオロケーションAPI

ipinfo.ioシンプルなプレーンテキストを返すインターフェイスを提供しますが、多くの地理位置情報 API と同様に、特定の制限を超える場合は有料バージョンのサービスが必要になります (つまり、ipinfo.io の場合は、1 日あたり 1,000 件を超えるリクエストまたは SSL が必要です)。

ipinfo.io の非 JSON バージョンを例にすると、PHP コードは次のようになります。

<?php 

    // This turns on error display without messing with php.ini
    // Delete the following two lines in production

    ini_set('display_errors',1); 
    error_reporting(E_ALL);

    // We avoid using $_SERVER['REMOTE_ADDR'] directly with a custom variable
    $ip = $_SERVER['REMOTE_ADDR'];

    // Otherwise, using the $_SERVER['REMOTE_ADDR'] directly
    //$city = file_get_contents('http://ipinfo.io/'. $_SERVER['REMOTE_ADDR']. '/city');
 
    // Using our custom $ip variable

    $city = file_get_contents('http://ipinfo.io/'. $ip. '/city');
    $country = file_get_contents('http://ipinfo.io/'. $ip. '/country');

    //$region = file_get_contents('http://ipinfo.io/'. $ip. '/region');
    

    // An alternate formatting of City State, Country
    //echo $city.' '.$region.', ' .$country;

    // Print our variables. <br> is a standard HTML line break.
    echo 'Your IP address is:   '.$ip;
    echo '<br>';
    echo 'Your Country name is: '.$country;
    echo '<br>'; 
    echo 'Your City name is:    '.$city;
?>

// で囲まれた行は、単なるコメント/例なので省略できます。同様に、エラー表示行 (ini_set/error_reporting) はデバッグ専用です。変数の前後のピリオドは連結に必要です。URL は次のように連結されます。

 Ex. http://ipinfo.io/123.123.123.123/city

このフォームでプレーンテキストを返します。ipinfo.io 開発者ページ返される内容について、さらにいくつかのアイデアがあります。上記のコードは次のものを返します。

Ex.
    Your IP address is :  xxx.xxx.xxx.xxx
    Your Country name is: US
    Your City name is:  Las Vegas

あるいは、「米国」ではなく「US」を希望する場合は、次のような方法も試すことができます。ジオバイト都市詳細レガシー API。「米国」を返すには:

<?php

    function getIP() {
      foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
         if (array_key_exists($key, $_SERVER) === true) {
           foreach (explode(',', $_SERVER[$key]) as $ip) {
             if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
                   return $ip;
                }
             } 
          }
       }
    }

    $tags=json_decode(file_get_contents('http://getcitydetails.geobytes.com/GetCityDetails?fqcn='. getIP()), true);

    // Prints all available members of the $tags array, in case we forget our options
    //print_r($tags);

    // $tags[geobytesipaddress]) creates a non-fatal error so we use '' quotes around the array elements.
    print_r('Your IP address is: ' .$tags['geobytesipaddress']);
    echo '<br>';
    print_r('Your Country name is: ' .$tags['geobytescountry']);
    echo '<br>'; 
    print_r('Your City name is:    ' .$tags['geobytescity']);

?>

これは、Geobytes ページから少し変更したサンプル コードです。記述されているとおり、最初のコード例の出力を複製しますが、国名は完全に表示されます。

Ex.
    Your IP address is :  xxx.xxx.xxx.xxx
    Your Country name is: United States
    Your City name is:  Las Vegas

ちなみに、Geobytes APIはipinfo.ioよりもいくつかのオプションをサポートしているようで、多くの未払いリクエスト率の上昇(それが重要であれば)。

関連情報