Country Webservice

Country Webservice

Credit: This is a modification of the Bermi Labs IP to country web service. Its purposes are to link IPs with ISO 3166-3 country codes, and to link 2 and 3 character country codes with country names.

Explanation

Like all web services, the input is a URL and the output is a string. In the example

http://www.lakeinfoworks.com/webservices/country/ip/abbr3/1044964400
http://www.lakeinfoworks.comThe server
webservicesThe web service's location on the server.
countryThe name of the PHP program that fetches the data.
ipDescribes the input, an IP address
abbr3Describes the output, a 3 character abbreviation.
1044964400The IP address

Put into your browser and you will get “FIN” echoed. Additional URI elements that may be used for either input or output are

  • abbr2
  • abbr3
  • name

There is one more feature that may be of some use. It is the input “all”. In the example

http://www.lakeinfoworks.com/webservices/country/all

The returned value is all of the countries. The countries are delimited by “\n” characters. Each country is a “|” delimited string consisting of the country's 3 character code, its 2 character code and its name.

Demonstrations

Within an Akelos program, you may code:

The webservice at Lakeinfoworks should not be used in production sites because your application will break whenever this site goes down or the program fails due to continuing development. Following are instructions for installing this webservice on your server.

Installation

  1. Create the “public_html/webservices/country” and “hidden/webservices/country” directories. “public_html” is the Document Root. “hidden” lies outside the Document Root, where a hacker with a browser can't get at it.
  2. Download and unpack country.zip into the “public_html/webservices” directory.
  3. Move the country.php, and .htaccess files to the “public_html/webservices/country” directory.
  4. Move the config.php file to the “hidden/webservices/country” directory.
  5. Create a database and permissions so that one set can update the database, and the other set can just do SELECTS.
    • On some servers, a database may have just one set of permissions available. In this case, it's more important to remove the database modifying programs after the tables are populated, as noted below.
  6. Modify config.php with your DB credentials: host, user, password and database name. The development database credentials are for updating; the production database credentials are for reading data from the database. If your server allows only one set of permissions, make both the same.
  7. Modify create_db.php, update.php and country.php so that the include_once for config.php points to the correct location.
  8. Create database tables by executing create_db.php.
  9. Populate the database tables by executing update.php.
    • This program uses the database provided by ip-to-country.webhosting.info. You may wish to download a fresh copy of it for your use. Bermi's program did this automatically, but my remote server wouldn't allow this feature, so I took it out. The ip-to-country.csv file has the country names as all upper case. This program makes them mixed case, but with a couple of problems. One country's name and 3 character code are missing. The PHP function ucwords didn't catch some data, such as after a ”.” and after a ”(”. These problems have been repaired in countries.sql. After running update.php, you may want to update your table with this file.
  10. After checking your tables, you may wish to remove the database modifying programs create_db.php and update.php. This will insure that a hacker can't execute them.

Your web service is ready for use.

Notes:

  • Remember to cache your visitors results so you don't hit this service twice for the same IP.
  • After you load country.zip into public_html/webservices and unzip it, you may open index.html with your browser to see these instructions and to make the links useful.
  • The web service will be changed in the near future to create a replacement for country_select.
 
webservice-country.txt · Last modified: 2008/03/24 15:23 by 83.245.212.191