#22531 closed enhancement (wontfix)
integrate fetch_json($url)
Reported by: | alexvorn2 | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | General | Keywords: | |
Focuses: | Cc: |
Description
as we have fetch_feed() function, it will be great to have fetch_json() function too, to download a json file and decode into an array.
Change History (14)
#2
@
12 years ago
- Milestone Awaiting Review deleted
- Resolution set to wontfix
- Status changed from new to closed
- Type changed from defect (bug) to enhancement
There is no need of it.
#3
in reply to:
↑ 1
@
12 years ago
Replying to nacin:
Feeds need to be parsed. JSON doesn't. Here's your function:
json_decode( file_get_contents( $url ) );
It is not nice to have file_get_contents function in a theme
#4
@
12 years ago
You're right. This would be the proper way to do it in WordPress:
json_decode( wp_remote_retrieve_body( wp_remote_get( $url ) ) );
#5
@
11 years ago
- Resolution wontfix deleted
- Status changed from closed to reopened
I want to reopen this because I think It would be very helpful to have a fetch_json function with cache, so retrieving the URL data with json_decode( wp_remote_retrieve_body( wp_remote_get( $url ) ) );
every time, makes the page load slower then usual.
The cache should store the data for at least 12 hours as in the fetch_feed function.
#6
@
11 years ago
alexvorn2, we already have a setup ready for caching json data and external api content via the Transients API http://codex.wordpress.org/Transients_API Simple to use and saves temporarily to the options table based on a length of time you specify.
#8
@
11 years ago
- Milestone set to Awaiting Review
- Resolution wontfix deleted
- Status changed from closed to reopened
We do need something like this. JSON feeds are used a lot and it fits good inside core. It's not as simple as calling three functions since you do need to check if the output is valid from wp_remote_get. Like the body doesn't have to be a JSON string what will result in a notice.
#9
@
11 years ago
fetch_feed() specifically wraps RSS feeds. fetch_ json() is just ripe for abuse. If all it does is wrap the HTTP API, and then does json_decode(), that does not seem useful. Why add an extra layer to the API here? Especially given disparate needs of caching, API calls versus fetching data, etc.
#10
@
11 years ago
-1. A caching layer is almost always going to be application specific. It's trivial to write a function that does this in 5 minutes and has way more flexibility than a built-in function could.
#11
@
11 years ago
The primary function of fetch_rss()
is for displaying the content of feeds on your site (eg. in the RSS feed widget). Caching in this situation is a must. The same can't be said for JSON feeds.
#12
@
11 years ago
- Keywords close added
Parsing feeds is also a heck of a lot more work than parsing JSON (trust me on that one), and it's a much higher-level process. Recommending close.
#13
@
11 years ago
- Keywords close removed
- Resolution set to wontfix
- Status changed from reopened to closed
feeds and json data from an external API are entirely different. We have a fully functional HTTP API that allows developers to retrieve and store data any way they want. There is no need for this function.
Feeds need to be parsed. JSON doesn't. Here's your function:
json_decode( file_get_contents( $url ) );