2

Frankly - this may be a duplicate of another question I have out there. Sorry if it is. I'm learning this site and PHP/Wordpress development. I have partnerships with local sites that use JSON and we're working together. They aren't Wordpress specialists though.

I want to pull what my friend calls a JSON object from this url:

http://aggrenda.com/mpellas/michael-pellas/events.json

I'm trying to create a shortcode that can parse the data into scheduled posts. I have specific date, address, description (and other) data.

I've been working with my PHP at phpfiddle.org and have been successful. I haven't been able to translate anything to Wordpress functionality though.

Here's one of my latest attempts:

add_shortcode('aggrenda', 'requestAggrendaEvents');
function requestAggrendaEvents() 
{
$json = file_get_contents("http://aggrenda.com/mpellas/michael-pellas/events.json");
$data = json_decode($json, true);
var_dump($data['events'][1]['title']);
}

The basic php works, but if I chnge file_get_contents with wp_remote_get I end up with nothing but a blank page...or a null return depending on little variations in my code. I put everything into functions.php.

I have anumber of other snippets where the PHP works but i can't translate it to WP functionality. I'm sure i'm mssing something small. Help?

4
  • Could you add an example with wp_remote_get() used? It would be easier to look/copy it than try to guess what goes wrong. :)
    – Rarst
    Commented Aug 24, 2015 at 19:37
  • This returns only "null" - add_shortcode('aggrenda', 'requestAggrendaEvents'); function requestAggrendaEvents() { $json = wp_remote_get("aggrenda.com/mpellas/michael-pellas/events.json"); $data = json_decode($json, true); var_dump($data['events'][1]['title']); }
    – mpellas
    Commented Aug 24, 2015 at 19:40
  • @mpellas, you missed http in above example, did you missed just over here or you tried with this invalid url only? Second thing, can you check if cURL is enabled on your server?
    – Domain
    Commented Aug 24, 2015 at 19:57
  • Thanks for the reply. That was an error in my copy and paste. I just tried the code and made sure I had the http in front of the URL and only got NULL back. I can check on if cURL is enabled on the server.
    – mpellas
    Commented Aug 24, 2015 at 20:11

1 Answer 1

0

As per documentation on wp_remote_get() it doesn't return you just the body of requested resource. Its return will be either the array of data or WP_Error object on failure.

The simplest snippet to get to the body would be:

$json = wp_remote_retrieve_body( wp_remote_get( $url ) );

PS it's kinda weird to be doing this in shortocde, are you sure it's most appropriate place for it? :)

4
  • Thanks for the answer. For my current skillset, I wasn't sure if add_shortcode or wp_insert_post would be more appropriate. I found wp_remote_retrieve_body today but haven't used it. Would you not use that in a shortcode?
    – mpellas
    Commented Aug 24, 2015 at 19:59
  • I think I will let you build it out some more rather than dump bunch of stuff on your head. :) Processing of stuff doesn't have to be in the post content / shortcode and probably shouldn't, but if that's fastest for you to make sense of it for starters then just do that.
    – Rarst
    Commented Aug 24, 2015 at 20:08
  • I appreciate that! I'm really trying to get through this with one project that I'm building for a specific purpose. I'm getting the hand of PHP...but it's driving me crazy that I can't translate it over to Wordpress. It'll come in time :)
    – mpellas
    Commented Aug 24, 2015 at 20:16
  • If i could throw this out there...This is my current attempt: add_shortcode('aggrenda', 'requestAggrendaEvents'); function requestAggrendaEvents() { $url = 'aggrenda.com/mpellas/michael-pellas/events.json'; $response = wp_remote_get( esc_url_raw( $url ) ); /* Will result in $api_response being an array of data, parsed from the JSON response of the API listed above */ $api_response = json_decode( wp_remote_retrieve_body( $response ), true ); }
    – mpellas
    Commented Aug 24, 2015 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.