Solved

Using JSON url as WordPress Data

Posted on 2015-01-03
3
127 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 108

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 108

Expert Comment

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

747 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

13 Experts available now in Live!

Get 1:1 Help Now