如何在PHP Nusoap中解决“在第1行解析SOAP负载的XML错误:无效的文档结束”

时间:2015-11-26 08:04:07

标签: php xml soap nusoap

我刚接触PHP Nusoap,我试图运行我的程序,当我print_r($client)时,我遇到了这种错误。这是错误:

  

错误:在第1行解析SOAP有效内容时出现XML错误:文档结束无效

我在google中发现一些相关的主题,文章和问题,但是如果有答案则无法帮助或解决我的问题 我还打印了我的请求并在此处回复了详细信息:

**Request**

POST /Nusoap/webservice_server.php HTTP/1.0
Host: sampleserver:8544
User-Agent: NuSOAP/0.9.5 (1.123)
Content-Type: text/xml; charset=ISO-8859-1
SOAPAction: "urn:Insertwsdl#InsertData"
Content-Length: 616

<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope 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:Insert"><SOAP-ENV:Body><tns:InsertData xmlns:tns="urn:Insert"><data/><data1/><data2/><data3/><data4/></tns:InsertData></SOAP-ENV:Body></SOAP-ENV:Envelope>

Response

HTTP/1.1 200 OK
Date: Thu, 26 Nov 2015 06:17:25 GMT
Server: Apache/2.2.14 (Unix) PHP/5.2.11
X-Powered-By: PHP/5.4.44
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
X-SOAP-Server: NuSOAP/0.9.5 (1.123)
Set-Cookie: PHPSESSID=rftjeluijhnej49rvpu94rs6f0; path=/
Content-Length: 411
Connection: close
Content-Type: text/xml; charset=ISO-8859-1

Data : Filename of of data
Data1 : Sender of the data
Data2 : Receiver of the data
Data : Filename1 of the data
Data1 : Sender1 of the data
Data2 : Receiver1 of the data
Da

代码:

webservice_server.php

<?php 
require_once('lib/nusoap.php');

$server = new soap_server();

$server->configureWSDL('Database Data Insertion', 'urn:Insert');
//$server->soap_defencoding = 'utf-8';
$server->register('InsertData',
    array(
        'data' => 'xsd:data',       
        'data1' => 'xsd:data1',
        'data2'  => 'xsd:data2',
        'data3'  => 'xsd:data3',
        'data4' => 'xsd:data4',
    ),
    array(
        'return' => 'xsd:string'        
    ),
    'urn:Insert',
    'urn:Insertwsdl#InsertData',
    'rpc',
    'literal',
    'Retrieve data from  the database'
    );

    function InsertData($data, $data1, $data2, $data3, $data4){

        $db_host = '127.0.0.1';
        $db_username = 'sample';
        $db_password = '' ;
        $db_name = 'sample';

        $conn = new mysqli($db_host, $db_username, $db_password, $db_name);

        if ($conn->connect_error) {
            trigger_error('Database connection failed: ', $conn->connect_error, E_USER_ERROR);
        }

        $sql = "SELECT * FROM sample_transaction ";

        $query = $conn->query($sql);

        if ($query->num_rows > 0) {
            while ($row = $query->fetch_assoc()) {
                echo "<br /> Data : " .$row['data'];
                echo "<br /> Data1 : " .$row['data1'];
                echo "<br /> Data2 : "  .$row['data2'];
            }

        } else {
            echo "0 Results";
        }
    }

    $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA)? $HTTP_RAW_POST_DATA : '' ;
    $server->service($HTTP_RAW_POST_DATA);
?>

webservice_client.php

<?php
require_once('lib/nusoap.php');

$data = json_decode(file_get_contents("php://input"), true);


$data = $data['data'];
$data1 = $data['data1'];
$data2 = $data['data2'];
$data3= $data['data3'];
$data4 = $data['data4'];
$client = new nusoap_client('http://sampleserver:8544/Nusoap/webservice_server.php?wsdl', true);

$datas = array(
     'data' => $data,
    'data1' => $data1,
    'data2'=> $data2,
    'data3' => $data3,
    'data4' => $data4,  
    );

$result= $client->call('InsertData', $datas);

print_r($result);

echo "<br /><pre>" .$client. "</pre>";
echo "<h2>Request</h2>";
echo "<pre>" .htmlspecialchars($client->request, ENT_QUOTES). "</pre>";
echo "<h2>Response</h2>";
echo "<pre>" .htmlspecialchars($client->response, ENT_QUOTES). "</pre>";
?>

我只是评论$server->soap_defencoding,因为如果没有注释,响应将会有text/xml charset=utf-8之类的内容类型。如果我没有相同的内容类型,我的意思是字符集,这也是一个问题吗?根据我上面发布的代码,我如何解决这个问题,因为我一直在寻找2天的解决方案。有人可以指出我哪里出错了,我该如何解决这个问题?顺便说一句,代码只会从数据库中选择,它只从数据库中检索少量数据并得到该错误。 任何帮助表示赞赏。非常感谢。

3 个答案:

答案 0 :(得分:1)

请保持

$server->soap_defencoding = 'utf-8';

添加

$client->soap_defencoding = 'utf-8'; 
$client->encode_utf8 = false;
$client->decode_utf8 = false;

如果错误仍然存​​在,请添加

$server->encode_utf8 = false;
$server->decode_utf8 = false;

到服务器文件

答案 1 :(得分:1)

我在通过https发布xml时遇到此错误。通过http传递它是好的。经过几天的谷歌搜索并试图改变我的php.ini上的一些东西,我弄乱了整个文件,所以我选择重新安装php5,它工作。

我使用以下命令重新安装php

apt-get install libapache2-mod-php5

答案 2 :(得分:0)

我有同样的错误。我试着解决它一天。不幸的是,我没有解决方案。在追踪我的php代码之后,我在config.php文件中看到了CREATE PROC MyCriticalWork(@MyParam INT) AS DECLARE @LockRequestResult INT SET @LockRequestResult=0 DECLARE @MyTimeoutMiliseconds INT SET @MyTimeoutMiliseconds=5000--Wait only five seconds max then timeouit BEGIN TRAN EXEC @LockRequestResult=SP_GETAPPLOCK 'MyCriticalWork','Exclusive','Transaction',@MyTimeoutMiliseconds IF(@LockRequestResult>=0)BEGIN /* DO YOUR CRITICAL READS AND WRITES HERE */ --Release the lock COMMIT TRAN END ELSE ROLLBACK TRAN 。如果您只删除该行或将其更改为ini_set('display_errors', 1);,它将像魅力一样工作。

我希望这有助于其他人在他们的SOAP(nusoap)项目库或文件中克服这个问题。

度过美好的一天。