How to import external JSON and display in php

10,806

You can follow it:

<?php
    $url = "http://api.lillycoi.com/v1/trials/search.json?query=cond:%22Multiple+Sclerosis%2C+Relapsing-Remitting%22&fields=id_info.nct_id,condition_browse,sponsors,intervention_browse,overall_status&limit=1000";

    $json = file_get_contents($url);
    $data = json_decode($json, TRUE);
?>

You can use var_dump or print_r to know what's in your data :

<?php var_dump($data); ?>

or

  <?php print_r($data); ?>

The TRUE returns an array instead of an object.

Share:
10,806
Jeri Burtchell
Author by

Jeri Burtchell

Updated on June 04, 2022

Comments

  • Jeri Burtchell
    Jeri Burtchell almost 2 years

    I have a WordPress site for people with Multiple Sclerosis and to assist them in finding current clinical trials that are recruiting participants I have an external JSON file of search results I want to display on my site.

    Here is a sample output from the JSON:

    {"intervention_browse":{"mesh_term":["Copolymer 1"]},"id_info":{"nct_id":"NCT00004814"},"sponsors":{"collaborator":[{"agency":"University of Maryland","agency_class":"Other"}],"lead_sponsor":{"agency":"National Center for Research Resources (NCRR)","agency_class":"NIH"}},"overall_status":"Completed","condition_browse":{"mesh_term":["Multiple Sclerosis","Sclerosis","Multiple Sclerosis, Relapsing-Remitting"]}}
    

    What would be the best way to do this? An example using cURL or JSON_decode would be nice. (I only know enough to get myself into trouble, so assume I'm in kindergarten please. :) )

    Since it's going into a WordPress site, should I use PHP or javascript or a combination of both for displaying it?

    Once I get the information to display on my site I have no problem styling it with css. Getting it on the page is my main issue.

    I have installed a plugin that allows me to add php directly into any page or post using shortcodes, but what code to put there is what's holding me up.

    Here is the JSON file:http://api.lillycoi.com/v1/trials/search.json?query=cond:%22Multiple+Sclerosis%2C+Relapsing-Remitting%22&fields=id_info.nct_id,condition_browse,sponsors,intervention_browse,overall_status&limit=1000