api 设计 多api一起访问

来源:互联网 发布:奇兔刷机软件下载 编辑:程序博客网 时间:2024/06/02 01:24

查看了下别的api的返回示例 我选出这样的返回样式
{
“resultcode”:”200”,
“reason”:”Successed!”,
“result”:{
“count”: 11,
“commentlist”: []
},
“notice”….
}
当前api访问的错误和原因加 非当前api的处理(比如notice 通知 注:来自开源中国ios版本的代码理解 他每个api都有可能有notice节点) 都跟result同级

很多时候(特别是app刚刚启动)经常会同时发起好几个api请求 于是乎我就想 能不能几个api同时请求呢我就使用php来做了个测试结果是可以做到的

<?phpnamespace Home\Controller;use Think\Controller;use Home\Action\Article;use Home\Action\Chapter;class IndexController extends Controller {    protected $response_data = array ();    protected function response() {        if (count ( $this->response_data ) > 0) {            switch ($this->dtype) {                case "xml" :                    exit ( xml_encode ( $this->response_data ) );                    break;                default :                    exit ( json_encode ( $this->response_data ) );                    break;            }        }    }     public function index() {        $actions = array();        foreach ( $_GET  as $key => $action ) {            $actions[$key] = $action;        }        foreach ( $_POST  as $key => $action ) {            $actions[$key] = $action;        }        foreach ( $actions  as $key => $action ) {            $action = json_decode($action,TRUE);            $this->addResponseData($key,$action);        }        $this->response();    }    public function addResponseData($key,$action){        $this->response_data[$key] = $this->handleAction($action);    }    /***     * article_cover    * 获取小说的图片    * articleno    */    protected $accept_action_article_cover_key = "article_cover";    /***     * chapter_list    * 获取小说章节list    * articleno    * page pagesize    */    protected $accept_action_chapter_list_key = "chapter_list";    /***     * article_list    * 获取小说list    * page pagesize    */    protected $accept_action_article_list_key = "article_list";    /***     * chapter    * 获取某章节的内容    * articleno chapterno(17110  或者 17110,17111)    * dtype txt json xml    */    protected $accept_action_chapter_key = "chapter";    /***     * article    * 获取小说详情    * articleno    * page pagesize    */    protected $accept_action_article_key = "article";    public function handleAction($actionInfo){        $action = $actionInfo["action"];        $articleno = $actionInfo["articleno"];        $chapterno = $actionInfo["chapterno"];        switch ($action) {            case $this->accept_action_article_cover_key :                $Article = new Article ($actionInfo);                $response_data =  $Article->ArticleCoverInfo ();                break;            case $this->accept_action_chapter_list_key :                $Chapter = new Chapter($actionInfo);                $chapter_list = $Chapter->getList( "articleno = " . $articleno );                $response_data =  $chapter_list;                break;            case $this->accept_action_article_list_key :                $Article = new Article($actionInfo);                $response_data =   $Article->getlist();                break;            case $this->accept_action_chapter_key :                $Chapter = new Chapter($actionInfo);                $response_data =  $Chapter->getTxt($articleno, $chapterno);                break;            case $this->accept_action_article_key :                $Article = new Article($actionInfo);                $response_data =  $Article->getArticle();                break;            default :                break;        }        return array($action=>$response_data);    }}

1、首先获取操作集合
从代码中可以看出是获取所有请求中的数据
每个key对应json格式的数据
json_decode($action,TRUE); 后面这个参数 是让解析的结果是数组而不是对象

2、然后针对每一个action做处理handleAction 此处处理中 就么法使用 get post来获取参数 了 我使用的是 在构造函数中 把参数传递进去 这样就可以把这个action只跟此处传递来的json有关系
后期可以加上一个user认证的json

<?phpnamespace Home\Action;use Home\Action\BaseAction;class Article extends BaseAction{    protected $articleno;    //protected $file_base = "F:/yixuan_android/WWW/thinkphp_git/Public/article";    protected $file_base = "d:/wwwroot/thinkphp_api/wwwroot/Public/article";    protected $file_image_base = "/image/0/";    public function __construct($info){        parent::__construct($info);        $this->table_name = "article";        $this->articleno = $info["articleno"];    }    public function getArticle($articleno = -1){        if ($articleno != -1) {            $this->articleno = $articleno;        }        return $this->getModel()->find ( $this->articleno );    }    public function ArticleList(){        return $this->getList();    }    public function SaveCover(){        //保存base64字符串为图片        //匹配出图片的格式        $base64_image_content = $_POST["base64_image_content"];        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){            $type = $result[2];            $new_file = "./test.{$type}";            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){                echo '新文件保存成功:', $new_file;            }        }    }    public function ArticleCoverInfo(){        $image_file = $this->ArticleCoverFile();        $image_info = getimagesize($image_file);        $base64_image_content = array();        $base64_image_content[image_info] =$image_info;        $base64_image_content[base64_contents] = chunk_split(base64_encode(file_get_contents($image_file)));        return $base64_image_content;    }    public function ArticleCoverFile() {        return  $this->file_base . $this->file_image_base . "/" . $this->articleno . "/" . $this->articleno . "s" . ".jpg";    }    public function ArticleCover() {        $file = $this->ArticleCoverFile();        if (file_exists ( $file )) {            return file_get_contents ( $file );        } else {            return "封面不存在";        }    }}

此图中专门让小说章节丢失为的是能展示完整
这里写图片描述

以前的请求方式两个请求分别是
…?action=chapter&chapterno=17110,17111
…?action=article_list

0 0