How can I solve "XML error parsing SOAP payload on line 1: Invalid document end" in PHP Nusoap

Errorhere picture Errorhere · Nov 26, 2015 · Viewed 9.8k times · Source

I am just new to PHP Nusoap and I tried to run my program and I am getting this kind of error when I print_r($client). Here is the error :

Error: XML error parsing SOAP payload on line 1: Invalid document end

I find some related topics, articles and questions in google they are unanswered and if there are answered it doesn't help or solve my problem I also print my request and reponse details here it is :

**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

for my code :

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>";
?>

I just comment out the $server->soap_defencoding because the response will have a content-type like text/xml charset=utf-8 if not comment out. Is it also a problem if I don't have the same content-type, the charset I mean? Base on the codes I posted above how can I solve this issue because I been looking for solution for this in 2 days. Can someone point out where did I go wrong and how can I solve this? By the way, the code will just select from the database, it retrieves only few data from the database and got that error. Any help is appreciated. Thanks a lot.

Answer

Jorge Carvalho picture Jorge Carvalho · Aug 23, 2016

Please keep

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

add

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

if error continues, please add

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

to the server file