$body = get_transient( 'my_api_response_value' );
if ( false === $body )
{
$url="https://api.myapi.com/chart?ticker=" . $ticker . '';
$response = wp_remote_get($url);
if (200 !== wp_remote_retrieve_response_code($response)) {
return;
}
$body = wp_remote_retrieve_body($response);
set_transient( 'my_api_response_value', $body, 5*MINUTE_IN_SECONDS );
}
$formatted_json = json_decode($body, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
We submit a request at the beginning because the transient doesn’t yet exist, and then we save the $body as a transitory value.
More info about the transient go to this Link : https://developer.wordpress.org/reference/functions/set_transient/
Related Posts:
- How do I fetch feed info from cache instead of directly from feed?
- Feed cache fallback – A feed could not be found
- custom php file inside my custom folder of plugins directory is not caching
- Generate aggregated feed from member blogs
- Best way to show Dynamic Content on a Cached WordPress Site?
- Is define(‘WP_CACHE’, true) needed for object caching?
- Configuring WordPress for Amazon CloudFront Caching?
- What exactly is an advanced object cache?
- WordPress Browser Cache with W3-Total-Cache doesn’t show new posts [closed]
- Nginx + WordPress + HHVM: Why isn’t Batcache working? Would Varnish help even more?
- wp_cache_set() or wp_cache_add()
- How to use cache with simplepie
- Fragment caching increasing database queries
- Which WordPress pages/URLs should never be cached?
- How to archive a wordpress site (make it read only)
- Stale cache handling with a persistent object cache
- Varnish + W3 Total Page Cache [closed]
- Are there any server requirements for WordPress transients to work properly?
- W3 Total Cache doesn’t detect memcached [closed]
- Settings to get the most out of W3 Total Cache
- can’t see live version of style.css – is it web server caching?
- Want to enforce the visitor to load the new version of your stylesheets and scripts [closed]
- Speed up WordPress
- fetch_feed: retrieve entries in the appearing order, not chronologically
- wp-super-cache exclude file from caching
- Should the page cache be refreshed often? [closed]
- Plugin a specific cache functionality?
- Possible to configure nginx to ignore cache for logged in users in certain roles only?
- Check if a user is connected and get is ID without fully loading wordpress
- Should I enable FastCGI on WordPress?
- Will caching WordPress affect session variables, custom filed updates and displaying images from a non wordpress table?
- Automatically enabled caching in some hosting companies w/o visible plugin (Must use plugin)
- Register script version not showing
- Why does running get_the_excerpt() when generating JSON file take 28 seconds vs. 599 milliseconds without it?
- Is there a get_post(s) filter that can alter/replace the output completely?
- server load. $_SERVER[‘REMOTE_ADDR’] and $_SERVER[‘HTTP_HOST’] [closed]
- Optimizing Jetpack for WordPress [closed]
- Is it possible to save an entire piece of rendered HTML in a transient?
- Execute a piece of code also when the cached version of a page is served
- Can a WordPress blog be entirely hosted on a CDN if cached?
- How to set the cache for the built-in SimplePie feed parser?
- Transient feeds and caching error
- To have WordPress interact with memcached it must be installed as a PHP extension?
- Prevent installation of style.css cookies and file caching
- How can I revert to showing cached pages when no database connection is available? [closed]
- Where does WordPress default SimplePie save cache data?
- Headway body classes and W3TC page cache conflict [closed]
- Reasonable Size Limit to options entry
- Is the object cached?
- How can I cache the LearnDash quizzes on my server?
- WP_Object_Cache for heavy WP_Query
- Cache wp-json/posts without a plugin?
- How to store or cache custom shopping cart data for every user’s session
- How to decode wordpress transient cache stored in database?
- Hook an action when a transient is deleted ? Can’t get it to work!
- WordPress PHP 5.5 and OPcache
- Accelerate and clean up WordPress [closed]
- How to make page/post password protected so you must reenter everytime you visit the page?
- Fixed version number for cached stylesheets and javascript
- How effective are cacheing plugins for dynamic pages?
- Custom JSON feed rewrite
- How can I permanently cache or “archive” a WP blog without needing future maintenance
- Any idea why Google not caching my site? [closed]
- How to solve minify js and css first time load delay?
- Last-Modified header support doesnât speed up server processing – want more
- How to get Caching Plug-ins to work on localhost with HTTPS?
- WordPress website not showing changes without clearing cache
- Cloud flare, cache, minify question [closed]
- HIgh CPU usage – Will using cache plugins help
- Cache WordPress for offline use
- Jetpack stats and caching strategy [closed]
- issues with object cache (memcached as backend) and wpmu when flush_all is run
- How do I serve fully cached full HTML on cloudfront
- Get user name and caching system
- I’m receiving the following error using the below code: Uncaught Error: Cannot use object of type WP_REST_Response as array
- How to get the URL of a sitemap that contains a certain post?
- How to solve simple wp simple ajax chat caching problem
- dynamic page not displaying correctly when Varnish hosting ignores query string parameters
- How to not cache nonces with WP Rocket?
- What do I do if my WordPress site is suddenly sanitizing portions of URLs it shouldn’t?
- Clear cache when comment is added
- How to make login / logout links cache independent?
- Why WordPress cache is not available after reload page?
- New blog posts don’t update on page – How to force update of cookies more frequently?
- WordPress PHP error getting posts from another wordpress blog
- PHP Fatal Error in cache.php on line 123 $wp_object_cache is non-object
- No Query String vs Ignore Query String in WordPress
- w3 total cache uninstall remove cache
- Completely reload style.css
- Page wrongly displayed when cache plugin activated
- Serve content from centralized location
- Major cache but only on WordPress site
- User cannot authenticate after signup when object cache enabled
- W3 Total Cache Minification: way to revert back? [closed]
- After Migration, Warning: include_once() errors on all pages
- Server Variables not recognized in certain posts
- Editing wp-includes/feed.php
- Experiencing a weird caching issues without any caching plugins
- How I define max cache time to Google Fonts installed on my site
- Keeping Objects in Memory