You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PHP创建nuSOAP服务器时遇XML错误:文档末尾有额外内容

Fixing "Extra content at the end of the document" Error in nuSOAP PHP Server

Hey there, that "extra content at the end of the document" error is a classic XML parsing issue—your nuSOAP server is sending valid SOAP XML mixed with extra stuff (like PHP errors, whitespace, or stray output) that breaks the client's ability to parse the response. Let's walk through the most likely fixes based on your code snippet:

1. Eliminate accidental pre-output

  • Double-check your conn.php file and main script: ensure there’s no whitespace, HTML, or echo statements before the opening <?php tag. Even a single newline or space here will get sent before the SOAP XML, triggering the error.
  • If your files use UTF-8 encoding, make sure they don’t have a hidden BOM (byte order mark) at the start—this is a common hidden culprit.

2. Complete your incomplete PDO fetch call

Your code cuts off at $stmt->fetch(PDO::FETCH_...—this incomplete line will throw a PHP parse error, which gets printed directly to the response before the SOAP XML. Finish the fetch call with a valid PDO fetch mode, like:

$data = $stmt->fetch(PDO::FETCH_ASSOC); // Returns an associative array of the row

3. Suppress PHP error output (but log them for debugging)

PHP warnings or notices (like undefined variables, PDO connection issues) will leak into the SOAP response. Add these lines at the top of your script to hide them from the client while still logging them for you to debug:

error_reporting(E_ALL);
ini_set('display_errors', 0);
ini_set('log_errors', 1);

4. Ensure proper nuSOAP server setup

Make sure you’re initializing the server, registering your function, and handling the request correctly without extra output. Here’s a polished, complete version of your code to reference:

<?php
include 'conn.php';
require 'lib/nusoap.php';

// Initialize nuSOAP server and WSDL
$server = new soap_server();
$server->configureWSDL('EmployeeDataService', 'urn:EmployeeDataService');

// Register your fetch function with type hints
$server->register(
    'fetchDataEmp',
    array('id' => 'xsd:int'),
    array('return' => 'xsd:struct'), // Match the data type you're returning
    'urn:EmployeeDataService',
    'urn:EmployeeDataService#fetchDataEmp',
    'rpc',
    'encoded',
    'Fetches employee data by ID'
);

function fetchDataEmp($id){
    global $conn;
    $getData = "SELECT * FROM user WHERE id = :id";
    $stmt = $conn->prepare($getData);
    $stmt->bindParam(':id', $id, PDO::PARAM_INT); // Explicit parameter type for safety
    $stmt->execute();
    $data = $stmt->fetch(PDO::FETCH_ASSOC); // Complete fetch call
    
    // Return empty array if no data found to avoid null issues
    return $data ?: [];
}

// Handle incoming SOAP request
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);

// Optional: Omit the closing ?> tag to prevent accidental whitespace after it

5. Remove post-service output

Make sure there’s nothing after the $server->service(...) call—no closing ?> tag (it’s optional in PHP and avoids accidental trailing whitespace), no echo statements, no debug dumps.

If you’re still stuck, try accessing your server’s WSDL URL directly in a browser. Any PHP errors, warnings, or stray text above the XML output is exactly what’s causing the client’s parsing error.

内容的提问来源于stack exchange,提问作者Khaled Gritli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:15:39