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

如何在PHP中从含WSDL的SOAP请求字符串提取信息?

Extracting Data from a SOAP Request in PHP

Hey there! Extracting specific values from your SOAP request string is straightforward once you work with PHP’s XML parsing tools. Here are two reliable methods tailored to your scenario:

Method 1: Using SimpleXML (Most Straightforward)

SimpleXML is perfect for basic to moderate XML parsing needs. Since your SOAP request uses namespaces (like wsim), you’ll need to register them to access the elements correctly.

// Your existing code to get the SOAP request
$data = file_get_contents('php://input');

// Load the SOAP XML into a SimpleXML object
$xml = simplexml_load_string($data);

// Critical: Register the "wsim" namespace with its actual URI (find this in your full SOAP envelope's xmlns:wsim attribute)
// Example: If your SOAP Envelope has xmlns:wsim="http://your-service-namespace.com", use that URI here
$xml->registerXPathNamespace('wsim', 'http://your-service-namespace.com');

// Extract authentication credentials from the SOAP Header
$authHeader = $xml->xpath('//wsim:WSEAuthenticateHeader')[0];
$username = (string)$authHeader->Usser; // Note: Your XML uses "Usser" (not User) — match the tag exactly!
$password = (string)$authHeader->Password;

// Extract items from the postScormRequest in the SOAP Body
$scormRequest = $xml->xpath('//wsim:postScormRequest')[0];
$item1 = (string)$scormRequest->item1;
$item2 = (string)$scormRequest->item2;
$item3 = (string)$scormRequest->item3;

// Use the extracted data as needed
var_dump($username, $password, $item1, $item2, $item3);

Method 2: Using DOMDocument (For More Complex XML)

If you need advanced control over XML nodes (like handling attributes or nested structures), DOMDocument paired with DOMXPath is a solid choice:

$data = file_get_contents('php://input');

$dom = new DOMDocument();
$dom->loadXML($data);

// Set up XPath with the registered namespace
$xpath = new DOMXPath($dom);
$xpath->registerNamespace('wsim', 'http://your-service-namespace.com');

// Fetch each value with error checking (in case nodes are missing)
$usernameNode = $xpath->query('//wsim:WSEAuthenticateHeader/wsim:Usser')->item(0);
$username = $usernameNode ? $usernameNode->nodeValue : '';

$passwordNode = $xpath->query('//wsim:WSEAuthenticateHeader/wsim:Password')->item(0);
$password = $passwordNode ? $passwordNode->nodeValue : '';

$item1Node = $xpath->query('//wsim:postScormRequest/wsim:item1')->item(0);
$item1 = $item1Node ? $item1Node->nodeValue : '';

// Repeat for item2 and item3 similarly

Key Tips to Avoid Headaches

  • Double-check the namespace URI: The wsim namespace’s URI must exactly match what’s in your full SOAP request (look for xmlns:wsim="..." in the root <soap:Envelope> tag).
  • Match tag names exactly: Your XML uses <wsim:Usser> (with two s’s) — don’t accidentally spell it User!
  • Ensure valid XML: Make sure the full SOAP request isn’t truncated (your example cuts off at </soap:...). Invalid XML will break parsing.
  • Handle sensitive data safely: Never log or echo passwords in production — treat authentication credentials with care.

内容的提问来源于stack exchange,提问作者Juan Medina

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 07:48:19