您的位置:首页 > 运维架构 > Apache

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

2015-04-22 17:47 781 查看
经过多方查找总结出来 Apache+PHP 实现基于Slim的REST框架 做个笔记。

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

步骤一,安装apache2

安装完成。

运行如下命令重启下:

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

步骤二 ,安装php:

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

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

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

然后输入:

接着保存文件,在浏览器里输入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
plaincopy





<?php

/**

* Step 1: Require the Slim Framework

*

* If you are not using Composer, you need to require the

* Slim Framework and register its PSR-0 autoloader.

*

* If you are using Composer, you can skip this step.

*/

require 'Slim/Slim.php';

\Slim\Slim::registerAutoloader();

/**

* Step 2: Instantiate a Slim application

*

* This example instantiates a Slim application using

* its default settings. However, you will usually configure

* your Slim application now by passing an associative array

* of setting names and values into the application constructor.

*/

$app = new \Slim\Slim();

/**

* Step 3: Define the Slim application routes

*

* Here we define several Slim application routes that respond

* to appropriate HTTP request methods. In this example, the second

* argument for `Slim::get`, `Slim::post`, `Slim::put`, `Slim::patch`, and `Slim::delete`

* is an anonymous function.

*/

// GET route

$app->get(

'/test/get',

function () {

echo 'hello
slim';

}

);

// POST route

$app->post(

'/test/post',

function () {

echo 'This is a POST route';

}

);

// PUT route

$app->put(

'/test/put',

function () {

echo 'This is a PUT route';

}

);

// PATCH route

$app->patch('/test/patch', function () {

echo 'This is a PATCH route';

});

// DELETE route

$app->delete(

'/test/delete',

function () {

echo 'This is a DELETE route';

}

);

/**

* Step 4: Run the Slim application

*

* This method should be called last. This executes the Slim application

* and returns the HTTP response to the HTTP client.

*/

$app->run();

>

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/put
【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 修改为:

// GET route

$app->get(

'/test/get',

function () {

echo 'hello
slim';

exec("ls /",$dirs);

var_dump($dir);

}

);

将输出 根目录下的文件的信息。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐