nginx+php+flight 构建RESTFul API

时间:2024-08-20 14:34:44

配置:

Nginx:

conf目录下nginx.conf配置文件。

第44行改为:root   D:/wwwroot/www;

第45行改为:index  index.html index.htm index.php;  加了PHP文件支持。

第65至71行改为:

(改为)location ~ \.php$ {

root        D:/wwwroot/www;
           fastcgi_pass 127.0.0.1:9000;
           fastcgi_index index.php;                   
           fastcgi_param SCRIPT_FILENAME   D:/wwwroot/www$fastcgi_script_name;                   
           include fastcgi_params;
    }

(原来)location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;

d:/wwwroot为网站的根目录。此目录也可以重新配置。找到代码:

location / {
            root   html;
            index  index.html index.htm index.php;
}

PHP:

查找定位至:;extension=php_gd2.dll,将前面的分号去掉为:extension=php_gd2.dll

查找定位至:;extension=php_mbstring.dll,将前面的分号去掉为:extension=php_mbstring.dll

查找定位至:;extension=php_mysql.dll,将前面的分号去掉为:extension=php_mysql.dll

查找定位至:;extension=php_mysqli.dll,将前面的分号去掉为:extension=php_mysqli.dll

查找定位至:;extension=php_pdo_mysql.dll,将前面的分号去掉为:extension=php_pdo_mysql.dll

查找定位至:;cgi.force_redirect = 1,将前面的分号去掉为:cgi.force_redirect = 1

查找定位至:;cgi.fix_pathinfo=1,将前面的分号去掉为:cgi.fix_pathinfo=1

查找定位至:;cgi.rfc2616_headers = 0,将前面的分号去掉并改为:cgi.rfc2616_headers = 1

flight源码包:https://github.com/mikecao/flight

Demo:index.php

<?php
require_once('flight/Flight.php');
require_once('response.php'); header("Content-Type:application/json"); function db()
{
$conn = mysql_connect("127.0.0.1","root","");
mysql_query("SET NAMES UTF8");
return $conn;
} function get_user_list()
{
$users =array();
$sql = "select * from wordpress.wp_users";
$result = mysql_query($sql,db());
while ($row = mysql_fetch_array($result)) {
$users[] = array('user_id'=>$row['ID'],
'user_login'=>$row['user_login'],
'user_nicename'=>$row['user_nicename'],
'user_email'=>$row['user_email'],
'user_registered'=>$row['user_registered'],
'user_name'=>$row['display_name']);
} return $users;
} function get_user_derail($id)
{
$user =array();
$sql = "select * from wordpress.wp_users where id=".$id;
$result = mysql_query($sql,db());
while ($row = mysql_fetch_array($result)) {
$user = array('user_id'=>$row['ID'],
'user_login'=>$row['user_login'],
'user_nicename'=>$row['user_nicename'],
'user_email'=>$row['user_email'],
'user_registered'=>$row['user_registered'],
'user_name'=>$row['display_name']);
} return $user;
} Flight::route('/users', function(){
$users = get_user_list();
Response::show('200','返回成功',$users);
}); Flight::route('/users/@id', function($id){ $user = get_user_derail($id); if(!empty($user))
{
//Flight::json($user);
Response::show('200','返回成功',$user);
}
else
{
Response::show('404','资源不存在',$user);
}
}); Flight::start();
?>

GET: http://localhost:8090/users

{
code: "200",
message: "返回成功",
data: [
  {
    user_id: "1",
    user_login: "11",
    user_nicename: "11",
    user_email: "11.cd@11.corp",
    user_registered: "2014-05-07 02:33:19",
    user_name: "111"
  },
  {
    user_id: "2",
    user_login: "leoliu",
    user_nicename: "leoliu",
    user_email: "leo.j.liu@11.com",
    user_registered: "2014-05-07 02:54:07",
    user_name: "刘, 建波"
  },
  {
    user_id: "3",
    user_login: "1",
    user_nicename: "1",
    user_email: "11.j.11@11.com",
    user_registered: "2014-05-07 02:59:24",
    user_name: "吴, 11"
  }
]
}

GET:http://localhost:8090/users/{userId}

{
code: "200",
message: "返回成功",
data: [
{
  user_id: "3",
  user_login: "1",
  user_nicename: "1",
  user_email: "11.j.11@11.com",
  user_registered: "2014-05-07 02:59:24",
  user_name: "吴, 11"
}
]
}