doulangbi6869 2018-08-26 07:57
浏览 59
已采纳

无法在php中创建我的第一个SOAP服务

I am trying to build my first SOAP web server but I failed. I tried several tutorials but none of them works.Below is what i have tried so far:

Server script:

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);

Client script:

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>';

When I call my page https://mydomain/soap_client.php I get

Fault

Array
(
    [faultcode] => SOAP-ENV:Client
    [faultactor] => 
    [faultstring] => error in msg parsing:
xml was empty, didn't parse!
    [detail] => 
)

Request

POST /dss/soap_server.php HTTP/1.0
Host: mydomain
User-Agent: NuSOAP/0.9.5 (1.123)
Content-Type: text/xml; charset=utf-8
SOAPAction: ""
Content-Length: 507

<?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><ns9471:getProd xmlns:ns9471="http://tempuri.org"><category xsi:type="xsd:string">books</category></ns9471:getProd></SOAP-ENV:Body></SOAP-ENV:Envelope>

Response

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&apos;t parse!</faultstring><detail xsi:type="xsd:string"></detail></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>

I can also provide htmlspecialchars($client->debug_str, ENT_QUOTES) if needed.

could you please help me to find how to solve the issue? Any help will be appreaciated. Thanks.

  • 写回答

1条回答 默认 最新

  • douwei4370 2018-08-26 08:32
    关注

    As Magnus Eriksson already pointed out - i would use in any case the native built in PHP Implementation

    In your case it looks pretty simple (i wrapped your function in a class called Book)

    Server

    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

    $client = new SoapClient(null, [
        'location' => "http://localhost/soap/server.php",
        'uri'      => "http://localhost/soap/server.php",
        'trace'    => 1 ]
    );
    
    echo $client->__soapCall("getBooks",["books"]);
    

    For more information take a look at the official documention

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名