WordPress 跨域请求 JSON 并保存在本地
我在给一个 WordPress 主题添加一个功能:在首页显示当地的 PM2.5 指数。使用了这个第三方服务.
GET 方法可以直接请求到 JSON ,但是 请求次数有限制 (比如一小时最多 5 次),所以我想用 PHP 请求到我要的 JSON,然后保存在服务器上,这样用户访问页面时,直接从我的服务器上请求数据即可。
现在我有这两种思路:
我一开始的思路是,用 PHP 请求到 JSON 后,把 JSON 文件保存到 wp-content/uploads 目录(想法类似于上传文件),找到了
wp_handle_upload
这个函数(和一些零散的资料Link 1, Link 2),但是一直没弄明白怎么用,希望有用过的朋友能指点一下。后来又想到一个办法,在主题目录里面预先放一个 JSON 文件,然后用 PHP 请求第三方的数据之后再复写这个 JSON 文件。我想这样就避开了 “ 上传 ” 这个动作,直接操作一个 JSON 文件即可。
请问上面那一种方式更可行?如果可行,具体需要用到哪些技术?我后端知识浅薄,希望大家不吝赐教,先多谢了!
回复内容:
我在给一个 WordPress 主题添加一个功能:在首页显示当地的 PM2.5 指数。使用了这个第三方服务.
GET 方法可以直接请求到 JSON ,但是 请求次数有限制 (比如一小时最多 5 次),所以我想用 PHP 请求到我要的 JSON,然后保存在服务器上,这样用户访问页面时,直接从我的服务器上请求数据即可。
现在我有这两种思路:
我一开始的思路是,用 PHP 请求到 JSON 后,把 JSON 文件保存到 wp-content/uploads 目录(想法类似于上传文件),找到了
wp_handle_upload
这个函数(和一些零散的资料Link 1, Link 2),但是一直没弄明白怎么用,希望有用过的朋友能指点一下。后来又想到一个办法,在主题目录里面预先放一个 JSON 文件,然后用 PHP 请求第三方的数据之后再复写这个 JSON 文件。我想这样就避开了 “ 上传 ” 这个动作,直接操作一个 JSON 文件即可。
请问上面那一种方式更可行?如果可行,具体需要用到哪些技术?我后端知识浅薄,希望大家不吝赐教,先多谢了!
就一个小功能而已,不和WordPress扯上关系也完全么问题呀。直接写一个PHP文件,将抓取过来的JSON文件放到wp-content/uploads
目录内,如果文件存在,则直接读取,不存在则进行抓取工作。文件的文件名可以采用“时间地点”的格式。至于主题里头直接就用file_get_contents
获取就OK了。给个示例代码:
<code><?php $name = "20140129Beijing"; $file = './wp-content/uploads/'.$name.'.json'; $api_url = ""; if(file_exist($file)) { echo file_get_contents($file); } else { $json = file_get_contents($api_url); file_put_contents($json, $file); echo $json; } ?> </code>
用用WordPress的API,也是个好选择。再不济WordPress也是个封装了很多东西的框架嘛。
WordPress插件(或主题)如果想存储少量数据,也不必用文件这么麻烦,大可直接借助WordPress的选项系统,把数据扔进wp_options
表中。就像这样:
<code><?php $pm25_mod_data = get_option('pm25_mod_data_cache'); if (!$pm25_mod_data) { $pm25_mod_data = file_get_contents($pm25_mod_apiurl); //这里也可以调用你做API请求的任何有效代码或函数调用 add_option('pm25_mod_data_cache', $pm25_mod_data); } echo $pm25_mod_data; //或任何其他的格式化输出 ?> </code>
以上这段代码只能实现访客首次访问,且由于种种原因没有缓存时不出错。
你还需要有一个定时刷新,强行更新缓存内容的机制。
这个你应该查查WordPress Cron - WordPress封装了一个环境无关的计划任务系统,既能在可以使用真实cron的平台工作,也可以在没有计划任务程序的情况下“变通的”工作。用这个来定时取最新数据很适合。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

A step-by-step guide to replacing a header image of WordPress: Log in to the WordPress dashboard and navigate to Appearance >Theme. Select the topic you want to edit and click Customize. Open the Theme Options panel and look for the Site Header or Header Image options. Click the Select Image button and upload a new head image. Crop the image and click Save and Crop. Click the Save and Publish button to update the changes.

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

You can view the WordPress front-end by logging into the dashboard and switching to the View Sites tab; automate the viewing process with a headless browser; installing the WordPress plugin to preview the front-end within the dashboard; viewing the front-end via a local URL (if WordPress is set locally).

Importing WordPress source code requires the following steps: Create a sub-theme for theme modification. Import the source code and overwrite the files in the sub-topic. Activate the sub-theme to make it effective. Test the changes to make sure everything works.

WordPress editing dates can be canceled in three ways: 1. Install the Enable Post Date Disable plug-in; 2. Add code in the functions.php file; 3. Manually edit the post_modified column in the wp_posts table.

The steps to create a custom header in WordPress are as follows: Edit the theme file "header.php". Add your website name and description. Create a navigation menu. Add a search bar. Save changes and view your custom header.
