我需要一些明确的代码示例来创建Web服务和客户端。
在回答中,我提供了一些示例,并且您可以找到更多解释问题的页面的网址,对于初级程序员来说是一个很好的参考。
答案 0 :(得分:0)
我发现这篇文章很容易解释如何在PHP中创建WS服务和客户端,url是下一个:
http://thenullpointerexceptionx.blogspot.mx/2016/05/crear-web-services-con-php-y-soap-basico.html
示例代码为:
服务器强>
<?php
require_once "nusoap.php";
function getProd($category) {
if ($category == "books") {
return join(",", array(
"The WordPress Anthology",
"PHP Master: Write Cutting Edge Code",
"Build Your Own Website the Right Way"));
}
else {
return "No products listed under that category";
}
}
$server = new soap_server();
$server->register("getProd");
$server->service($HTTP_RAW_POST_DATA);
?>
和客户:
<?php
require_once "nusoap.php";
$client = new nusoap_client("http://localhost/nusoap/productlist.php");
$error = $client->getError();
if ($error) {
echo "<h2>Constructor error</h2><pre>" . $error . "</pre>";
}
$result = $client->call("getProd", array("category" => "books"));
if ($client->fault) {
echo "<h2>Fault</h2><pre>";
print_r($result);
echo "</pre>";
}
else {
$error = $client->getError();
if ($error) {
echo "<h2>Error</h2><pre>" . $error . "</pre>";
}
else {
echo "<h2>Books</h2><pre>";
echo $result;
echo "</pre>";
}
}
?>