PHP Web服务报错:Uncaught SoapFault异常[Client]未获取XML文档求助
Hey there! Sorry you're hitting this SOAP error—it's super common when you're just getting started with SOAP services in PHP, so let's break down what's going on and how to fix it.
The "looks like we got no XML document" error means your SOAP server isn't receiving valid XML input when the client sends the order request. Based on your code snippet, here are the most likely fixes:
1. You're using form-encoded data instead of SOAP XML
Your current code tries to read $_POST['mname'], $_POST['qty'], etc.—but SOAP doesn't send data as regular form fields. It transmits a full XML document in the raw POST body.
To fix this, use PHP's built-in SoapServer class to handle the request properly:
// Initialize SOAP server (use your service URI here) $server = new SoapServer(null, ['uri' => 'http://your-service-domain/order-service']); // Register a function to process orders function placeOrder($MealName, $Quantity, $CustName, $SpecialInstructions) { include("connect.php"); // Sanitize inputs and insert into database (use prepared statements for security!) $stmt = $conn->prepare("INSERT INTO orders (meal_name, quantity, cust_name, special_instructions) VALUES (?, ?, ?, ?)"); $stmt->bind_param("siss", $MealName, $Quantity, $CustName, $SpecialInstructions); $stmt->execute(); // Return a SOAP-compatible response return ['success' => true, 'orderId' => $stmt->insert_id]; } $server->addFunction("placeOrder"); $server->handle(); // This automatically reads and parses the raw XML input
On the client side (the part that sends the order), use SoapClient to send a proper SOAP request:
$client = new SoapClient(null, [ 'location' => 'http://your-service-domain/order-endpoint.php', 'uri' => 'http://your-service-domain/order-service' ]); $result = $client->placeOrder("Cheeseburger", 2, "Buffy Summers", "Extra pickles");
2. Check the Content-Type header
If you're testing with tools like Postman or custom client code, make sure you send the correct Content-Type header:
- For SOAP 1.1:
Content-Type: text/xml - For SOAP 1.2:
Content-Type: application/soap+xml
Without this header, the server won't recognize the input as SOAP XML.
3. Fix malformed XML in the request
Even if you're sending XML, small syntax errors (unclosed tags, missing namespaces, unescaped special characters like <, >, &) will trigger this error. A valid SOAP request body looks like this:
<?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:uri="http://your-service-domain/order-service"> <soapenv:Header/> <soapenv:Body> <uri:placeOrder> <MealName>Cheeseburger</MealName> <Quantity>2</Quantity> <CustName>Buffy Summers</CustName> <SpecialInstructions>Extra pickles</SpecialInstructions> </uri:placeOrder> </soapenv:Body> </soapenv:Envelope>
4. Read raw input manually (if you can't use SoapServer)
If you need to handle XML parsing yourself, read the raw POST data instead of relying on $_POST:
$rawXml = file_get_contents('php://input'); $xml = simplexml_load_string($rawXml); // Extract values from the XML structure $MealName = trim($xml->Body->placeOrder->MealName); $Quantity = trim($xml->Body->placeOrder->Quantity); // ... repeat for other fields
Since you're new to PHP web services, note that REST is more widely used these days—but if you're sticking with SOAP, using SoapServer and SoapClient will handle most of the XML heavy lifting for you, reducing errors like this.
内容的提问来源于stack exchange,提问作者buffyBTVS96

