[wp-hackers] using transient api with http api

Otto otto at ottodestruct.com
Tue Aug 2 00:19:55 UTC 2011


On Mon, Aug 1, 2011 at 7:13 PM, fris <fris at fris.net> wrote:
> Would the json decode be in the if statement? Just a line before set
> transient, or do you mean before the if?
>
> Cause the response is coming from the http api in the if statement.

You'd do the json_decode in the if statement and then store the
decoded response. Like so:

if ( false === ( $parsedResponse = get_transient( $cache_key ) ) ) {
       $response = wp_remote_retrieve_body(wp_remote_get($requestUrl));
       $parsedResponse = json_decode($response, true);
       set_transient($cache_key,$parsedResponse,60*60*12);
}

This way, your parsed response, which is all you actually care about,
is what you're storing in the transient in the first place. No need to
store the raw data then have to decode it on every page load.

-Otto


More information about the wp-hackers mailing list