我正在尝试使用nussoap实现一个简单的web服务。服务器:
<?php
require_once "nusoaplib/nusoap.php";
class food {
public function getFood($type) {
switch ($type) {
case 'starter':
return 'Soup';
break;
case 'Main':
return 'Curry';
break;
case 'Desert':
return 'Ice Cream';
break;
default:
break;
}
}
}
$server = new soap_server();
$server->configureWSDL("foodservice", "urn:foodservice");
$server->register("food.getFood",
array("type" => "xsd:string"),
array("return" => "xsd:string"),
"urn:foodservice",
"urn:foodservice#getFood",
"rpc",
"encoded",
"Get food by type");
@$server->service($HTTP_RAW_POST_DATA);
?>
客户:<?php
require_once "nusoaplib/nusoap.php";
$client = new nusoap_client("http://localhost/SOAPServer.php?wsdl", true);
$error = $client->getError();
if ($error) {
echo "<h2>Constructor error</h2><pre>" . $error . "</pre>";
}
$result = $client->call("food.getFood", array("type" => "Main"));
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>Main</h2>";
echo $result;
}
}
// show soap request and response
echo "<h2>Request</h2>";
echo "<pre>" . htmlspecialchars($client->request, ENT_QUOTES) . "</pre>";
echo "<h2>Response</h2>";
echo "<pre>" . htmlspecialchars($client->response, ENT_QUOTES) . "</pre>";
?>
wsdl文件由nussoap生成,如下所示:
<definitions xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:foodservice" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="urn:foodservice">
<types>
<xsd:schema targetNamespace="urn:foodservice">
<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
<xsd:import namespace="http://schemas.xmlsoap.org/wsdl/"/>
</xsd:schema>
</types>
<message name="food.getFoodRequest">
<part name="type" type="xsd:string"/>
</message>
<message name="food.getFoodResponse">
<part name="return" type="xsd:string"/>
</message>
<portType name="foodservicePortType">
<operation name="food.getFood">
<documentation>Get food by type</documentation>
<input message="tns:food.getFoodRequest"/>
<output message="tns:food.getFoodResponse"/>
</operation>
</portType>
<binding name="foodserviceBinding" type="tns:foodservicePortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="food.getFood">
<soap:operation soapAction="urn:foodservice#getFood" style="rpc"/>
<input>
<soap:body use="encoded" namespace="urn:foodservice" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body use="encoded" namespace="urn:foodservice" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
</binding>
<service name="foodservice">
<port name="foodservicePort" binding="tns:foodserviceBinding">
<soap:address location="http://10.152.128.39/SOAPServer.php"/>
</port>
</service>
</definitions>
当访问服务器文件和wsdl文件时,它们都可以工作,但是当我试图访问客户端时,我得到错误消息:
[faultcode] => SOAP-ENV:Client
[faultactor] =>
[faultstring] => error in msg parsing:
xml was empty, didn't parse!
[detail] =>
有什么建议吗?
在您的肥皂。服务器:
:
@$server->service($HTTP_RAW_POST_DATA);
:
@$server->service(file_get_contents("php://input"));
如果您想检查通知和警告,可以删除@。
一些解释从http://php.net/manual/en/ini.core.php#ini.always-populate-raw-post-data
此功能在PHP 5.6.0中已弃用,并在PHP 7.0.0中删除。如果设置为TRUE, PHP将始终填充包含原始POST数据的$HTTP_RAW_POST_DATA。否则,只有当数据的MIME类型无法识别时才填充变量。
访问原始POST数据的首选方法是php://input,而$HTTP_RAW_POST_DATA在php 5.6.0以后已弃用。将always_populate_raw_post_data设置为-1将选择在PHP的未来版本中实现的新行为,其中永远不会定义$HTTP_RAW_POST_DATA。
无论如何设置,$HTTP_RAW_POST_DATA都不可用enctype="multipart/form-data"