让 typecho 支持 JSON 输出,做小程序API接口

<?php
if (stripos($this->request->getRequestUri(), 'json')) {
    $arr = array();
    while ($this->next()) {
        $this->categories = $this->categories[0];
        $content = htmlspecialchars_decode($this->content);
        $content = preg_replace( "@<script(.*?)</script>@is", "", $content );
        $content = preg_replace( "@<iframe(.*?)</iframe>@is", "", $content );
        $content = preg_replace( "@<style(.*?)</style>@is", "", $content );
        $this->content = preg_replace( "@<(.*?)>@is", "", $content );

        $a = array('title' => $this->title, 'date' => $this->date->format('F j, Y'), 'content' => $this->content, 'url' => $this->permalink, 'categories' => $this->categories, 'tags' => $this->tags);
        $arr[] = $a;
    }
    $num = count($arr);
    $this->response->throwJson(array("status" => 1,"num" => $num, "data" => $arr));
}
?>
来源:中战博客,欢迎分享!
标签: typecho,json,小程序,api,接口