无法在php中创建我的第一个SOAP服务
我正在尝试构建我的第一个SOAP web服务器,但失败了。我尝试了几本教程,但都不管用。以下是我迄今为止尝试过的内容: 服务器脚本:无法在php中创建我的第一个SOAP服务,php,soap,Php,Soap,我正在尝试构建我的第一个SOAP web服务器,但失败了。我尝试了几本教程,但都不管用。以下是我迄今为止尝试过的内容: 服务器脚本: require_once "lib/nusoap.php"; function getProd($category) { if ($category == "books") { return join(",", array( "The WordPress Anthology", "PHP M
require_once "lib/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->soap_defencoding = 'utf-8';
$server->register("getProd");
$server->service($HTTP_RAW_POST_DATA);
require_once "lib/nusoap.php";
$client = new nusoap_client("https://mydomain/soap_server.php");
$client->soap_defencoding = 'utf-8';
$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>";
}
}
echo '<h2> Request</h2><pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre></br>';
echo '<h2> Response</h2><pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre></br>';
echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre></br>';
客户端脚本:
require_once "lib/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->soap_defencoding = 'utf-8';
$server->register("getProd");
$server->service($HTTP_RAW_POST_DATA);
require_once "lib/nusoap.php";
$client = new nusoap_client("https://mydomain/soap_server.php");
$client->soap_defencoding = 'utf-8';
$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>";
}
}
echo '<h2> Request</h2><pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre></br>';
echo '<h2> Response</h2><pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre></br>';
echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre></br>';
请求
POST/dss/soap\u server.php HTTP/1.0
主持人:mydomain
用户代理:NuSOAP/0.9.5(1.123)
内容类型:text/xml;字符集=utf-8
SOAPAction:“
内容长度:507
书
响应
HTTP/1.1500内部服务器错误
服务器:nginx
日期:2018年8月26日星期日07:49:26 GMT
内容类型:text/xml;字符集=utf-8
内容长度:684
连接:保持活力
X-SOAP-Server:NuSOAP/0.9.5(1.123)
消息解析中的SOAP-ENV:Clienterror:
xml是空的,没有&apos;t解析!
如果需要,我还可以提供htmlspecialchars($client->debug\u str,ENT\u QUOTES)
你能帮我找到解决问题的方法吗?
任何帮助都将被告知。
谢谢。正如Magnus Eriksson已经指出的那样-无论如何,我都会使用本机内置的PHP实现 在您的例子中,它看起来非常简单(我将您的函数包装在一个名为
Book
的类中)
服务器
客户端
有关更多信息,请查看官方文档
当PHP5出现时,他们还实现了Soap服务器和客户端的本机实现。它们可能更稳定,并且具有更好的性能(以及适当的文档)。如果由于某种原因无法安装本机soap扩展,请将NuSoap视为一种后备方案。NuSoap有点“过时”。最好使用本机SOAP扩展或zend SOAP库。大多数来自谷歌的“如何”点击都是基于nusoap的,所以我尝试了这个。我将测试提供的解决方案。
HTTP/1.1 500 Internal Server Error
Server: nginx
Date: Sun, 26 Aug 2018 07:49:26 GMT
Content-Type: text/xml; charset=utf-8
Content-Length: 684
Connection: keep-alive
X-SOAP-Server: NuSOAP/0.9.5 (1.123)
<?xml version="1.0" encoding="utf-8"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
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/">
<SOAP-ENV:Body><SOAP-ENV:Fault><faultcode xsi:type="xsd:string">SOAP-ENV:Client</faultcode><faultactor xsi:type="xsd:string"></faultactor><faultstring xsi:type="xsd:string">error in msg parsing:
xml was empty, didn't parse!</faultstring><detail xsi:type="xsd:string"></detail></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>
try
{
$objSoapServer = new SoapServer(null, ['uri' => 'http://localhost/soap/server.php']);
$objSoapServer->setClass('Book');
$objSoapServer->handle();
}
catch(SoapFault $e)
{
echo $e->getMessage();
}
class Book
{
public function getBooks($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";
}
}
}
$client = new SoapClient(null, [
'location' => "http://localhost/soap/server.php",
'uri' => "http://localhost/soap/server.php",
'trace' => 1 ]
);
echo $client->__soapCall("getBooks",["books"]);