Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using JSON url as WordPress Data

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

730 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