WordPress:发布帖子后保存 API JSON [英] WordPress: save API JSON after publish a post
问题描述
我需要获取我所有的博客文章,然后在发布文章后使用 WordPress API Rest 提供的确切结构将其保存在 json 文件中.所以我正在使用这个:
I need to grab all my blog posts and save then in a json file using the exactly structure provided by WordPress API Rest after a post is published. So I'm using this:
add_action('publish_post', function($ID, $post) {
$url = 'http://website.local/wp-json/wp/v2/posts/';
$response = wp_remote_get( $url );
file_put_contents('data.json', $response);
}, 10, 2);
但它返回一个错误:
可捕获的致命错误:类的对象Requests_Utility_CaseInsensitiveDictionary 无法转换为串入/var/www/public/wp-content/themes/twentyseventeen/functions.php 上第578行
Catchable fatal error: Object of class Requests_Utility_CaseInsensitiveDictionary could not be converted to string in /var/www/public/wp-content/themes/twentyseventeen/functions.php on line 578
推荐答案
其实需要使用json encode函数将返回值转成字符串:
Actually you need to use json encode function to convert the return to string:
$response = wp_remote_get( $url );
$responseData = json_encode($response);
这篇关于WordPress:发布帖子后保存 API JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!