Using APIs to Track the World Cup in Real-time

The heat is on!

In Brazil, the Football World Cup 2014 started on June 12 with the opening match between Brazil and Croatia. The world’s media spins all kinds of news around the tournament, the games, the players, the spectators and fans. The world’s sports fans enjoy a rush of entertainment and excitement, happiness and disappointments, during a few busy mid-summer weeks.

Keep your own score

How about if you could keep your own, dynamic scoreboard, for you and your friends to enjoy? When you have put some effort to design a cool looking chart, you need dynamic data to fill it with. No problem, there are several useful APIs for feeding your scoreboard. A few examples:

Not a programmer?

No worries, you can explore plenty of interesting stuff without programming a single line of code. Take a look at Kimonolabs’ World Cup Explorer, where you can play around with settings directly on their web site. For Everysport.com, there is a convenient tool, the API Console, which lets you explore the Everysport API response, even without your own API key.

And now, a little bit of coding

Let’s have a deeper look at the Everysport.com API for feeding your scoreboard, and see how we can use use it with a working example. The Everysport.com API gives you updated standings in real time (more or less). To access the API independently of their console, you need an API key; you can request one as described in their API documentation. Their handling is quick, and they reply promptly. In the documentation, we also see the terms of use, which basically asks us to publish a credit note and to link back to Everysport.com from our service.

The API uses unique numbers to identify each sport, league, event, team etc. For our little test, we will use the endpoint “leagues” and fetch the “events” object by using a GET request from a PHP script. The response is in JSON format. Since Everysport.com is in Swedish, the national team names in the response will be in Swedish.

Of course, we are interested in the football World Cup league, or “fotbolls-VM” in Swedish. But to fetch events, we need to specify the exact group or play-off stage of the World Cup. We will use “Group A” as an example, and its ID is “64579.” To look up other IDs, just navigate Everysport.com to the correct page, and then the ID will be shown in the address field as the last digits of the URL.

To fetch information and current standings of all games in Group A, we use this URL string:

https://api.everysport.com/v1/leagues/64579/events?apikey=YOUR_API_KEY_HERE

The string is part of a PHP script which is called from a bit of JavaScript code in our HTML file. We parse the JSON response, and then we are able to read out every detail we want to publish on the web site. For each “event”, or game, we can see the date and time, and the ID and name of the home team and visiting team, and more. Example:

…..”startDate”:”2014-06-12T22:00+0200″,”round”:1,”status”:”FINISHED”,”homeTeam”:{“id”:28251,”name”:”Brasilien”,…..

Steal with pride!

For your convenience, a very simple, working example is available on Github. It contains both a PHP script and an HTML file.

The output looks something like the image below. As you can see, it is far from the nicely formatted scoreboard you want. But the rest is up to you. Go ahead and use the code as inspiration and develop your own dynamic World Cup scoreboard.

Scorecard image credit: happyholidays2014