DEV Community

Cover image for How to implement Countries List API in your project
Karim Abdallah
Karim Abdallah

Posted on

How to implement Countries List API in your project

Hello, in this article we will learn how to implement Countries List inside your project (website, android app, etc).



here's an example of android app:

android app

first, we will use CountriesList library from Github:
github.com/kimoandroid/CountriesList



Why this library

  • Easy to use anywhere.
  • You can add unlimited translation for the same file without editing the main function or use Hardcoded.
  • You can use country_code key to use it in your sytem for calling codes.
  • you can display country name with it's flag easily.



You can easily use the ready api directly by using this link: https://api.encept.co/countries/index.php?lang=en

  • api parameters: lang=ar OR lang=en
  • api response: JSON Encode.
  • JSON response example:
[{
"code":"US",
"calling_code":"+1",
"flag":"πŸ‡ΊπŸ‡Έ",
"name":"United States"
}]
Enter fullscreen mode Exit fullscreen mode



You can use this api directly and call this link from your project and get the JSON response.

Or you can create your api from zero at your own server & use your own link.

First, Download The Library Files That I Mentioned Before From Github: github.com/kimoandroid/CountriesList

This Library is very flexable and easy to use & edit.

Inside Localization folder you will find the translation of the countries name you can add new translation file by creating file called country_[lang symbol].php such as spanish, you will create a file named country_sp.php.

and after that copy all content that inside default_strings.php file 'this is the translation schema'

now if anyone called the api url and put this parameter: 'lang=sp' the api will return the countries list with spanish.



That's all, Follow For More :)

Top comments (8)

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ • Edited

Might want to make the response headers match the response type - and also make it valid JSON (what is that title tag doing there???)

Collapse
 
karim_abdallah profile image
Karim Abdallah

Can you explain more ?

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ • Edited

Three issues I found straight away:

  1. Your endpoint is returning mangled HTML, not JSON - it starts with JSON then has a weird <title> tag floating around at the end. This will result in the JSON failing to parse correctly.
  2. Many libraries etc. will expect a Content-Type header of application/json if the data being returned is JSON, and may reject the content depending upon how strict they are. You API is currently returning text/html as the content type.
  3. I had CORS errors when trying the endpoint from some apps/code - you might want to review the CORS settings.
Thread Thread
 
Sloan, the sloth mascot
Comment deleted
 
karim_abdallah profile image
Karim Abdallah

@jonrandy
*hello bro, i've fixed the library u can try it now thanks for your help *

Thread Thread
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

Unfortunately it is now broken in a different way. It returns a single JSON string, rather than an array of country objects.

Thread Thread
 
karim_abdallah profile image
Karim Abdallah

but it works for me in this app:

Coding Oasis App

Thread Thread
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

Then maybe there is an issue in that app too? The data is effectively double-encoded. I can see that from your PHP code. For some reason you decided to manually create the actual JSON string for the array of country data, then encode this again using json_encode - making the final result a string encoded in JSON (containing the JSON for the data array), rather than just the array of country data encoded as JSON.

I would suggest storing the countries data as an actual PHP array, then using json_encode to encode that. This would remove the potential of any mistakes possible by creating the countries JSON by hand - making the code far more readable and maintainable.