Cloudron makes it easy to run web apps like WordPress, Nextcloud, GitLab on your server. Find out more or install now.


    Cloudron Forum

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular

    Solved Code snippet ?

    GeoIP Service
    2
    3
    99
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • timconsidine
      timconsidine last edited by

      GeoIP is very interesting to run some checks on visitor location (to deliver customised content).

      Just wondering if there is a code snippet to extract the country from the results returned.

      murgero 1 Reply Last reply Reply Quote 0
      • murgero
        murgero App Dev @timconsidine last edited by

        @timconsidine Since the output from the api is just json encoded you can take the output in a variable and parse it:

        this example is PHP of course:

        <?php
        
        //google.com IP for mid-west US:
        $ip = "142.251.32.14";
        
        //Get string from API URL then convert the json output to PHP obj
        $geoip = file_get_contents("https://geoip.example.com/json?ip=$ip");
        $geoip = json_decode($geoip, true);
        
        //Output Country in string:
        echo "The registered country of the IP $ip is ".$geoip['registered_country']['names']['en']."\r\n";
        echo "Also, you can grab the country name via ['country']['names']['en']: ".$geoip['country']['names']['en']."\r\n";
        ?>
        

        --
        https://urgero.org
        ~ Professional Nerd. Freelance Programmer. ~

        timconsidine 1 Reply Last reply Reply Quote 3
        • timconsidine
          timconsidine @murgero last edited by

          @murgero wonderful, thank you !
          PHP is great !

          1 Reply Last reply Reply Quote 1
          • First post
            Last post
          Powered by NodeBB