Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using JSON url as WordPress Data

Posted on 2015-01-03
3
Medium Priority
?
160 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

Expert Comment

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to implement server side field validation and display customized error messages to the client.
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 viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

916 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