如何在php中显示来自SOAP Response的firstName
,lastName
和targetName
。我已经尝试将echo $response;
更改为:
echo $response->FindPersonsResponse->return->matchingPersons->person->targetName;
或者:
echo $response->getElementsByTagName('firstName')->length;
或者:
echo $response->getElementsByTagName('firstName')->item(0)->nodeValue;
但没有一个正在发挥作用。
我是SOAP或REST的新手。 谢谢你的帮助。
这是我的SOAP请求:
<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://xxx.xxxxxxxxxxx.xxx/webservices/schema#5.5.47">
<x:Header/>
<x:Body>
<sch:FindPersons>
<sch:user>soaptest</sch:user>
<sch:password>soaptest</sch:password>
<sch:clientTimestamp></sch:clientTimestamp>
<sch:clientIP></sch:clientIP>
<sch:clientOSUser></sch:clientOSUser>
<sch:company></sch:company>
<sch:searchParameters>
<sch:targetName>sandieax</sch:targetName>
</sch:searchParameters>
</sch:FindPersons>
</x:Body>
这是我的SOAP响应:
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:FindPersonsResponse xmlns:ns="http://xxx.xxxxxxxxxxx.xxx/webservices/schema#5.5.47">
<ns:return type="com.invoqsystems.apex.web.soap.FindPersonsReturn">
<ns:matchingPersons>
<ns:person>
<ns:firstName>Aris</ns:firstName>
<ns:lastName>Sandiego</ns:lastName>
<ns:targetName>sandieax</ns:targetName>
</ns:person>
</ns:matchingPersons>
<ns:serverTimestamp>16/12/2015 11:51:42 AM</ns:serverTimestamp>
<ns:status>OK</ns:status>
</ns:return>
</ns:FindPersonsResponse>
</soapenv:Body>
</soapenv:Envelope>
这是我的php文件:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://xxx.xxxxxxxxxxx.xxx/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "<x:Envelope xmlns:x=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:sch=\"http://xxx.xxxxxxxxxxx.xxx/",/webservices/schema#5.5.47\">\n <x:Header/>\n <x:Body>\n <sch:FindPersons>\n <sch:user>soaptest</sch:user>\n <sch:password>soaptest</sch:password>\n <sch:clientTimestamp></sch:clientTimestamp>\n <sch:clientIP></sch:clientIP>\n <sch:clientOSUser></sch:clientOSUser>\n <sch:company></sch:company>\n <sch:searchParameters>\n <sch:targetName>sandieax</sch:targetName>\n </sch:searchParameters>\n </sch:FindPersons>\n </x:Body>\n</x:Envelope>",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: text/xml"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
编辑:
好的,所以我终于得到它来捕获firstname,lastname或targetname。 下一个问题是如何显示所有结果而不仅仅是第一个结果?
<?php
$data = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:sch=\"http://www.xxxxxxxx.com/webservices/schema#5.5.47\">\r\n <soapenv:Header/>\r\n <soapenv:Body>\r\n <sch:FindPersons>\r\n <sch:user>soaptest</sch:user>\r\n <sch:password>soaptest</sch:password>\r\n <sch:clientTimestamp/>\r\n <sch:clientIP/>\r\n <sch:clientOSUser/>\r\n <sch:company/>\r\n <sch:searchParameters>\r\n <sch:targetName></sch:targetName>\r\n </sch:searchParameters>\r\n </sch:FindPersons>\r\n </soapenv:Body>\r\n</soapenv:Envelope>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://wsdlurl");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml; charset=utf-8", "Content-Length: " . strlen($data)));
$output = curl_exec($ch);
curl_close($ch);
$response = DOMDocument::loadXml($output);
$value = $response->getElementsByTagName('targetName')->item(0)->textContent;
echo $value;
答案 0 :(得分:0)
这已经解决了。
$response = DOMDocument::loadXml($output);
$x = 0;
do {
$targetName = $response->getElementsByTagName('targetName')->item($x)->textContent;
$firstName = $response->getElementsByTagName('firstName')->item($x)->textContent;
$lastName = $response->getElementsByTagName('lastName')->item($x)->textContent;
$result = count($targetName);
echo "[" . $targetName . "] -- " . $firstName . " " . $lastName . "<br>";
$x++;
} while ($result != 0);