SOA
SOA:(面向服务的架构 Service-Oriented Architecture) 就是说此类架构是面向服务的。原理是抽象底层的复杂关系,使用户可以简单调用接口服务。
下面讲到的 SOAP RPC 等都是 SOA 的一种实现而已。
非 WSDL 的 SOAP
// test.php 原始脚本
class ServiceFunction
{
public function getDisplayName($firstName, $lastName)
{
$name = strtoupper(substr($firstName, 0, 1)) . " " . ucfirst($lastName);
return $name;
}
}
$options = [
"uri" => "http://localhost/",
];
$server = new SoapServer(NULL, $options);
$server->setClass("ServiceFunction");
$server->handle();
// tmp.php 调用脚本
$options = [
"uri" => "http://localhost/",
"location" => "http://localhost/b/test.php",
"trace" => 1
];
$client = new SoapClient(NULL, $options);
echo $client->getDisplayName("michael", "leon");
# 输出: M Leon
/*
SOAP :Simple Object Access Protocol 简单对象访问协议
WSDL (web service description language): 用来描述 web 服务的一组定义。 本来 soap 就比较复杂,用的人较少。现在与 WSDL 结合将更加复杂。这里就不再举例。详情百度。
*/
RPC
RPC : Remote Procedure Call Protocol 远程程序调用协议
SOAP 是一种特殊的 xml-RPC。个人理解,SOAP 是类的远程调用。而 RPC 是方法或函数的远程调用。
require "Service.class.php";
if(isset($_GET["method"])){
switch($_GET["method"]){
case "countWords":
$response=Service::countWords($_GET["words"]);
break;
case "getDisplayName":
// ...
}
}
header("Content-Type:application/json");
echo json_encode($response);
// 调用脚本
GET http://foo.foo?words=michael
REST
REST : Representational State Transfer 表述性状态转移
性能、效率和易用性上都优于SOAP协议。
REST架构对资源的操作包括获取、创建、修改和删除资源的操作正好对应HTTP协议提供的GET、POST、PUT和DELETE方法(Verb)。
$request=new Request();
$request->urlElements=[];
if(isset($_SERVER['PATH_INFO'])){
$request->urlElements=explode('/',$_SERVER['PATH_INFO']);
}
switch($_SERVER['REQUEST_METHOD']){
case "GET":
...
break;
case "POST":
...
break;
case "PUT":
...
break;
}