Solved

Using JSON url as WordPress Data

Posted on 2015-01-03
3
133 Views
Last Modified: 2015-01-19
Hi,

The below url links to JSON data.
http://api.worldbank.org/countries/CYP/indicators/AG.SRF.TOTL.K2?MRV=1&Gapfill=Y&format=json

I would like to import this into a WordPress site. What's the best way of doing this?

Thanks.
0
Comment
Question by:dabug80
  • 2
3 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40529023
Since this is posted in the JavaScript, WordPress and PHP Zones, I don't really know how you want to consume the data.  But I can show you the PHP part.  Please see http://iconoun.com/demo/temp_dabug80.php

The data set from the World Bank consists of an array with two elements.  The first element is a simple object; the second element is an array of one element with objects nested inside it.  Typically you would use iterators like foreach() to access these elements and "sub-elements" but you can also access them directly with PHP array and object notation.

Here is the script:
<?php // demo/temp_dabug80.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28590031.html

// THE REMOTE RESOURCE
$url = 'http://api.worldbank.org/countries/CYP/indicators/AG.SRF.TOTL.K2?MRV=1&Gapfill=Y&format=json';

// ACQUIRE THE JSON DATA FROM THE RESOURCE
$jso = file_get_contents($url);

// DECODE THE INFORMATION AND SHOW THE DATA STRUCTURE
$dat = json_decode($jso);
echo '<pre>';
var_dump($dat);

// GET THE PAGE NUMBER FROM THE FIRST ELEMENT
$sub = $dat[0];
echo PHP_EOL . 'page: ' . $sub->page;

// GET COUNTRY FROM THE SECOND ELEMENT
$sub = $dat[1][0];
echo PHP_EOL . 'country: ' . $sub->country->value;

Open in new window


Here is the output:
array(2) {
  [0]=>
  object(stdClass)#1 (4) {
    ["page"]=>
    int(1)
    ["pages"]=>
    int(1)
    ["per_page"]=>
    string(2) "50"
    ["total"]=>
    int(1)
  }
  [1]=>
  array(1) {
    [0]=>
    object(stdClass)#2 (5) {
      ["indicator"]=>
      object(stdClass)#3 (2) {
        ["id"]=>
        string(14) "AG.SRF.TOTL.K2"
        ["value"]=>
        string(21) "Surface area (sq. km)"
      }
      ["country"]=>
      object(stdClass)#4 (2) {
        ["id"]=>
        string(2) "CY"
        ["value"]=>
        string(6) "Cyprus"
      }
      ["value"]=>
      string(4) "9250"
      ["decimal"]=>
      string(1) "0"
      ["date"]=>
      string(4) "2014"
    }
  }
}

page: 1
country: Cyprus

Open in new window

0
 
LVL 1

Author Closing Comment

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

Expert Comment

by:Ray Paseur
ID: 40557452
Thanks for the points and best of luck with your project, ~Ray
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
This article discusses four methods for overlaying images in a container on a web page
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 set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

815 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now