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);
}
);
将输出 根目录下的文件的信息。
1.在ubuntu下配置了一个Apache服务器。
步骤一,安装apache2
运行如下命令重启下:
步骤二 ,安装php:
安装完后,我们要重新启动Apache,让它加载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);
}
);
将输出 根目录下的文件的信息。
相关文章推荐
- 基于配置实现信息管理系统开发基础框架
- DWZ富客户端框架(jQuery RIA framework), 是中国人自己开发的基于jQuery实现的Ajax RIA开源框架.
- MySQL 实现调用外部程序和系统命令
- Firemonkey实现Mac OS程序中内嵌浏览器的功能(自己动手翻译,调用苹果提供的webkit框架)
- MySQL 实现调用外部程序和系统命令
- MySQL 实现调用外部程序和系统命令
- java开发系统内核:实现基于FAT12文件系统的dir命令
- java开发系统内核:cls 屏幕清空命令的实现
- Ubutnu系统下用QT等IDE调用Caffe、SSD框架,编译Caffe实现分类检测等工程应用的方法
- java开发系统内核:实现type命令读取文件内容
- 基于Slim微型框架实现强大的API—— Slim入门篇
- RDIFramework.NET — 基于.NET的快速信息化系统开发框架 — 系列目录
- 【1】基于quartz框架和Zookeeper实现集群化定时任务系统
- 【Qt编程】基于Qt的词典开发系列<八>--用户登录及API调用的实现
- angular JS 基于ionic框架 开发移动端项目 实现进入前台 判断用户权限 控制项目UI布局和tab的部门显示和隐藏
- 基于Struts2,Spring4,Hibernate4框架的系统架构设计与示例系统实现
- java开发系统内核:实现系统API调用
- 基于Metronic的Bootstrap开发框架经验总结(9)--实现Web页面内容的打印预览和保存操作
- java开发系统内核:使用一个中断实现多个API调用
- android 调用系统命令实现关机2017亲测有效