Look at the return example for the next API I chose this return style
{
"ResultCode": "200",
"Reason": "successed!",
"Result": {
"Count": 11,
"Commentlist": []
},
"Notice" ....
}
Current API access errors and causes of non-current API processing (such as notice notification Note: From Open source China iOS version of the code to understand that he each API may have a notice node) are similar to the result
A lot of times (especially when the app just started) often launch several API requests at the same time so I just want to be able to request a few APIs at the same time. I used PHP to do a test and I could do it.
<?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 * Get a picture of the novel * Articleno * * protected $accept _action_article_cover_key="Article_cover";/*** * chapter_list * Get a novel chapter list * Articleno * page pagesize */ protected $accept _action_chapter_list_key="Chapter_list";/*** * article_list * Get Fiction list * page pagesize */ protected $accept _action_article_list_key="Article_list";/*** * Chapter * Get the content of a section * Articleno Chapterno (17110 or 17110,17111) * Dtype txt JSON XML */ protected $accept _action_chapter_key="chapter";/*** * Article * get fiction Details * 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=NewArticle ($actionInfo);$response _data=$Article->articlecoverinfo (); Break; Case $this->accept_action_chapter_list_key:$Chapter=NewChapter ($actionInfo);$chapter _list=$Chapter->getlist ("Articleno =".$articleno);$response _data=$chapter _list; Break; Case $this->accept_action_article_list_key:$Article=NewArticle ($actionInfo);$response _data=$Article->getlist (); Break; Case $this->accept_action_chapter_key:$Chapter=NewChapter ($actionInfo);$response _data=$Chapter->gettxt ($articleno,$chapterno); Break; Case $this->accept_action_article_key:$Article=NewArticle ($actionInfo);$response _data=$Article->getarticle (); Break;default: Break; }return Array($action=$response _data); }}
1. Get the Operation Collection first
As you can see from the code, get all the data in the request
Each key corresponds to a JSON-formatted data
Json_decode ($action, TRUE); The next parameter is to let the parsed result be an array instead of an object
2, and then for each action to do the processing handleaction here in the method of using get post to get the parameters I used to pass the parameters in the constructor so that the action can only be passed here with the JSON has a relationship
Later you can add a user-certified 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(){ //Save Base64 string as Picture //Match the format of the picture $base 64_image_content=$_post["Base64_image_content"];if(Preg_match ('/^ (data:\s*image\/(\w+); base64,)/',$base 64_image_content,$result)){$type=$result[2];$new _file="./test. {$type} ";if(File_put_contents ($new _file, Base64_decode (Str_replace ($result[1],"',$base 64_image_content)))){Echo ' new file saved successfully: ',$new _file; } } } Public function articlecoverinfo(){ $image _file=$this->articlecoverfile ();$image _info= GetImageSize ($image _file);$base 64_image_content=Array();$base 64_image_content[Image_info] =$image _info;$base 64_image_content[Base64_contents] = Chunk_split (Base64_encode (file_get_contents ($image _file)));return $base 64_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)) {returnFile_get_contents ($file); }Else{return "The cover doesn't exist."; } }}
This image is specifically designed to lose the chapters of the novel in order to show the complete
The previous request method two requests were
...? action=chapter&chapterno=17110,17111
...? Action=article_list
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
API design multiple API access together