Using Wordpress's JSON API with W3 Total Cache - json

Using the excellent Wordpress plugin, JSON API, I've created an application that retrieves fresh data by visiting a URL along the following lines, doing a Unix curl dump.
http://www.website.net/?json=get_recent_posts&post_type=tree&count=200
The problem is this: Because the site delivers cache versions using W3 Total Cache, the stuff that gets curled is inconsistent. Sometimes I get the JSON that I need. Other times I just get a cache showing me "website.net"'s front page, without the effects of the query string.
I'm hoping to do one of two things:
1) Find a way for my request to bypass w3 Total Cache. But how? Is there something I can put into the query string that will tell Wordpress to give me a fresh page read and not a cache?
If that can't work,
2) Get the plugin data through PHP rather than through the RESTful Web request. But I only know how to use JSON API for Web URL calls and not for using the JSON data internally.
Any help much appreciated. I also thought that maybe W3TC's function to cache query string results might work, but I can't figure it out.

If you are using page cache using disk enhanced in w3tc settings, the URL with query strings will not cached. I think this solves your problem.

Related

"Reverse" JSON Status API

I've been wondering how to fetch the PlayStation server status. They display it on this page:
https://status.playstation.com/en-us/
But PlayStation is known to use APIs instead of PHP database fetches. After looking around in the source code of the site, I found that they have a separate file called /data.json.
https://status.playstation.com/en-us/data.json
The content of this file is the same as the index file (for some reason). They use stuff like {{endDateTitle}} and {{message}}, but I can't find where it's defined, if it's pulled using a separate file or just pulled from a database using PHP.
How can I "reverse" this site and see if there's a API I can use to display the status on my site?
Maybe I did not get the question right, but it seems pretty straightforward.
If using firefox, open Developer tools, Network. Reload the page.
You can clearly see the requested URL
https://status.playstation.com/data/statuses/region/SCEA.json
It seems that an empty list as a status means "No problems" (since there are no problems I cannot verify this assumption. That's all
The parenthesis {{}} are used by various HTML templating languages, like angular, so you'd have to go through the js code to understand where they get updated.

Wordpress JSON API returns normal site page in html. How do I get it to give me JSON like it's supposed to

For example, entering http://mywordpresswebsite.example.com/?json=1 into the browser loads the main site html, the same as omitting the json querystring variable: http://mywordpresswebsite.example.com/
The JSON API is activated. I have tried reactivating and deactivating, checking .htaccess file settings, and deactivating all other plugins. None of those have made much difference so far.
TIA
I had the same problem with my localhost test page and was wondering, why my route worked last week and was not accessible this week.
Short explanation
After some tests and a lot of frustration, I was able to use the REST API Route again by following the wordpress documentation about routes-vs-endpoints with “Pretty Permalinks” and “Ugly” Permalinks
Longer explanation
I guess in my case it was based on the reinstall of my MySQL Database. By installing the new database, my previous setup has been reset to the wordpress standard installation with permalinks as "plain", which is an "ugly" permalink. That's the reason, why the answer of Mattygabe work for me after the reinstall of the database.
But with this solution, I had a problem with my filter value and therefore I found the solution with "pretty premalinks" and changed my permalinks to "Month and name", as shown in the picture. After this change, I could access my REST API via the desired route.
There could be also some difficulties with REST APIs related to the following examples:
using "wp" within the REST route
if you work on plugins, which should be shared, keep on mind that some plugins may restrict REST Access, e.g. iThemes Security
I'm likely doing it wrong, but when I form my requests for a Wordpress installation at http://www.example.com/ like this:
http://www.example.com/index.php?rest_route=/my/rest/route/here
I end up getting proper responses back.
I had a heck of a time figuring this out and ended up grokking a URL formatted like that in the HTML returned to me. I was expecting to make requests as http://www.example.com/wp_json/wp/v2/my/rest/route/here , but I only got HTML responses.
(FWIW, I am reposting this on all similar questions on the StackExchange network. Admins/mods - if this is against the rules or seen as rep spamming, feel free to take it down. Was hoping to help anyone else hitting the same issue I am, and to also learn what it is I've done wrong and why.)
Ok, so the new endpoint for Wordpress 4.7 is mywordpresswebsite.example.com/index.php/wp-json. It's part of Wordpress Core as of 4.7 and not a plugin anymore, there's nothing to be activated. Thank you, Mark Kaplun.
I also experienced this issue. I did install the WP API plugin and then realized I didn't need it so I deactivated it and deleted it. Afterwards I tried a GET request to https://example.com/wp-json/wp/v2/posts and received the HTML of my wordpress site.
To fix this I ended up deactivating all plugins and then I started receiving the JSON response from https://example.com/wp-json/wp/v2/posts so I stepped through each plugin reactivating and in the end all my plugins are active and the endpoint is responding with JSON.
I changed Permalinks (Settings => Permalinks)
I had an issue returning html page instead of JSON response on Wordpress 5.3 and I got resolved when I changed the Permalink as Post name from plain

How do I parse a json image returned from an API?

To make an example Wordpress plugin that shows an image, or at least stores it in the media library, I go out to the NASA Open API and fetch the Astronomy Picture of the Day. I have not yet tested that code, but assuming it works my problem is: then what? I guess I have to parse the return data and somehow get an image file I can upload to the library. I'm used to grabbing pre-known text fields such as data.customerid, etc. but never an image. The API site is not much help. I have gleaned from around here a few references to "base64" but I don't know what that means. I can't find a straightup example if what to do. Any thoughts?
you'll have to do steps along this lines, you are pretty much correct on that:
fetch response from API (example: https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY)
parse url:
use media_sideload_image function in WordPress to save image in media to be able to show it in your plugin or to your users..
I suggest you to start building your plugin from scratch and then when you get stuck post your code here and community can help you better..

Drupal 7 (VERY) Custom Preview

I have a drupal site that is being used strictly as a CMS that produces JSON feeds using services and services_views, which are consumed by a separate site. What I would like to do (and I have a working proof of concept of this) is allow for a "live preview" on the real site, by intercepting the node form preview / submit, encoding the node as JSON, and loading a special page on the live site that consumes that JSON and displays the page accordingly.
The problem with this JSONized node is, it's different from the JSON being produced by my view (using services_views). My end goal is to produce JSON that is identical for both previewed and non-previewed objects, without having to maintain separate output methods (I could easily hand-customize the json but then when my view for the public api changes I have to make the same changes to the preview json. Trying to avoid this).
I'm looking for feedback on this approach. Is what I'm attempting even possible? The ideas I've been able to come up with so far are:
being able to (conditionally) drive my view with data from a non-databse source
sneakily inserting data into the view object during one of the stages of execution? Kludgy but I'm not above that :)
saving a "clone" node (or revision?) of the node being previewed and let the view use that to display the preview JSON?
Maybe this is the wrong approach altogether and there's something better? (Trying to intercept and format the services output in my module... maybe avoid services_views altogether?)
If anyone can offer some advice, insight or opinions on how to best proceed here, I'd be really grateful.
in a custom module, you could set up a page that grabs the json output from the view page.
$JSON = file_get_contents($url);
that way the preview stays bound to the view, even if the view changes.
First I think it's not an easy task what you are trying to achieve. So before all, good luck.
I think you could intercept the node submission data, then create a node programatically, then render that node, and then export the rendered node to JSON. Inmediately after you get the JSON, delete this node, because the programmatically created node is only for preview.
This task could be more CPU demanding but think that previewing content exactly as the content will look is difficult.
Your rss feeds that your site reads could be filtered with some parameter to avoid programmatically created nodes (prewiew nodes), despite these nodes will be available for a very short time.

Line chart from external csv-file

I want to make a line chart that automatically updates with data from this page: http://www2.nve.no/h/hd/plotreal/Q/0027.00025.000/knekkpunkt.csv
The .csv is updated once per hour, and contains date and two values for amount of water flowing in a river. How can I set up a Highcharts, or similar, to get data from this file, and render to a graph?
I don't have server access to nve.no, where the data is stored.
Appreciate any ideas!
As a part of the ZingChart team, we've received several questions similar to this one. You're likely to initially run in to cross-domain issues when using a resource that is hosted on another domain, especially since you do not have any access to that server.
There are ways around this, however. One method involves using JSONP and YQL (Yahoo! Query Language). Using YQL, you can pull from the URL that you've provided and have the data returned as JSONP.
Here is a JSbin demo that shows this off: http://jsbin.com/hidel/1/edit?html,output
At the beginning I recommend you to visit the working with data article.