[WebDev] Using the API Feeds

Day 1,203, 15:07 Published in United Kingdom United Kingdom by Darkmantle

Welcome back!

Today, I will be giving a basic example of how to connect to the eRepublik API to display information. In the examples I will be using the Citizen feeds but as long as you change the feed URL and the variables shown, it should work for any feed!

You will need to right-click the images and "Open in new window" to view them full size and read the code!




Resources Needed

To do this, you are going to need some basic resources. The first 2 are your web server either an online host, or a local one such as Xampp (See last article), and web editing software (Notepad, Notepad++, Dreamweaver etc).

You will need to ensure that your server has both PHP and cURL enabled. These are both needed for the script, and without them it won't work efficiently enough. You are also going to need the URL of the feed you want to use, which can be found here: http://api.erepublik.com/eapi-docs.pdf


Connecting to Feed

All PHP scripts need to have the same basic structure. They must start with or . This is the “start tag” and “end tag” for PHP, and is what your server will use to identify the code as PHP. So, let's start making the script. First, remember that any lines beginning with // or any lines between /* and */ are comments, and don't affect the actual code.



Storing the data

Next, you need to store the data in an array, which lets you store numerous string in one variable. This means that as you code down in a page you don't have to have lots of variables and can use them for other things, especially common ones like ID, NAME or LEVEL.




Displaying the data

The next logical step would be displaying the data. I am going to show you a few quick and simple ways, because echoing data in PHP is one of the first things you should learn by going to any of the websites shown in the previous article.




The whole code

This is a simple script once you know how, but it is one that can be changed and used for various projects. That code is used many times within nearly every script developed by eRepublik Tools Developers, including the eUK Military Hub and Tools website.

You can download the full script here:

Displaying API Data


Helpful Websites


PHP Manual SimpleXML Help
w3Schools - SimpleXML
w3Schools - Arrays


I hope that once again, my article can be of help to any aspiring coders out there. Simple scripts like this is what PHP is about, you can change and recycle them to suit nearly all of your needs with minimal ease once you learn the different functions needed.

Thank you,



eUK Coder