php 通过REST API获取WP帖子

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 通过REST API获取WP帖子相关的知识,希望对你有一定的参考价值。

<?php
class foo {
	
/**
 * Return sites of MU.
 * $sites object
 */
public function get_sites() {
	
	$sites = get_sites();
	return $sites;
}
	
/**
 * Parse posts.
 */
public function get_remote_posts() {

	$posts = get_transient( 'remote_posts' );

	if ( empty( $posts ) ) {
		$response = wp_remote_get( 'http://mysite.com/wp-json/wp/v2/posts/' );
		
	if ( is_wp_error( $response ) ) {
		return array();
	}

	$posts = json_decode( wp_remote_retrieve_body( $response ) );
	
		if ( empty( $posts ) ) {
			return array();
		}
	
		set_transient( 'remote_posts', $posts, HOUR_IN_SECONDS );
	}

	return $posts;
}
	
}

以上是关于php 通过REST API获取WP帖子的主要内容,如果未能解决你的问题,请参考以下文章