Make WordPress Core

Opened 12 years ago

Closed 11 years ago

Last modified 11 years ago

#22531 closed enhancement (wontfix)

integrate fetch_json($url)

Reported by: alexvorn2's profile 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)

#1 follow-up: @nacin
12 years ago

Feeds need to be parsed. JSON doesn't. Here's your function: json_decode( file_get_contents( $url ) );

#2 @ocean90
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 @alexvorn2
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 @nacin
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 @alexvorn2
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 @tw2113
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.

#7 @tw2113
11 years ago

  • Resolution set to wontfix
  • Status changed from reopened to closed

#8 @markoheijnen
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 @nacin
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 @rmccue
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 @johnbillion
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 @rmccue
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 @c3mdigital
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.

#14 @SergeyBiryukov
11 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.