Frontend with simple JSON backend
In this tutorial, we will keep the backend really, really simple. In fact, our backend will simply convert the XML files to JSON. The frontend will do all the work, fetching the JSON files regularly via AJAX. The fetched sports data will be shown in a HTML page.
Verify that your server can download XML files
In your order confirmation email, there are 5 links. As described in How it works, you need to setup a regular automatic download of those 5 XML files to your own server.
In this tutorial, we will use wget to download the XML files. Wget is available on most systems and has decent performance. Let us start by downloading one of the files manually once, just to check that wget is installed and is able to download. Enter the command below (you must change "7384" to whatever it says in your order confirmation email) in a terminal (i.e. login to your server via SSH). Remember to run this command from your server since the XML file is restricted to the IP address you entered when you placed your order.
If you are unfamiliar with the command line, do not despair, it is not as complicated as it looks. The double ampersand (&&) separates commands. First "wget" is executed. If it does not fail, the "less" command is executed. In other words, first we download the Standing XML. If the file was successfully downloaded, we view it to make sure it contains XML.
Downloading does not work?
Your XML files should be available about 15 minutes after you bought them and the command above should download the Standing XML file to your home directory on the server. However, it is possible that you may not be able to download the XML files.
A possible reason is that you did not enter your server's IP address correctly when you placed your order. To verify that you entered the correct IP address, find the IP address of your server. When you have found your IP address, login and go to My Profile. Enter the IP address, and try downloading again.
The download may also fail if there is a problem with your server setup. If the wget command above does not work, it may be easier for you to write a custom download script in the language you are the most familar with. In PHP, you can use file_get_contents to download a file, for instance. Then you may be able to figure out what is wrong by looking at your servers error log.
Yet another reason could be that there are currently simply no matches being played in any of the leagues you bought. Check when your leagues start and end. If the leagues are not in season, you may want to test your script with our example XML data. Download the example files and upload them to your server. If you test your solution using our example XML data, it is important that you test your solution again later when there are live matches in case the Premier League example data has more coverage than the leagues you bought.
Converting the XML feed to JSON
Error handling has been omitted to keep the code simple.
Setting up an automatic update
Now we know how to download XML files and convert them to JSON. Let us automate this by setting up an update process that automatically downloads the Standing XML and converts it to JSON.
Note that you have to download the files into a directory where your web server can access them. This is also known as the web servers public directory. Below, we will asume that your website is stored in /var/www/sport.com/ , and that you will want to put the JSON files in the "tutorial/feed" subdirectory, meaning that the Standing file will be available as JSON via the URL /tutorial/feed/standing.json.
With Linux, you can setup a cronjob. If you are not familar with Cron, please refer to a Cron tutorial. Let us say you wish to update once a minute. Edit the crontab, as described in the Cron tutorial. Adding the following line will download the standing.xml file and run the xml_to_json.php script once a minute:
This should start updating the Standing XML. You can verify if it works by opening the JSON file directly in your browser (/tutorial/feed/standing.json). When you know there is a match, you can press F5 to verify that the file changes when goals are scored, etc.
But wait, how dare we call it livescore if it only updates once a minute? Surely that is not live. It turns out that Cron can normally only run a script once a minute, but fortunately that is easy to solve. Let us run our script every 15 seconds by calling our PHP script 4 times with a 15 second delay between each call.
Now that is more live. If you wanted it to update every 10 seconds, merely run it 6 times with a delay of 10 between each call.
By now, you may be wondering how often we update data. Only the Live XML file is updated live, the other XML files are updated every 15 minutes. This means that updating a league table every 15 seconds is overkill, but it does show how you can fetch the Live XML file very frequently.
The update frequency of the Live XML depends on the league. Big leagues such as the Premier League is covered by more than one updater so it is updated quickly with minimal delays. Depending on the league, you can typically expect delays of approximately 5-15 seconds and up to approximately 30 seconds. The delay depends on various things, but we are confident you will be satisfied with the update frequency of major leagues.
We do not offer a demo feed, but if you want to measure our performance, consider buying access for one month to one of the smaller leagues that still have live coverage and measure the delay from when you see it on TV to when it appears in your file (taking any delays from Cron etc into account). Minor leagues may have a longer delay, depending on things such as how many matches are played simultaneously throughout the world.
The frontend view
Inspecting sports data
Screenshot: Inspect element
Screenshot: Manchester United
Let us write a simple stylesheet, just to make our standing look a wee bit prettier. Of course, in your own app, maybe you would like your standing to be more fancy, maybe using responsive design, perhaps coloring odd and even rows differently, etc.
Time to see everything in action. The data shown is our example XML data, so you should see an old Premier League standing. On your own server, you should see a similar standing for one of the leagues you bought.
When you start using our data, you may notice that some leagues have more data than others. A league such as the Premier League has all data, but we do not currently provide a list of topscorers for the Azerbaijani Premier Division, for instance. To see exactly what we cover for your leagues, go to the XML Sports Data page. Move your mouse over the icon of a league of interest. A popup wil open, showing exactly what we cover.
Improving the backend
The demo works, but it is not as efficient as it could be. If you have many visitors, you can do better. For instance, the file standing.json contains various data that we fetch via HTTP but we do not actually show it. The standing.json file used in the demo also contains standing data for a spanish league, though we only show a standing for the Premier League. Clearly it is inefficient to load data that we are not making use of.
To improve performance, you should implement another backend. In this tutorial, we wrote an extremely simple backend that simply converts the XML files to JSON. There are various other ways to write a backend. Below we briefly look at two other options, though there are many other ways you could process and use our data.
MVC pattern (backend)
MVC pattern (frontend)