Trying to display external data to local wordpress site [WPGetAPI / Snippets]

[ad_1]

Hi so I’m in a bit of a pickle. I have no understanding of coding so go easy on me. I want to display this raw data [https://services.swpc.noaa.gov/json/solar\_regions.json](https://services.swpc.noaa.gov/json/solar_regions.json) on my website. (Not raw of course)

I created a php file (fetch\_data.php) with this code:

<?php
// Fetch JSON data from the external website
$json_data = file_get_contents(“https://services.swpc.noaa.gov/json/solar_regions.json”);
// Save the data to a local file
file_put_contents(“local_data.json”, $json_data);
echo “Data fetched and saved successfully!!”;
?>

and it works, I can see the raw data in the local\_data.json file locally

Now I want to be able to display this nicely on a page so I installed the plugin WPGetAPI and configured the url and everything and it works ok.

Then I installed Snippets (since I can’t run php from a page) and inserted this code to check if I can still read the raw data, and it works. RAW data is displayed on the screen.

<?php
$data = wpgetapi_endpoint( ‘kpi’, ‘kpi’, array(‘debug’ => false) );
var_dump($data);
?>

The issue I’m having is when I’m trying to display the entries in a nice way (not RAW data)

When I amend the above code to the below, shouldn’t it display the observed\_date and region as a starter or I’m missing something here (definitely am)? Nothing shows on the page.

<?php
$data = wpgetapi_endpoint( ‘kpi’, ‘kpi’, array(‘debug’ => false) );
echo $data[‘observed_date’];
echo ‘<br>’;
echo $data[‘ region ‘];
?>

All of this code etc etc was thanks to ChatGPT 🙂

I am basically following the example from youtube video: ‘Connect to API’s using WordPress, without writing code!’ by WPGetAPI channel.

Thanks for your time, appreciated!

[ad_2]
1 Comment
  1. You need to loop over the json data to get access to each entry, since this is in an [array](https://www.w3schools.com/php/php_arrays.asp). That’s why your $data[‘observed_date’] didn’t return anything.

    You also need to set the results format for your WPGetAPI api to: ‘PHP array data’.

    Then, you could do something like this to spit the data out into a table (just used a few columns as an example, which you should be able to add to/extend as per your requirements): [https://pastebin.com/4wmtHZxZ](https://pastebin.com/4wmtHZxZ)

    The above code, should give you something that looks like this: [https://ibb.co/MgxHhP6](https://ibb.co/MgxHhP6)

    You could then just use some custom CSS to make the table look nice.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer