WordPress cross-origin request JSON and saved locally

Source: Internet
Author: User
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_uploadThis 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_uploadThis 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/uploadsDirectory. 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_contentsGet 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_optionsTable. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.