Solved

World Bank Data API: Converting links to data?

Posted on 2015-01-01
4
136 Views
Last Modified: 2015-01-19
Hello,

The World Bank website currently allows anyone to use it's worldwide data

In the above page, the World bank provides a basic call structure format. Following this protocol, I have generated links such as:
Surface area (sq. km) of Ethiopia: http://api.worldbank.org/countries/ETH/indicators/AG.SRF.TOTL.K2?MRV=1&Gapfill=Y

Entering the above link into a url, returns the following data:

<wb:data xmlns:wb="http://www.worldbank.org" page="1" pages="1" per_page="50" total="1">
<wb:data>
<wb:indicator id="AG.SRF.TOTL.K2">Surface area (sq. km)</wb:indicator>
<wb:country id="ET">Ethiopia</wb:country>
<wb:date>2014</wb:date>
<wb:value>1104300</wb:value>
<wb:decimal>0</wb:decimal>
</wb:data>
</wb:data>

Open in new window


I'm not a programmer, but all I want to be able to do is grab the <wb:value> of 1104300 above and add it to a webpage, for multiple url links.

E.g my website would have 10 separate pages (one for each country) and on each of these pages would be the label: Surface area (sq. km): alongside the pulled World Bank data from the above url.

How would I approach this?
0
Comment
Question by:dabug80
  • 3
4 Comments
 
LVL 33

Expert Comment

by:shalomc
ID: 40529297
I didn't know that about the world bank :)

You need some code and programming to extract the value from the XML. Which language do your programmers use, or which language is used on you web site?

There are 2 general approaches:
* Do it on the server. If your web site uses ruby code, then the logic of retrieving the links, parsing the XML and displaying it in context, will be written in ruby.

* Do it on the client. You will write Javascript code that runs in the browser to retrieve the links, parse the XML and display it in context.
0
 
LVL 33

Expert Comment

by:shalomc
ID: 40529356
Just checked - you can't do it client-side in Javascript, because the CORS headers are missing on the World Bank API :(
0
 
LVL 33

Accepted Solution

by:
shalomc earned 500 total points
ID: 40529385
Here is a php snippet that does the job

 
<?php 
function httpGet($url)
{
    $ch = curl_init();  
 
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
//  curl_setopt($ch,CURLOPT_HEADER, false); 
    curl_setopt($ch,CURLOPT_ENCODING , "gzip");

    $output=curl_exec($ch);
 
    curl_close($ch);
    return $output;
}
 
 

$xmlString=httpGet("http://api.worldbank.org/countries/ETH/indicators/AG.SRF.TOTL.K2?MRV=1&Gapfill=Y"); 

$xml=simplexml_load_string( $xmlString ) or die("Error: Cannot create object");
// This gets all nodes in namespace wb that is used inside the XML file
$wb = $xml->children("wb",true);
// print_r($wb); 

echo "<p>"; 
echo "country: " . $wb->data[0]->country . "<br>";
echo "Area: " . $wb->data[0]->value; 


?>

Open in new window

0
 
LVL 1

Author Closing Comment

by:dabug80
ID: 40557419
Excellent. Thanks for taking the time to code and explain. Really appreciate it.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question