1

I'm testing an API I build locally with a local install of wordpress.

My API path is something like: http://local.web.tt.com:8615/api/product

With my Node server running it displays this in the browser when you go to that link (mongodb collections object]:

[
    {
        "_id":"54bd5fbb646174009a450001",
        "productname":"Product 1",
        "overview":"Overview Title",
        "benefits":
            [
                "List item 1",
                "List item 2",
                "List item 3"
            ]
    }
]

My PHP wordpress shortcode plugin

add_shortcode('product', function($atts, $content) {
    $service_url = 'http://local.web.tt.com:8615/api/product';
    $data = json_decode($service_url, true);

    return $data;
});

Basically nothing shows up on the page when I add [product] in a blog post. The shortcode does work if I return just a simple string.

I also just tried this:

add_shortcode('product', function($data) {
    $service_url = 'http://local.web.tt.com:8615/api/product';
    $get = file_get_contents($service_url);
    $data = json_decode($get, true);
    return $data;
});

However that just spits out Array into the spot where the shortcode goes:

enter image description here

What I'm trying to do is capture the strings in each of those keys of the JSON object, then nicely display them with HTML & CSS. ie: The Benefits array items will show up as bullet points.

Basically something like this:

$content .='
    <h2>$data.overview</h2>
    <ul>
       <li>$data.benefits[0]
       <li>$data.benefits[1]'

return $content;

Any idea what I'm missing? Thanks in advance!

Leon Gaban
  • 36,509
  • 115
  • 332
  • 529
  • You are trying to get data via `curl` in this case u don't need it, just `json_decode()` will work fine for you. – Alex Jan 19 '15 at 17:16
  • That's what I tried first, but getting blank... and no errors :( ok will remove the crud part of the post since that isn't needed – Leon Gaban Jan 19 '15 at 17:21
  • Maybe you need to `return "product = $data";` , im not an wp expert but as i read about this : http://codex.wordpress.org/Function_Reference/add_shortcode – Alex Jan 19 '15 at 17:29
  • Hmm, tried that and also `return "product = {$data['name']}";` but the only thing that shows up in the post is the string `product =` – Leon Gaban Jan 19 '15 at 17:33
  • `print_r($data)` to see if you get any contents at all. – Alex Jan 19 '15 at 17:34
  • 1
    should be `$data['benefits'][0]` – Alex Jan 19 '15 at 19:58

1 Answers1

1
{
"_id": "84b7e4c7946174009a1f0000",
"name": "Name of product",
"overview": "Long blah blah blah blah here...",
"benefits": [
    "List item 1",
    "List item 2",
    "List item 3",
    "List item 4"
]
}

fix your json to have quotes for keys

 $service_url = 'http://local.web.tt.com:8615/api/product'; 
 $get = file_get_contents($service_url);
 $data = json_decode($get, true);
 return $data;

Also, why are you returning the array ?

Karan
  • 1,187
  • 2
  • 9
  • 16
  • Oh I'm getting `Array` back now! Well I want to display all that data nicely with CSS. The benefits array items are basically going to be bullet points. I changed the keys to have quotes too, but still just getting `Array` – Leon Gaban Jan 19 '15 at 19:40
  • Ooo I'm getting somewhere with this: http://stackoverflow.com/questions/16700960/how-to-use-curl-to-get-json-data-and-decode-the-data I can see the var_dump on the page now with the correct data! It's ugly, but just gotta format it now... – Leon Gaban Jan 19 '15 at 20:07
  • I got a bit further and made a new question here, do you think you'd have a moment to check it out? http://stackoverflow.com/questions/28033267/how-to-parse-out-variables-from-a-json-var-dump-in-a-wordpress-plugin – Leon Gaban Jan 19 '15 at 20:52