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:
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"
}]
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)
Might want to make the response headers match the response type - and also make it valid JSON (what is that
title
tag doing there???)Can you explain more ?
Three issues I found straight away:
<title>
tag floating around at the end. This will result in the JSON failing to parse correctly.Content-Type
header ofapplication/json
if the data being returned is JSON, and may reject the content depending upon how strict they are. You API is currently returningtext/html
as the content type.@jonrandy
*hello bro, i've fixed the library u can try it now thanks for your help *
Unfortunately it is now broken in a different way. It returns a single JSON string, rather than an array of country objects.
but it works for me in this app:
Coding Oasis App
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.