Wordpress REST API - 写入JSON文件?

时间:2017-05-04 15:45:48

标签: php wordpress wordpress-rest-api

我一直在搞乱Wordpress REST API,并创建了我的自定义端点,并获得了我想要的确切数据。基本上我创建了一个端点来接收我的所有帖子/ pages / acf - 我不想在每个页面加载时调用API,我只想在预加载器中调用API一次。

然而,当我调用API时,所有逻辑运行,然后导致1到2秒的加载时间。是否有可能每当我在Wordpress上进行更新时,它都会调用我的端点,并在服务器上写一个JSON文件,所以data.json?这样,当我加载我的网站时,它可以调用该data.json,完全没有任何延迟。

我不确定这是否可行但是想在这里问一下。

4 个答案:

答案 0 :(得分:3)

我找到了3种不同的方法来解决这个问题,所有这些都在Stackoverflow上,但我将与Tanner刚开始的方法一起发表,全文已发表:

function export_posts_in_json() {
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
    );

    $query = new WP_Query($args);
    $posts = array();

    while ($query->have_posts()): $query->the_post();
        $posts[] = array(
            'title' => get_the_title(),
            'excerpt' => get_the_excerpt(),
            'author' => get_the_author(),
            // any extra field you might need
        );
    endwhile;

    wp_reset_query();
    $data = json_encode($posts);
    $upload_dir = wp_get_upload_dir(); // set to save in the /wp-content/uploads folder
    $file_name = date('Y-m-d') . '.json';
    $save_path = $upload_dir['basedir'] . '/' . $file_name;

    $f = fopen($save_path, "w"); //if json file doesn't gets saved, comment this and uncomment the one below
    //$f = @fopen( $save_path , "w" ) or die(print_r(error_get_last(),true)); //if json file doesn't gets saved, uncomment this to check for errors
    fwrite($f, $data);
    fclose($f);
}

add_action('save_post', 'export_posts_in_json');

Original snippet here: https://wordpress.stackexchange.com/questions/232708/export-all-post-from-database-to-json-only-when-the-database-gets-updated

希望这会有所帮助。

答案 1 :(得分:2)

你应该能够沿着这些方向完成某些事情。看看下面的代码:

function export_posts_in_json () {

  $args = array(
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
  );

  $query = new WP_Query( $args );

  ...

  $data = json_encode($posts);

  $folder = 'YOUR_EXPORT_PATH_HERE';
  $file_name = date('Y-m-d') . '.json';
  file_put_contents($folder.$file_name, $data);
}

add_action( 'save_post', 'export_posts_in_json' );

这应该在每次发布帖子时保存json文件。我确信您可以修改它以导出您网站所需的所有数据。

答案 2 :(得分:1)

此方法允许您从外部或内部API端点编写json; 它不如上面的一种复杂(明智的是目的地文件夹),但是使用了REST API,因此您无需指定所有字段就可以获取完整的posts对象:

// Export API Data to JSON, another method
add_action('publish_post', function ($ID, $post) {

    $wp_uri = get_site_url();
    $customApiEndpoint = '/wp-json/wp/v2/posts'; // or your custom endpoint

    $url = $wp_uri . $customApiEndpoint; // outputs https://your-site.com/wp-json/wp/v2/posts
    // $url = 'https://your-site.com/wp-json/wp/v2/posts'; // use this full path variable in case you want to use an absolute path

    $response = wp_remote_get($url);
    $responseData = json_encode($response); // saved under the wp root installation, can be customized to any folder

    file_put_contents('your_api_data_backup.json', $responseData);

}, 10, 2);

inspired from https://stackoverflow.com/questions/46082213/wordpress-save-api-json-after-publish-a-post

答案 3 :(得分:1)

跟随更准确的答案,我将该函数包装到了自己的容器中,从而形成了标准的动作/函数结构。

// Export API Data to JSON, another method
add_action('publish_post', 'export_wp_rest_api_data_to_json', 10, 2);

function export_wp_rest_api_data_to_json($ID, $post) 
{
    $wp_uri = get_site_url();
    $customApiEndpoint = '/wp-json/wp/v2/posts'; // or your custom endpoint

    $url = $wp_uri . $customApiEndpoint; // outputs https://your-site.com/wp-json/wp/v2/posts
    // $url = 'https://your-site.com/wp-json/wp/v2/posts'; // use this full path variable in case you want to use an absolute path

    $response = wp_remote_get($url);
    $responseData = json_encode($response); // saved under the wp root installation, can be customized to any folder

    file_put_contents('your_api_data_backup.json', $responseData);
}