Apache+PHP 实现基于Slim的REST框架 调用系统命令或自己开发的程序

经过多方查找总结出来  Apache+PHP 实现基于Slim的REST框架   做个笔记。

1.在ubuntu下配置了一个Apache服务器。

步骤一,安装apache2

1
sudo apt-get install apache2

安装完成。 
运行如下命令重启下:

1
sudo /etc/init.d/apache2 restart

在浏览器里输入http://localhost或者是http://127.0.0.1,如果看到了It works!,那就说明Apache就成功的安装了,Apache的默认安装,会在/var下建立一个名为www的目录,这个就是Web目录了,所有要能过浏览器访问的Web文件都要放到这个目录里。

步骤二 ,安装php:

1
sudo apt-get install libapache2-mod-php5 php5

此外,建议安装扩展php5-gd php5-mysql,安装方式同上.

安装完后,我们要重新启动Apache,让它加载PHP模块:

1
sudo /etc/init.d/apache2 restart

接下来,我们就在Web目录下面新建一个test.php文件来测试PHP是否能正常的运行,命令:

1
sudo gedit /var/www/test.php

然后输入:

1
2
     
<?php echo &quot;hello,world!!&quot;?>

接着保存文件,在浏览器里输入http://127.0.0.1/test.php,如果在网页中显示hello,world!!,那就说明PHP已经正常运行了。

2.实现基于Slim的REST框架

   slim是一个简单而又强大的PHP5框架,可以用来创建RESTful的web应用。RESTFul架构对物联网非常重要,通过Slim的学习也加深对RESTFul框架和相关技术的理解。

步骤三 ,安装slim:

最简单粗暴和直接的方法——到github下载zip文件,slim github【链接】。解压之后把【1】Slim文件夹,【2】.htaccess文件和【3】index.php文件复制到www目录中。若看到slim网页说明slim安装成功。


步骤四 ,简单框架:

    Slim提供完善的REST框架,支持GET、POST、PUT和Delete等方法,
    可从以下代码中可以熟悉Slim的基本框架和使用方法。
a.创建test.php。
[php]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. /** 
  3.  * Step 1: Require the Slim Framework 
  4.  * 
  5.  * If you are not using Composer, you need to require the 
  6.  * Slim Framework and register its PSR-0 autoloader. 
  7.  * 
  8.  * If you are using Composer, you can skip this step. 
  9.  */  
  10. require 'Slim/Slim.php';  
  11.   
  12. \Slim\Slim::registerAutoloader();  
  13.   
  14. /** 
  15.  * Step 2: Instantiate a Slim application 
  16.  * 
  17.  * This example instantiates a Slim application using 
  18.  * its default settings. However, you will usually configure 
  19.  * your Slim application now by passing an associative array 
  20.  * of setting names and values into the application constructor. 
  21.  */  
  22. $app = new \Slim\Slim();  
  23.   
  24. /** 
  25.  * Step 3: Define the Slim application routes 
  26.  * 
  27.  * Here we define several Slim application routes that respond 
  28.  * to appropriate HTTP request methods. In this example, the second 
  29.  * argument for `Slim::get`, `Slim::post`, `Slim::put`, `Slim::patch`, and `Slim::delete` 
  30.  * is an anonymous function. 
  31.  */  
  32.   
  33. // GET route  
  34. $app->get(  
  35.     '/test/get',  
  36.     function () {  
  37.         echo 'hello slim';  
  38.     }  
  39. );  
  40.   
  41. // POST route  
  42. $app->post(  
  43.     '/test/post',  
  44.     function () {  
  45.         echo 'This is a POST route';  
  46.     }  
  47. );  
  48.   
  49. // PUT route  
  50. $app->put(  
  51.     '/test/put',  
  52.     function () {  
  53.         echo 'This is a PUT route';  
  54.     }  
  55. );  
  56.   
  57. // PATCH route  
  58. $app->patch('/test/patch'function () {  
  59.     echo 'This is a PATCH route';  
  60. });  
  61.   
  62. // DELETE route  
  63. $app->delete(  
  64.     '/test/delete',  
  65.     function () {  
  66.         echo 'This is a DELETE route';  
  67.     }  
  68. );  
  69.   
  70. /** 
  71.  * Step 4: Run the Slim application 
  72.  * 
  73.  * This method should be called last. This executes the Slim application 
  74.  * and returns the HTTP response to the HTTP client. 
  75.  */  
  76. $app->run();  
  77.   >
b.测试REST

在浏览器中输入http://localhost/test.php/get

显示:hello slim

      其他类型的测试方法可借助cURL工具
    【1】测试post
    curl --request POST  http://localhost/test.php/post
    【2】测试put方法
    curl --request PUT  http://localhost/test.php/p ut
    【3】测试delete
    curl --request DELETE  http://localhost/test.php/ delete

c.用PHP来执行系统命令

  php的内置函数exec、system都可以调用系统命令(dos和shell命令),passthru和escapeshellcmd也可以。

  使用这两个函数就需要在php.ini中将安全模式关闭,否则为了安全期间,php是不让调用系统命令的。

  exec --- 执行外部程式

  语法:string exec (string command [,array &output [,int &return_var]])

  参数:1.command:系统命令;

     2.output:数组,被命令输出的每一行填满;

     3.return_var:状态,成功返回0,失败返回1.

  system --- 执行外部程式并且显示输出

  语法:string system (string command [,int &return_var])

  不同点:

  exec可以把执行的结果全部返回到$output数组里,$return_var是执行状态,0为成功,1为失败;

  system不需要提供$output函数,它可以直接把结果返回来并且打印出来,同意$return_var是执行的状态码,0为成功,1为失败。

例如,将GET 修改为:

  1. // GET route  
  2. $app->get(  
  3.     '/test/get',  
  4.     function () {  
  5.         echo 'hello slim';  
  6. exec("ls /",$dirs);
  7. var_dump($dir);
  8.     }  
  9. ); 

将输出  根目录下的文件的信息。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值