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


Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Bookmarks
  • Search
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

Cloudron Forum

Apps | Demo | Docs | Install
  1. Cloudron Forum
  2. GeoIP Service
  3. Code snippet ?

Code snippet ?

Scheduled Pinned Locked Moved Solved GeoIP Service
3 Posts 2 Posters 835 Views 2 Watching
  • 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.
  • timconsidineT Offline
    timconsidineT Offline
    timconsidine
    App Dev
    wrote on last edited by
    #1

    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.

    murgeroM 1 Reply Last reply
    0
    • timconsidineT timconsidine

      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.

      murgeroM Offline
      murgeroM Offline
      murgero
      App Dev
      wrote on last edited by
      #2

      @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. ~

      timconsidineT 1 Reply Last reply
      3
      • murgeroM murgero

        @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";
        ?>
        
        timconsidineT Offline
        timconsidineT Offline
        timconsidine
        App Dev
        wrote on last edited by
        #3

        @murgero wonderful, thank you !
        PHP is great !

        1 Reply Last reply
        1
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Bookmarks
        • Search