在PHP中从SOAP响应中提取值

在PHP中从SOAP响应中提取值,php,soap,Php,Soap,我需要从这个SOAP响应中获取一个值。该值位于loginresponse/return元素中。以下是回应: <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="

我需要从这个SOAP响应中获取一个值。该值位于loginresponse/return元素中。以下是回应:

<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/">
<soap-env:body soap-env:encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="urn:DBCentralIntf-IDBCentral">
    <ns1:loginresponse>
        <return xsi:type="xsd:string"><**THIS IS THE VALUE I NEED**></return>
    </ns1:loginresponse>
</soap-env:body>
$response = curl_exec($ch);
curl_close($ch);

$cleanxml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $response);
$cleanxml = str_ireplace('NS1:','', $cleanxml);
$xml = simplexml_load_string($cleanxml);

echo $xml->Body->LoginResponse->return[0];
但是我得到一个空的物体

$response = curl_exec($ch);
curl_close($ch);

$cleanxml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $response);
$cleanxml = str_ireplace('NS1:','', $cleanxml);
$xml = simplexml_load_string($cleanxml);

echo $xml->Body->LoginResponse->return[0];
谢谢你的帮助

更新:
$response = curl_exec($ch);
curl_close($ch);

$cleanxml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $response);
$cleanxml = str_ireplace('NS1:','', $cleanxml);
$xml = simplexml_load_string($cleanxml);

echo $xml->Body->LoginResponse->return[0];
删除名称空间会让事情变得更清楚一些(尽管这是一种黑客行为)。这是我的新代码:

$response = curl_exec($ch);
curl_close($ch);

$cleanxml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $response);
$cleanxml = str_ireplace('NS1:','', $cleanxml);
$xml = simplexml_load_string($cleanxml);

echo $xml->Body->LoginResponse->return[0];
更新: 删除名称空间会让事情变得更清楚一些(尽管这是一种黑客行为)。这是我的新代码:

$response = curl_exec($ch);
curl_close($ch);

$cleanxml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $response);
$cleanxml = str_ireplace('NS1:','', $cleanxml);
$xml = simplexml_load_string($cleanxml);

echo $xml->Body->LoginResponse->return[0];

<>而不是使用CURL并试图解析XML响应,考虑使用PHP SOAP客户端。(我在Windows上使用PHP,所以我只需在PHP.ini中取消注释
extension=PHP\u soap.dll

$response = curl_exec($ch);
curl_close($ch);

$cleanxml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $response);
$cleanxml = str_ireplace('NS1:','', $cleanxml);
$xml = simplexml_load_string($cleanxml);

echo $xml->Body->LoginResponse->return[0];
如果安装了SOAP,则可以从正在使用的web服务的提供者处获取WSDL。通过在您显示的XML中搜索该值:
xmlns:ns1=“urn:DBCentralIntf IDBCentral”
,我猜您可以找到它,但您可能会更幸运地找到它,因为您确实知道您正在使用的web服务

$response = curl_exec($ch);
curl_close($ch);

$cleanxml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $response);
$cleanxml = str_ireplace('NS1:','', $cleanxml);
$xml = simplexml_load_string($cleanxml);

echo $xml->Body->LoginResponse->return[0];
拥有WSDL后,使用PHP SOAP客户端非常简单:

$response = curl_exec($ch);
curl_close($ch);

$cleanxml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $response);
$cleanxml = str_ireplace('NS1:','', $cleanxml);
$xml = simplexml_load_string($cleanxml);

echo $xml->Body->LoginResponse->return[0];
$client = new SoapClient('path/to/your.wsdl');
$response = $client->Login(['username', 'password']);
$theValueYouNeed = $response->loginresponse->return;

<>而不是使用CURL并试图解析XML响应,考虑使用PHP SOAP客户端。(我在Windows上使用PHP,所以我只需在PHP.ini中取消注释
extension=PHP\u soap.dll

$response = curl_exec($ch);
curl_close($ch);

$cleanxml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $response);
$cleanxml = str_ireplace('NS1:','', $cleanxml);
$xml = simplexml_load_string($cleanxml);

echo $xml->Body->LoginResponse->return[0];
如果安装了SOAP,则可以从正在使用的web服务的提供者处获取WSDL。通过在您显示的XML中搜索该值:
xmlns:ns1=“urn:DBCentralIntf IDBCentral”
,我猜您可以找到它,但您可能会更幸运地找到它,因为您确实知道您正在使用的web服务

$response = curl_exec($ch);
curl_close($ch);

$cleanxml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $response);
$cleanxml = str_ireplace('NS1:','', $cleanxml);
$xml = simplexml_load_string($cleanxml);

echo $xml->Body->LoginResponse->return[0];
拥有WSDL后,使用PHP SOAP客户端非常简单:

$response = curl_exec($ch);
curl_close($ch);

$cleanxml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $response);
$cleanxml = str_ireplace('NS1:','', $cleanxml);
$xml = simplexml_load_string($cleanxml);

echo $xml->Body->LoginResponse->return[0];
$client = new SoapClient('path/to/your.wsdl');
$response = $client->Login(['username', 'password']);
$theValueYouNeed = $response->loginresponse->return;

所以你一定试过什么,让我们看看,这样我们就不会认为这是一个为我做的问题,你是怎么得到这个回答的?您正在使用PHP SOAP客户端吗?您的问题在哪里?您是否使用了本机SoapClient类,该类应至少返回一个stdClass对象,该对象应包含您要查找的值?太多,无法全部列出@RiggsFolly我在编辑中发布的是我的最新版本,我相信它帮助我遍历到了正确的子元素。@Don'tPanic使用Curl,因此您一定尝试过什么,让我们看看,这样我们就不会认为这是一个“为我做”的问题,你是如何得到这样的回答的?您正在使用PHP SOAP客户端吗?您的问题在哪里?您是否使用了本机SoapClient类,该类应至少返回一个stdClass对象,该对象应包含您要查找的值?太多,无法全部列出@RiggsFolly我在编辑中发布的是我的最新版本,我相信它帮助我遍历到正确的子元素。@Don't使用cURL