Permalink
Browse files

增加获取文章列表的api

  • Loading branch information...
1 parent c470611 commit 720bf3d05b2e3c2a08a179d9c88700ae1b78aa5c @hjue committed Aug 18, 2014
Showing with 65 additions and 15 deletions.
  1. +10 −4 api.md
  2. +0 −1 application/config/routes.php
  3. +55 −10 application/controllers/api.php
View
@@ -1,10 +1,15 @@
<link rel="stylesheet" type="text/css" href="/css/GitHub2.css">
JustWriting Api
============
+### recently posted articles
+ /api/articles
+
+Http Method:GET
+
### Post article
- /api/article/post/
+ /api/articles
Http Method:Post
@@ -36,15 +41,16 @@ Failed:invalid api key ,http status code = 403
### Append Image/Text to Article
- /api/article/append/
+ /api/articles
Content-Type:multipart/form-data
-Post parameters
+Post parameters,PHP对PUT支持的不好,只能接着用Post了。
key|description
----|------
api_key|Api的密钥,在settings.php中设置
+action|append,没有这个值就会覆盖文章。
name|文章的文件名,去掉.md的部分
text|追加的内容
image|追加的图片
@@ -54,7 +60,7 @@ image|追加的图片
### Upload Image
- /api/image
+ /api/images
Post parameters
@@ -50,7 +50,6 @@
$route['sync/dropbox/(:any)'] = "dropbox/$1";
$route['api'] = "api/index";
-$route['api/article/post'] = "api/article/post";
$route['404_override'] = '';
@@ -1,5 +1,12 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+/*
+测试Api的curl
+curl http://localhost:8080/api/images -F api_key=1234561 -F image=@2.png
+curl http://localhost:8080/api/articles -d api_key=1234561 -d name=test -d text=AAAA
+curl http://localhost:8080/api/articles -F action=append -F api_key=1234561 -F image=@2.png -F name=test -F text=AAAb
+*/
+
class Api extends CI_Controller {
public function __construct()
@@ -37,22 +44,61 @@ private function check_auth()
}
- public function article($action)
+ public function articles()
{
- if($action=='post')
+ $method = strtolower($_SERVER['REQUEST_METHOD']);
+
+ if($method=='get')
+ {
+ $this->article_list ();
+ }
+
+ if($method=='post')
{
- $this->article_post();
+ $action = strtolower($this->input->post('action'));
+ if($action=='append'){
+ $this->article_append();
+ }else{
+ $this->article_post();
+ }
+
}
- if($action == 'append'){
- $this->article_append();
+ if($method == 'put'){
+ parse_str(file_get_contents("php://input"),$post_vars);
+ }
+ }
+
+ public function article_list()
+ {
+ $posts = $this->blog_lib->get_posts();
+ if($posts){
+ $posts = array_slice($posts,0,20);
+ $articles = array();
+ foreach($posts as $post){
+ foreach($post as $key=>$val){
+
+ if(!in_array($key,array('link','title','date','tags','intro'))){
+ unset($post[$key]);
+ }
+ }
+ $articles[]= $post;
+
+ }
+
+ echo json_encode($articles);
+ }else{
+ set_status_header(404);
+ echo json_encode(array('errorMsg'=>'no article found'));
+ exit;
}
+
}
/*
- curl http://localhost:8080/api/image -F api_key=1234561 -F image=@2.png
+ Post Image
*/
- public function image()
+ public function images()
{
$this->check_auth();
if(isset($_FILES))
@@ -75,7 +121,7 @@ public function image()
}
/*
- curl http://localhost:8080/api/article/post -d api_key=1234561 -d name=test -d text=AAAA
+ Post Article
*/
private function article_post()
{
@@ -98,12 +144,11 @@ private function article_post()
}
/*
- curl http://localhost:8080/api/article/append -F api_key=1234561 -F image=@2.png -F name=test -F text=AAAA
+ Append Text/Image to Article
*/
private function article_append()
{
-
$this->check_auth();
$name = $this->input->post('name');

0 comments on commit 720bf3d

Please sign in to comment.