我正在从事一个项目,我需要从网络服务中获取一些数据,并需要使用 PHP 在屏幕上显示这些数据。
当我在 soapUI 中执行网络服务时,我得到了这个响应:
<soapenv:Envelope xmlns:ns="http://schemas.test.nl/are/planning/business/1.0" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<header xmlns="http://schemas.cordys.com/General/1.0/">
<msg-id>00505600-0646-11e5-fcef-ea885aa59f6e</msg-id>
</header>
</soapenv:Header>
<soapenv:Body>
<tonenbeschikbaarheidportaalresponse xmlns="http://schemas.test.nl/are/planning/business/1.0">
<tonenbeschikbaarheidportaalresult>
<beschikbaarheidslijst starttime="06:00:00.0">
<day daycode="sun">
<available>0</available>
<starttimedate>2016-03-06T05:00:00.0</starttimedate>
</day>
<day daycode="mon">
<available>0</available>
<starttimedate>2016-03-07T05:00:00.0</starttimedate>
</day>
<day daycode="tue">
<available>0</available>
<starttimedate>2016-03-08T05:00:00.0</starttimedate>
</day>
<day daycode="wed">
<available>0</available>
<starttimedate>2016-03-09T05:00:00.0</starttimedate>
</day>
<day daycode="thu">
<available>0</available>
<starttimedate>2016-03-10T05:00:00.0</starttimedate>
</day>
<day daycode="fri">
<available>0</available>
<starttimedate>2016-03-11T05:00:00.0</starttimedate>
</day>
<day daycode="sat">
<available>1</available>
<starttimedate>2016-03-12T05:00:00.0</starttimedate>
</day>
</beschikbaarheidslijst>
<beschikbaarheidslijst starttime="07:00:00.0">
<day daycode="sun">
<available>0</available>
<starttimedate>2016-03-06T06:00:00.0</starttimedate>
</day>
<day daycode="mon">
<available>0</available>
<starttimedate>2016-03-07T06:00:00.0</starttimedate>
</day>
<day daycode="tue">
<available>0</available>
<starttimedate>2016-03-08T06:00:00.0</starttimedate>
</day>
<day daycode="wed">
<available>0</available>
<starttimedate>2016-03-09T06:00:00.0</starttimedate>
</day>
<day daycode="thu">
<available>0</available>
<starttimedate>2016-03-10T06:00:00.0</starttimedate>
</day>
<day daycode="fri">
<available>0</available>
<starttimedate>2016-03-11T06:00:00.0</starttimedate>
</day>
<day daycode="sat">
<available>1</available>
<starttimedate>2016-03-12T06:00:00.0</starttimedate>
</day>
</beschikbaarheidslijst>
</tonenbeschikbaarheidportaalresult>
</tonenbeschikbaarheidportaalresponse>
</soapenv:Body>
</soapenv:Envelope>
我需要的是,我可以遍历每个 beschikbaarheidslijst(荷兰语的可用性列表)。
问题是我的代码中的响应几乎是空的(或不可见的): 我检查了其他系统中的 Web 服务是否给出了带有值的响应,并且确实如此。
$beschikbaarheid = $response[1];
var_dump($beschikbaarheid);
输出: 对象(stdClass)#51(1){ [“tonenbeschikbaarheidportaalresult”]=> 对象(stdClass)#52(0){ }
对于 print_r,其他对象也是不可见的:
$beschikbaarheid = $response[1];
print_r($beschikbaarheid);
输出: 标准类对象 ( [tonenbeschikbaarheidportaalresult] => stdClass 对象 ( ) )
我想遍历 $beschikbaarheid 响应,但代码没有到达那里。 $item 为 NULL。
foreach($beschikbaarheid->tonenbeschikbaarheidportaalresult->children('http://schemas.test.nl/are/planning/business/1.0')->beschikbaarheidslijst as $item)
{
echo '<pre>';
var_dump(get_object_vars($item)); // gives NULL
echo '</pre>';
$data[] = get_object_vars($item);
}
当我删除 children(' http://schemas.test.nl/are/planning/business/1.0 ')-> 时,我会收到通知,因此 namespace 必须正确:
Notice: Undefined property: stdClass::$beschikbaarheidslijst in D:\xampp\htdocs\prive\CCP\includes\inc_formulieren.php on line 187
你能告诉我如何遍历 beschikbaarheidslijst 元素吗? 我必须对属性 starttime="06:00:00.0"做些什么吗?
<beschikbaarheidslijst starttime="06:00:00.0">
我从 web 服务获得 soap 响应。
$response = call_service( $serviceOperation, $request, $entiteit, $backend );
echo '<pre> response from webservice ';
var_dump($response);
echo '</pre>';
response from webservice array(2) {
[0]=>
int(0)
[1]=>
object(stdClass)#51 (1) {
["tonenbeschikbaarheidportaalresult"]=>
object(stdClass)#52 (0) {
}
}
}
使用 SoapClient 和扩展调用服务:
class SoapClientNG extends \SoapClient
{
public function __doRequest($req, $location, $action, $version = SOAP_1_1)
{
$xml = explode("\r\n", parent::__doRequest($req, $location, $action, $version));
$response = preg_replace( '/^(\x00\x00\xFE\xFF|\xFF\xFE\x00\x00|\xFE\xFF|\xFF\xFE|\xEF\xBB\xBF)/', "", $xml[5] );
return $response;
}
}
function call_service( $serviceOperation, $request, $entiteit, $backend )
{
$error = 0;
$soapClient = getSoapClient($entiteit, $backend);
try {
$response = $soapClient->__call($serviceOperation, array($request));
}
catch (SoapFault $fault) {
$error = 1;
$response = $fault->faultcode."-".$fault->faultstring;
}
return array($error, $response);
}
解决方案结合文森特的回答:
我看到我的响应是一个对象,但是当我打印 __getLastResponse() 时,我得到了 XML。 http://php.net/manual/en/soapclient.getlastresponse.php
$response = $soapClient->__getLastResponse();
当我在 Vincent 的回答中使用它时,我在屏幕上看到了值:
$xml = simplexml_load_string($response);
$xml->registerXPathNamespace('list', 'http://schemas.test.nl/are/planning/business/1.0');
foreach ($xml->xpath('//list:beschikbaarheidslijst') as $list_object)
{
print_r($list_object);
}
最佳答案
您的代码中缺少命名空间声明,因此它不起作用,应该首先声明此命名空间 http://schemas.test.nl/are/planning/business/1.0
并且然后你可以加载 XML 字符串,执行此操作..
$xml_str = "YOUR SOAP RESPONSE HERE";
$xml = simplexml_load_string($xml_str);
$xml->registerXPathNamespace('list', 'http://schemas.test.nl/are/planning/business/1.0');
foreach ($xml->xpath('//list:beschikbaarheidslijst') as $list_object)
{
print_r($list_object);
}
这应该可以解决问题。 fiddle :https://eval.in/534908
关于php - 只有响应的第一个元素可见 - php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35940493/