2017-09-06 3 views
0

投稿が公開された後、WordPress API Restが提供する正確な構造を使用して、すべてのブログ投稿を取得してjsonファイルに保存する必要があります。 WordPress:投稿を公開した後にAPI JSONを保存する

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); 

しかし、それはエラーを返すだ:だから私は、これを使用してい

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

+0

file_put_contentsを呼び出すと、print_r($応答)を行い、出力が何かを確認できますか? $ responseはオブジェクトで、文字列として解釈する方法はわかりません。 – victor

+1

$ response = wp_remote_get($ url); jsondecodeを実行して2番目のパラメータをtrueに設定するよりjson_decode($ response、true); –

+0

@Aki実際、json_encodeを実行する必要があります。先端に感謝します。 – marcelo2605

答えて

0

は実際には文字列への復帰を変換するには、JSONエンコード機能を使用する必要があります前に

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

なぜあなたは本当ですか?それは 'JSON_HEX_TAG'と 'json_encode'を解決します。 – jh1711

関連する問題