四种php中webservice实现的简单架构方法及实例
2024-11-25 22:22:27
<?php
// 创建 SOAP 服务器对象
$server = new SoapServer(null, array('uri' => 'http://example.com/webservice'));
// 定义服务方法
$server->addFunction('helloWorld');
function helloWorld() {
return 'Hello, World!';
}
// 处理请求
$server->handle();
?><?php
// 创建 XML-RPC 服务器对象
$server = xmlrpc_server_create();
// 定义服务方法
xmlrpc_server_register_method($server, 'helloWorld', 'helloWorld');
function helloWorld($request) {
return 'Hello, World!';
}
// 处理请求
xmlrpc_server_call_method($server, $_POST['xml'], null);
?><?php
// 处理 GET 请求
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
// 返回数据
echo json_encode(array('message' => 'Hello, World!'));
}
?><?php // 创建服务对象 $service = new MyWebService(); // 处理请求 $response = $service->handleRequest(); // 返回响应 echo $response; ?>
