简体   繁体   English

WordPress:发布文章后保存API JSON

[英]WordPress: save API JSON after publish a post

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. 发布帖子后,我需要抓住我所有的博客帖子,然后使用WordPress API Rest提供的确切结构将其保存到json文件中。 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);

But it's returning an error: 但它返回一个错误:

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 可捕获的致命错误:在第578行的/var/www/public/wp-content/themes/twentyseventeen/functions.php中无法将类Requests_Utility_CaseInsensitiveDictionary的对象转换为字符串

Actually you need to use json encode function to convert the return to string: 实际上,您需要使用json编码函数将返回值转换为字符串:

$response = wp_remote_get( $url );
$responseData = json_encode($response);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM