I am adding a feature to a WordPress topic: display the local PM2.5 index on the home page. This third-party service is used. the GET method can directly request JSON, but the number of requests is limited (for example, a maximum of five requests per hour), so I want to use PHP to request the json I want... I am adding a feature to a WordPress topic: display the local PM2.5 index on the home page. Used
This third-party service.
The GET method can directly request JSON,The number of requests is limited.(For example, a maximum of five times in an hour ),I want to use PHP to request the json I want and save it on the server.In this way, the user can directly request data from my server when accessing the page.
Now I have two ideas:
My idea at the beginning was to use PHP to request JSON and save the JSON file to the wp-content/uploads directory (similar to uploading files ).wp_handle_upload
This function (Link 1, Link 2, and some scattered materials), but I have never figured out how to use it. I hope some useful friends can give me some advice.
Later, I came up with another method: Put A JSON file in the topic directory in advance, and then use PHP to request third-party data and then re-write the JSON file. In this way, I want to avoid the "Upload" action and directly operate a JSON file.
Which of the above methods is more feasible? If feasible, what technologies are needed? My back-end knowledge is simple. I hope you will not be enlightened. Thank you!
Reply content:
I am adding a feature to a WordPress topic: display the local PM2.5 index on the home page. UsedThis third-party service.
The GET method can directly request JSON,The number of requests is limited.(For example, a maximum of five times in an hour ),I want to use PHP to request the json I want and save it on the server.In this way, the user can directly request data from my server when accessing the page.
Now I have two ideas:
My idea at the beginning was to use PHP to request JSON and save the JSON file to the wp-content/uploads directory (similar to uploading files ).wp_handle_upload
This function (Link 1, Link 2, and some scattered materials), but I have never figured out how to use it. I hope some useful friends can give me some advice.
Later, I came up with another method: Put A JSON file in the topic directory in advance, and then use PHP to request third-party data and then re-write the JSON file. In this way, I want to avoid the "Upload" action and directly operate a JSON file.
Which of the above methods is more feasible? If feasible, what technologies are needed? My back-end knowledge is simple. I hope you will not be enlightened. Thank you!
It's just a small feature. It's totally different from WordPress. Directly write a PHP file and place the captured JSON filewp-content/uploads
Directory. If the file exists, the file is directly read. If the file does not exist, the file is crawled. The file name can be in the format of "Time and place. Use the theme directlyfile_get_contents
Get it. Sample Code:
Using WordPress APIS is also a good choice. WordPress is also a framework that encapsulates many things.
If the WordPress plug-in (or theme) wants to store a small amount of data and does not need to use files, you can directly use the WordPress option system to throw datawp_options
Table. Like this:
The above code can only be implementedThe visitor visits the site for the first time and is not cached for various reasonsNo error.
You also need a mechanism to regularly refresh and forcibly update cache content.
You should check this.WordPress Cron-WordPress encapsulates an environment-independent scheduled task system, which can work either on a real cron platform or without a scheduled task program. It is suitable to use this method to regularly retrieve the latest data.