Solved

Using JSON url as WordPress Data

Posted on 2015-01-03
3
134 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 110

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 110

Expert Comment

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Table style position to the left 8 24
restriction of entering a a page 5 31
Add social media icons to blog post 4 15
PHP Curl Problem 10 33
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
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…

735 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