用slim写的一个RESTful服务

php写rest服务,以下是一些资料和一个例子。仅供学习。

REST 资料:http://www.sitepoint.com/rest-can-you-do-more-than-spell-it-1/

SLIM资料:http://www.slimframework.com/(官网)

http://minimee.org/php/slim(中文文档)

https://github.com/codeguy/Slim-Extras/tree/master/Middleware(Slim的一些插件开源地址)

SLIM示例:http://www.sitepoint.com/writing-a-restful-web-service-with-slim/

同时推荐一个PHP数据库ORM类:http://www.notorm.com/

下面是我的例子:

  • GET /books: Retrieve a list of items
  • GET /book/123: Retrieve item 123
  • POST /book: Create a new item
  • PUT /book/123: Update item 123
  • DELETE /book/123: Remove item 123
<?php

//导入类库
require 'Slim/Slim.php';

//注册Slim框架自带的自动加载类
\Slim\Slim::registerAutoloader();

$dsn = "mysql:dbname=test;host=localhost";
$username = "root";
$password = "";

require "NotORM.php";
$pdo = new PDO($dsn, $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//设置编码
$pdo->query('SET NAMES utf8');
$db = new NotORM($pdo);


//创建实例
$app = new \Slim\Slim(array(
    "MODE" => "development",
    "TEMPLATES.PATH" => "./templates"
));

$app->get("/", function() {
    echo "<h1>Hello Slim World</h1>";
});

$app->get("/books", function () use ($app, $db) {
    $books = array();
    foreach ($db->books()->limit(5) as $book) {
        $books[]  = array(
            "id" => $book["id"],
            "title" => $book["title"],
            "author" => $book["author"],
            "summary" => $book["summary"]
        );
    }
    $app->response()->header("Content-Type", "application/json");
    echo json_encode($books);
});


$app->get("/book/:id", function ($id) use ($app, $db) {
    $app->response()->header("Content-Type", "application/json");
    $book = $db->books()->where("id", $id);
    if ($data = $book->fetch()) {
        echo json_encode(array(
            "id" => $data["id"],
            "title" => $data["title"],
            "author" => $data["author"],
            "summary" => $data["summary"]
            ));
    }
    else{
        echo json_encode(array(
            "status" => false,
            "message" => "Book ID $id does not exist"
            ));
    }
});

$app->post("/book", function () use($app, $db) {
    $app->response()->header("Content-Type", "application/json");
     $input =$app->request()->getBody();
	 $input = json_decode($input); 
	 //取值
	 $book["title"]=$input->title  ;
	 $book["author"]=$input->author  ;
	 $book["summary"]=$input->summary  ;
	 //存储数据库
    $result = $db->books->insert($book);
    echo json_encode(array("id" => $result["id"]));
});


$app->put("/book/:id", function ($id) use ($app, $db) {
    $app->response()->header("Content-Type", "application/json");
    $book = $db->books()->where("id", $id);
    if ($book->fetch()) {
        //$post = $app->request()->getBody();
		//$post["author"]="test";
		//获取put包
		$input =$app->request()->getBody();
		//转换json对象
	    $input = json_decode($input); 
		//赋值
		$post["title"]=$input->title  ;
	    $post["author"]=$input->author  ;
	    $post["summary"]=$input->summary  ;
		$post["id"]=$id  ;
	    
        $result = $book->update($post);
        echo json_encode(array(
            "status" => (bool)$result,
            "message" => "Book updated successfully"
            ));
    }
    else{
        echo json_encode(array(
            "status" => false,
            "message" => "Book id $id does not exist"
        ));
    }
});

$app->delete("/book/:id", function ($id) use($app, $db) {
    $app->response()->header("Content-Type", "application/json");
    $book = $db->books()->where("id", $id);
    if ($book->fetch()) {
        $result = $book->delete();
        echo json_encode(array(
            "status" => true,
            "message" => "Book deleted successfully"
        ));
    }
    else{
        echo json_encode(array(
            "status" => false,
            "message" => "Book id $id does not exist"
        ));
    }
});

$app->run();

 

此条目发表在日记分类目录,贴了, , , 标签。将固定链接加入收藏夹。

用slim写的一个RESTful服务》有1条回应

  1. hua说:

    require和rquire_once的区别是?

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据