Trouble reading a JSON file in a PHP file.
-
Hi all,
I’m not quite understanding how to read a JSON file from a PHP file.Here’s the setup:
I am running an installation of WP on my localhost. This site will be moved via FTP to a hosting service in the near future (Ideally as soon as I figure this JSON thing out).I’ve created a plugin and have this file structure:
– plugins/
— dereks-extension-chord/
— dereks-extension-chord.php
— boss_pedals.jsonI simply want to store the JSON data from boss_pedals.json in a variable in the dereks-extension-chord.php
I found a very similar question here. which suggested using:
$json = file_get_contents('path-to-file.json')
.This seemed rather promising but did not work for me.
Another find, a little more comprehensive can be seen here. No luck with that either.
I began reading about the file system API, but I’m becoming convinced this isn’t what I’m looking for (I don’t need to switch users, for example).
My current code is mostly comments from things that have not worked:
function get_boss_pedals(){ // $path = $_SERVER['DOCUMENT_ROOT'] . '/SEO-Grams/wp-content/plugins/dereks-extension-chord/boss_pedals.json'; // $path = WP_PLUGIN_URL . 'dereks-extension-chord/boss_pedals.json'; // $path = plugin_dir_url( __FILE__ ) . 'boss_pedals.json'; // $dir = trailingslashit( plugin_dir_url( __FILE__ ) ); // $url = wp_register_script( 'boss_pedals', $dir . 'boss_pedals.json' ); // $req = wp_remote_get( $url ); // $body = wp_remote_retrieve_body( $req ); // $data = json_decode( $body ); // paragraph_wrapper returns '<p>' . $peram . '</p>' // return paragraph_wrapper(var_dump( $data )); } add_shortcode('test', 'get_boss_pedals');
Apologies if my eyes have grown weary and I’ve missed the obvious here, but when I run file_exists() on any of the above $path vars above, I get false.
Why?
Thanks in advance.
- The topic ‘Trouble reading a JSON file in a PHP file.’ is closed to new replies.