We help IT Professionals succeed at work.

soapclient result all in one object

columbusace
columbusace asked
on
I just recently began accessing webservices (wsdl) through php and soap.

in the first code snipped you can see how I make a new soapclient and acces the GetJobOpenings service in order  to get all the jobopenings from the company named 'MySolution FLEX' and where the country is 'Nederland'

the result from the print_r is the following:

stdClass Object ( [GetJobOpeningsResult] => 7 2011-01-01T00:00:00+01:00 2013-01-01T00:00:00+01:00 MySolution FLEX UTRECHT U-A00033 Recruiter MySolution Nieuw Demonstratieteam Amsterdam West-Nederland Nederland Gewenste functie 0  36 2011-01-01T00:00:00+01:00 2011-12-30T00:00:00+01:00 MySolution FLEX UTRECHT U-Z00011 Petrochemie Arno de Haan Nieuwegein Nederland Gewenste functie 0  35 2011-07-10T00:00:00+02:00 2011-12-10T00:00:00+01:00 MySolution FLEX UTRECHT U-A00011 Monteur speciaal werk GTI Monteur Arno de Haan Schiedam Nederland Gewenste functie 0  )

So it does as promised and returns the jobopenings I wanted. However they are al in one and t he same object, making looping through them impossible. same goes for accessing seperate data like eg the function or territory.

If I look at the source code in my browser it shows the structure shown in the second snippet. Where I do get the idea that the jobopenings are stored in different sections.

So, how can I get them in an array or ..... so that I can loop through them, and use their values ?

Thnx in advance

Robert!


$client = new SoapClient("http://R01.ws.mysolutionflex.nl/JobOpeningservice.asmx?wsdl");
$result = $client->GetJobOpenings(array('features' => SOAP_SINGLE_ELEMENT_ARRAYS, 'company' => 'MySolution FLEX', 'responsibilitycenter' => '', 'function' => '', 'city' => '', 'territory' => '', 'country' => 'Nederland', 'keyword' => '' ));
print_r($result);

Open in new window

stdClass Object
(
    [GetJobOpeningsResult] => <NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="Code" type="xs:int" minOccurs="0" />
                <xs:element name="Start_x0020_Date" type="xs:dateTime" minOccurs="0" />
                <xs:element name="End_x0020_Date" type="xs:dateTime" minOccurs="0" />
                <xs:element name="Company" type="xs:string" minOccurs="0" />
                <xs:element name="Responsibility_x0020_Center" type="xs:string" minOccurs="0" />
                <xs:element name="Request_x0020_No_" type="xs:string" minOccurs="0" />
                <xs:element name="Description" type="xs:string" minOccurs="0" />
                <xs:element name="Function" type="xs:string" minOccurs="0" />
                <xs:element name="Co-Worker" type="xs:string" minOccurs="0" />
                <xs:element name="City" type="xs:string" minOccurs="0" />
                <xs:element name="Territory" type="xs:string" minOccurs="0" />
                <xs:element name="Country" type="xs:string" minOccurs="0" />
                <xs:element name="Characteristic" type="xs:string" minOccurs="0" />
                <xs:element name="Char_Value" type="xs:string" minOccurs="0" />
                <xs:element name="Char__x0020_Entry_x0020_No_" type="xs:int" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <Table>
    <Code>7</Code>
    <Start_x0020_Date>2011-01-01T00:00:00+01:00</Start_x0020_Date>
    <End_x0020_Date>2013-01-01T00:00:00+01:00</End_x0020_Date>
    <Company>MySolution FLEX</Company>
    <Responsibility_x0020_Center>UTRECHT</Responsibility_x0020_Center>
    <Request_x0020_No_>U-A00033</Request_x0020_No_>
    <Description>Recruiter MySolution Nieuw</Description>
    <Function>Demonstratieteam</Function>
    <Co-Worker />
    <City>Amsterdam</City>
    <Territory>West-Nederland</Territory>
    <Country>Nederland</Country>
    <Characteristic>Gewenste functie</Characteristic>
    <Char_Value />
    <Char__x0020_Entry_x0020_No_>0</Char__x0020_Entry_x0020_No_>
  </Table>
  <Table>
    <Code>36</Code>
    <Start_x0020_Date>2011-01-01T00:00:00+01:00</Start_x0020_Date>
    <End_x0020_Date>2011-12-30T00:00:00+01:00</End_x0020_Date>
    <Company>MySolution FLEX</Company>
    <Responsibility_x0020_Center>UTRECHT</Responsibility_x0020_Center>
    <Request_x0020_No_>U-Z00011</Request_x0020_No_>
    <Description>Petrochemie</Description>
    <Function />
    <Co-Worker>Arno de Haan</Co-Worker>
    <City>Nieuwegein</City>
    <Territory />
    <Country>Nederland</Country>
    <Characteristic>Gewenste functie</Characteristic>
    <Char_Value />
    <Char__x0020_Entry_x0020_No_>0</Char__x0020_Entry_x0020_No_>
  </Table>
  <Table>
    <Code>35</Code>
    <Start_x0020_Date>2011-07-10T00:00:00+02:00</Start_x0020_Date>
    <End_x0020_Date>2011-12-10T00:00:00+01:00</End_x0020_Date>
    <Company>MySolution FLEX</Company>
    <Responsibility_x0020_Center>UTRECHT</Responsibility_x0020_Center>
    <Request_x0020_No_>U-A00011</Request_x0020_No_>
    <Description>Monteur speciaal werk GTI</Description>
    <Function>Monteur</Function>
    <Co-Worker>Arno de Haan</Co-Worker>
    <City>Schiedam</City>
    <Territory />
    <Country>Nederland</Country>
    <Characteristic>Gewenste functie</Characteristic>
    <Char_Value />
    <Char__x0020_Entry_x0020_No_>0</Char__x0020_Entry_x0020_No_>
  </Table>
</NewDataSet>
)

Open in new window

Comment
Watch Question

Most Valuable Expert 2011
Top Expert 2016

Commented:
You might want to use var_dump() instead of print_r() since it gives a little more information about the data structures.
Most Valuable Expert 2011
Top Expert 2016

Commented:
See http://www.laprbass.com/RAY_temp_columbusace.php
Outputs:
7 U-A00033 Recruiter MySolution Nieuw
36 U-Z00011 Petrochemie
35 U-A00011 Monteur speciaal werk GTI

HTH, ~Ray
<?php // RAY_temp_columbusace.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA FROM THE POST AT EE, SHOWN IN HEREDOC FORMAT
$xml = <<<XML
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="Code" type="xs:int" minOccurs="0" />
                <xs:element name="Start_x0020_Date" type="xs:dateTime" minOccurs="0" />
                <xs:element name="End_x0020_Date" type="xs:dateTime" minOccurs="0" />
                <xs:element name="Company" type="xs:string" minOccurs="0" />
                <xs:element name="Responsibility_x0020_Center" type="xs:string" minOccurs="0" />
                <xs:element name="Request_x0020_No_" type="xs:string" minOccurs="0" />
                <xs:element name="Description" type="xs:string" minOccurs="0" />
                <xs:element name="Function" type="xs:string" minOccurs="0" />
                <xs:element name="Co-Worker" type="xs:string" minOccurs="0" />
                <xs:element name="City" type="xs:string" minOccurs="0" />
                <xs:element name="Territory" type="xs:string" minOccurs="0" />
                <xs:element name="Country" type="xs:string" minOccurs="0" />
                <xs:element name="Characteristic" type="xs:string" minOccurs="0" />
                <xs:element name="Char_Value" type="xs:string" minOccurs="0" />
                <xs:element name="Char__x0020_Entry_x0020_No_" type="xs:int" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <Table>
    <Code>7</Code>
    <Start_x0020_Date>2011-01-01T00:00:00+01:00</Start_x0020_Date>
    <End_x0020_Date>2013-01-01T00:00:00+01:00</End_x0020_Date>
    <Company>MySolution FLEX</Company>
    <Responsibility_x0020_Center>UTRECHT</Responsibility_x0020_Center>
    <Request_x0020_No_>U-A00033</Request_x0020_No_>
    <Description>Recruiter MySolution Nieuw</Description>
    <Function>Demonstratieteam</Function>
    <Co-Worker />
    <City>Amsterdam</City>
    <Territory>West-Nederland</Territory>
    <Country>Nederland</Country>
    <Characteristic>Gewenste functie</Characteristic>
    <Char_Value />
    <Char__x0020_Entry_x0020_No_>0</Char__x0020_Entry_x0020_No_>
  </Table>
  <Table>
    <Code>36</Code>
    <Start_x0020_Date>2011-01-01T00:00:00+01:00</Start_x0020_Date>
    <End_x0020_Date>2011-12-30T00:00:00+01:00</End_x0020_Date>
    <Company>MySolution FLEX</Company>
    <Responsibility_x0020_Center>UTRECHT</Responsibility_x0020_Center>
    <Request_x0020_No_>U-Z00011</Request_x0020_No_>
    <Description>Petrochemie</Description>
    <Function />
    <Co-Worker>Arno de Haan</Co-Worker>
    <City>Nieuwegein</City>
    <Territory />
    <Country>Nederland</Country>
    <Characteristic>Gewenste functie</Characteristic>
    <Char_Value />
    <Char__x0020_Entry_x0020_No_>0</Char__x0020_Entry_x0020_No_>
  </Table>
  <Table>
    <Code>35</Code>
    <Start_x0020_Date>2011-07-10T00:00:00+02:00</Start_x0020_Date>
    <End_x0020_Date>2011-12-10T00:00:00+01:00</End_x0020_Date>
    <Company>MySolution FLEX</Company>
    <Responsibility_x0020_Center>UTRECHT</Responsibility_x0020_Center>
    <Request_x0020_No_>U-A00011</Request_x0020_No_>
    <Description>Monteur speciaal werk GTI</Description>
    <Function>Monteur</Function>
    <Co-Worker>Arno de Haan</Co-Worker>
    <City>Schiedam</City>
    <Territory />
    <Country>Nederland</Country>
    <Characteristic>Gewenste functie</Characteristic>
    <Char_Value />
    <Char__x0020_Entry_x0020_No_>0</Char__x0020_Entry_x0020_No_>
  </Table>
</NewDataSet>
XML;

// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO VISUALIZE THE OBJECT
// var_dump($obj);

// USE AN ITERATOR TO ACCESS DATA INSIDE THE OBJECT
foreach ($obj->Table as $t)
{
    $c = (string)$t->Code;
    $r = (string)$t->Request_x0020_No_;
    $d = (string)$t->Description;

    // SHOW THE DATA WE FOUND
    echo PHP_EOL . "$c $r $d";
}

Open in new window

Author

Commented:
Hi Ray .....

The $result = client->GetJobOpenings construct generates the object I described above,

 do you know how I can get the data in the xml format you used ?  I can only get it when I choose 'view source' in the browsers and than manually copy it.

Most Valuable Expert 2011
Top Expert 2016

Commented:
I am guessing a little bit here, so let's work together to get to the exact answer.

In your PHP code you find a data structure that looks something like this:

stdClass Object
(
    [GetJobOpeningsResult] => ...

Let's say that object is named $result.  You can use var_dump($result) to look at the object, and it will be easier to read if you echo "<pre>"; before var_dump().

You might find that the string of data you need is located by accessing $result->GetJobOpeningsResult or $result["GetJobOpeningsResult"] -- the output of var_dump() should tell you which syntax works.

Once you have the <NewDataSet> xml string, you should be able to use that data in the way demonstrated at ID:37082492.

Author

Commented:
well I simply tried both ......

the $result["GetJobOpeningsResult"]  results in an error message :
Fatal error: Cannot use object of type stdClass as array in C:\Inetpub\wwwroot-ace-eu\zeepkist3.php on line 10
PHP Fatal error: Cannot use object of type stdClass as array in C:\Inetpub\wwwroot-ace-eu\zeepkist3.php on line 10

So that didn't seem to work well ..... echoing $result->GetJobOpeningsResult gets me:
7 2011-01-01T00:00:00+01:00 2013-01-01T00:00:00+01:00 MySolution FLEX UTRECHT U-A00033 Recruiter MySolution Nieuw Demonstratieteam Amsterdam West-Nederland Nederland Gewenste functie 0  36 2011-01-01T00:00:00+01:00 2011-12-30T00:00:00+01:00 MySolution FLEX UTRECHT U-Z00011 Petrochemie Arno de Haan Nieuwegein Nederland Gewenste functie 0  35 2011-07-10T00:00:00+02:00 2011-12-10T00:00:00+01:00 MySolution FLEX UTRECHT U-A00011 Monteur speciaal werk GTI Monteur Arno de Haan Schiedam Nederland Gewenste functie 0  

so about the same as before, just everything in one string... I've added the source of that in snipped nr 8323095. (first onde

Also I've added the var_dump() in snipped nr 8323096 .... (second)


<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="Code" type="xs:int" minOccurs="0" />
                <xs:element name="Start_x0020_Date" type="xs:dateTime" minOccurs="0" />
                <xs:element name="End_x0020_Date" type="xs:dateTime" minOccurs="0" />
                <xs:element name="Company" type="xs:string" minOccurs="0" />
                <xs:element name="Responsibility_x0020_Center" type="xs:string" minOccurs="0" />
                <xs:element name="Request_x0020_No_" type="xs:string" minOccurs="0" />
                <xs:element name="Description" type="xs:string" minOccurs="0" />
                <xs:element name="Function" type="xs:string" minOccurs="0" />
                <xs:element name="Co-Worker" type="xs:string" minOccurs="0" />
                <xs:element name="City" type="xs:string" minOccurs="0" />
                <xs:element name="Territory" type="xs:string" minOccurs="0" />
                <xs:element name="Country" type="xs:string" minOccurs="0" />
                <xs:element name="Characteristic" type="xs:string" minOccurs="0" />
                <xs:element name="Char_Value" type="xs:string" minOccurs="0" />
                <xs:element name="Char__x0020_Entry_x0020_No_" type="xs:int" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <Table>
    <Code>7</Code>
    <Start_x0020_Date>2011-01-01T00:00:00+01:00</Start_x0020_Date>
    <End_x0020_Date>2013-01-01T00:00:00+01:00</End_x0020_Date>
    <Company>MySolution FLEX</Company>
    <Responsibility_x0020_Center>UTRECHT</Responsibility_x0020_Center>
    <Request_x0020_No_>U-A00033</Request_x0020_No_>
    <Description>Recruiter MySolution Nieuw</Description>
    <Function>Demonstratieteam</Function>
    <Co-Worker />
    <City>Amsterdam</City>
    <Territory>West-Nederland</Territory>
    <Country>Nederland</Country>
    <Characteristic>Gewenste functie</Characteristic>
    <Char_Value />
    <Char__x0020_Entry_x0020_No_>0</Char__x0020_Entry_x0020_No_>
  </Table>
  <Table>
    <Code>36</Code>
    <Start_x0020_Date>2011-01-01T00:00:00+01:00</Start_x0020_Date>
    <End_x0020_Date>2011-12-30T00:00:00+01:00</End_x0020_Date>
    <Company>MySolution FLEX</Company>
    <Responsibility_x0020_Center>UTRECHT</Responsibility_x0020_Center>
    <Request_x0020_No_>U-Z00011</Request_x0020_No_>
    <Description>Petrochemie</Description>
    <Function />
    <Co-Worker>Arno de Haan</Co-Worker>
    <City>Nieuwegein</City>
    <Territory />
    <Country>Nederland</Country>
    <Characteristic>Gewenste functie</Characteristic>
    <Char_Value />
    <Char__x0020_Entry_x0020_No_>0</Char__x0020_Entry_x0020_No_>
  </Table>
  <Table>
    <Code>35</Code>
    <Start_x0020_Date>2011-07-10T00:00:00+02:00</Start_x0020_Date>
    <End_x0020_Date>2011-12-10T00:00:00+01:00</End_x0020_Date>
    <Company>MySolution FLEX</Company>
    <Responsibility_x0020_Center>UTRECHT</Responsibility_x0020_Center>
    <Request_x0020_No_>U-A00011</Request_x0020_No_>
    <Description>Monteur speciaal werk GTI</Description>
    <Function>Monteur</Function>
    <Co-Worker>Arno de Haan</Co-Worker>
    <City>Schiedam</City>
    <Territory />
    <Country>Nederland</Country>
    <Characteristic>Gewenste functie</Characteristic>
    <Char_Value />
    <Char__x0020_Entry_x0020_No_>0</Char__x0020_Entry_x0020_No_>
  </Table>
</NewDataSet>

Open in new window

object(stdClass)#2 (1) {
  ["GetJobOpeningsResult"]=>
  string(3834) "
  
    
      
        
          
            
              
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
              
            
          
        
      
    
  
  7
    2011-01-01T00:00:00+01:00
    2013-01-01T00:00:00+01:00
    MySolution FLEX
    UTRECHT
    U-A00033
    Recruiter MySolution Nieuw
    Demonstratieteam
    
    Amsterdam
    West-Nederland
    Nederland
    Gewenste functie
    
    0
   
  36
    2011-01-01T00:00:00+01:00
    2011-12-30T00:00:00+01:00
    MySolution FLEX
    UTRECHT
    U-Z00011
    Petrochemie
    
    Arno de Haan
    Nieuwegein
    
    Nederland
    Gewenste functie
    
    0
   
  35
    2011-07-10T00:00:00+02:00
    2011-12-10T00:00:00+01:00
    MySolution FLEX
    UTRECHT
    U-A00011
    Monteur speciaal werk GTI
    Monteur
    Arno de Haan
    Schiedam
    
    Nederland
    Gewenste functie
    
    0
   
"
}

Open in new window

Most Valuable Expert 2011
Top Expert 2016
Commented:
When you have browser controls like XML or HTML tags, you may need to use "view source" to see the actual stuff that is in the browser output stream.  This is true even with var_dump().

Let's go back to the script at ID:37082492.  Replace lines 5-90 with this (assumes you have made the SOAP connection and gotten the object back).

$xml = (string)$result->GetJobOpeningsResult;

Author

Commented:
it works .... thnx!

I've added the complete code I use to get to iterate through the data in code snipped 8232127.
$client = new SoapClient("http://R01.ws.mysolutionflex.nl/JobOpeningservice.asmx?wsdl");
	$result = $client->GetJobOpenings(array('features' => SOAP_SINGLE_ELEMENT_ARRAYS, 'company' => 'MySolution FLEX', 'responsibilitycenter' => '', 'function' => '', 'city' => '', 'territory' => '', 'country' => 'Nederland', 'keyword' => '' ));
	
	$xml = (string)$result->GetJobOpeningsResult; 
	
	$obj = SimpleXML_Load_String($xml);


	// USE AN ITERATOR TO ACCESS DATA INSIDE THE OBJECT
	foreach ($obj->Table as $t)
	{
    	$c = (string)$t->Code;
    	$r = (string)$t->Request_x0020_No_;
    	$d = (string)$t->Description;

    	// SHOW THE DATA WE FOUND
    	echo PHP_EOL . "$c $r $d";
		echo '************';
	}

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
Thanks for the points -- glad you've made progress here, ~Ray

Explore More ContentExplore courses, solutions, and other research materials related to this topic.