[WebDev] Using the API Feeds
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
Comments
Thanks ...
This hurts my skull....
sound great, I could play a bit with this, but why you refer specifically to WebDev in topic?
Because first of all, the articles I am making are going to be Web Development ones. I might do others in the future, such as design, or programming.
Oh, that is your point, my first association for term "WebDev" is... WebDev
http://www.windev.com/webdev/index.html
You should be explaining a bit about PHP/general coding formalities such as logic statements, pseudocode, and what the functions specific to each language mean.
Tomazim, the point of the articles is to help people in their journey for self-learning. My first article contains links to tutorials that explains it all. The next few articles will be about generic scripts helpful for coding with the eAPI.
very late reply, and probably useless now,
but use file_get_contents() to get the data ( see http://php.net/manual/en/function.file-get-contents.php ) Don't see any advantages in using curl, just complicates things.
also, host your code on pastebin... (a)