Only first element of response visible - php

Viewed 282

I am working on a project where I need to get some data from webservices and need to show it on screen using PHP.

When I execute the webservice in soapUI I get this response:

<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>

What I need, is that I can loop through every beschikbaarheidslijst (availability list in Dutch).

The problem is that the response in my code is almost empty (or invisible): I checked if the webservice in the other system gave a response with values and it did.

$beschikbaarheid = $response[1];
var_dump($beschikbaarheid);

Output: object(stdClass)#51 (1) { ["tonenbeschikbaarheidportaalresult"]=> object(stdClass)#52 (0) { } }

Also with print_r the others objects are invisible:

$beschikbaarheid = $response[1];
print_r($beschikbaarheid);

Output: stdClass Object ( [tonenbeschikbaarheidportaalresult] => stdClass Object ( ) )

I want to loop through the response $beschikbaarheid but the code doesn't get there. $item is 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);
}

When I remove children('http://schemas.test.nl/are/planning/business/1.0')-> then I get a notice, so the namespace must be correct:

Notice: Undefined property: stdClass::$beschikbaarheidslijst in D:\xampp\htdocs\prive\CCP\includes\inc_formulieren.php on line 187

Can you tell me how I can loop through the beschikbaarheidslijst elements? Do I have to do something with the attribute starttime="06:00:00.0"?

<beschikbaarheidslijst starttime="06:00:00.0">

I get the soap response from the webservice.

$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) {
    }
  }
}

Call service with use of SoapClient and extension:

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);

}

Solution in combination with answer of Vincent:

I saw that my response was an object, but when I printed __getLastResponse() I got XML. http://php.net/manual/en/soapclient.getlastresponse.php

$response = $soapClient->__getLastResponse();

When I use this in the answer of Vincent I see values on my screen:

$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);
}
1 Answers
Related