PHP fopen办法
< ?php $handle = fopen("http://yoursite.com/news","rb"); $content = ""; while (!feof($handle)) { $content .= fread($handle, 10000); } fclose($handle); $content_array = json_decode($content); #JSON内容转换为PHP工具?>
WordPress HTTP API办法
WordPress给我们供应一套很方便的HTTP API(详细利用方法见WordPress HTTP API),我们可以利用HTTP API很方便的实现上面的功能。

< ?php$content = wp_remote_retrieve_body( wp_remote_get(‘http://yoursite.com/news‘) );$content_obj = json_decode($content); #JSON内容转换为PHP工具?>
可以看出,WordPress的方法实在是太大略了,一行代码就实现了PHPfopen方法几行的功能,并且功能更加语义化,理解起来更随意马虎。
显示获取的内容
已经获取了JSON的内容,并转化为了PHP工具,显示的时候直接循环输出工具的内容就可以了。
< ?php foreach ($content_obj->data as $key) { ?>< ?php echo $key->title; ?>< ?php } ?>
适当的格式化上面的笔墨,显示出来的效果和直接调用WordPress内容是一样的。
详细解释:http://wphun.com/f/wp_remote_retrieve_body
源文件:
wp_remote_retrieve_body() 函数的代码位于 wp-includes/http.php
function wp_remote_retrieve_body( $response ) {if ( is_wp_error($response) || ! isset($response['body']) ) return '';return $response['body'];}