0

I'm an intern in a French company and today I started to work on a php SOAP project.

The main thing is to get data from the SOAP Request Response.

Please found the var_dump value of the response :

The\path\of\my\php\File.php:7:string '

--uuid:0fadab50-5d3c-4ee8-b58c-3a1bafca60e8

Content-Type: application/xop+xml; charset=UTF-8; type="application/soap+xml";

Content-Transfer-Encoding: binary

Content-ID: <root.message@cxf.apache.org>

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Body><ns2:getVersionResponse xmlns:ns2="http://www.reseaux-et-canalisations.gouv.fr/ws2_2" xmlns:ns3="http://www.reseaux-et-canalisations.gouv.fr/schema-teleservice/2.2" xmlns:ns4="http://www.opengis.net/gml/3.2" xmlns:ns5="http://'... (length=934)

I'm struggling to get thoses datas because what I get as response is a string and not a XML.

I tried to use simplexml_load_string() function but string conversion is not working at all.

What I get is an SimpleXMLElement object.

I used this to get only what I'm insterested in :

$soapClient = new GUSoapClient();
$soapClient->getVersion("xml");
$lastResponse = $soapClient->getClient()->__last_response;
preg_match('/<soap:Envelope.*<\/soap:Envelope>/', $lastResponse, $output_array);
$xmlTxt = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>".$output_array[0];

The function getVersion() return the SoapClient()->__getLastResponse();.

In GUSoapClient(), the attribute client is private so I get it with getClient().

The value of $lastResponse is :

--uuid:ea2243f0-cb00-44ab-a9df-9a77d46febf8 Content-Type: application/xop+xml; charset=UTF-8; type="application/soap+xml"; Content-Transfer-Encoding: binary Content-ID: <root.message@cxf.apache.org> <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Body><ns2:getVersionResponse xmlns:ns2="http://www.reseaux-et-canalisations.gouv.fr/ws2_2" xmlns:ns3="http://www.reseaux-et-canalisations.gouv.fr/schema-teleservice/2.2" xmlns:ns4="http://www.opengis.net/gml/3.2" xmlns:ns5="http://www.w3.org/1999/xlink" xmlns:ns6="http://www.isotc211.org/2005/gco" xmlns:ns7="http://www.isotc211.org/2005/gmd" xmlns:ns8="http://www.isotc211.org/2005/gts" xmlns:ns9="http://xml.insee.fr/schema"><version>4.3.3-SNAPSHOT</version><phase>1.0 1.1 1.2 2.0 2.1 2.2 2.3 3.1 3.2 3.3 5.1</phase><environment>recette</environment></ns2:getVersionResponse></soap:Body></soap:Envelope> --uuid:ea2243f0-cb00-44ab-a9df-9a77d46febf8--

The value of $output_array is :

 <?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Body><ns2:getVersionResponse xmlns:ns2="http://www.reseaux-et-canalisations.gouv.fr/ws2_2" xmlns:ns3="http://www.reseaux-et-canalisations.gouv.fr/schema-teleservice/2.2" xmlns:ns4="http://www.opengis.net/gml/3.2" xmlns:ns5="http://www.w3.org/1999/xlink" xmlns:ns6="http://www.isotc211.org/2005/gco" xmlns:ns7="http://www.isotc211.org/2005/gmd" xmlns:ns8="http://www.isotc211.org/2005/gts" xmlns:ns9="http://xml.insee.fr/schema"><version>4.3.3-SNAPSHOT</version><phase>1.0 1.1 1.2 2.0 2.1 2.2 2.3 3.1 3.2 3.3 5.1</phase><environment>recette</environment></ns2:getVersionResponse></soap:Body></soap:Envelope>

I tried to do this :

$xml = simplexml_load_string($xmlTxt);

But var_dump($xml) give me this :

object(SimpleXMLElement)[5]

All I want is to get the value I want. If you have any ideas on what I'm doing wrong I would like to ear them. Thanks !

NB: I'm french, my English is not perfect. If I'm not clear please tell me ;)

RemiZozo
  • 3
  • 3
  • Remember XML is passed around as a string. You have to convert it to something usable using an XML Reader of some sort – RiggsFolly Dec 11 '18 at 14:54
  • What is `$truc`. – RiggsFolly Dec 11 '18 at 14:57
  • Oops, I forget to rename this value, $truc is $soapClient. – RemiZozo Dec 11 '18 at 15:00
  • To get the actual content of a SimpleXMLElement - `echo $xml->asXML();` – Nigel Ren Dec 11 '18 at 15:15
  • @RiggsFolly, I tried to use the php class `XMLReader` following the exemple here after [link](https://stackoverflow.com/questions/1835177/how-to-use-xmlreader-in-php). I manage to get the data I want but I used an xml file with my data inside to get it. But It some ind of work. Thanks for the hint ! – RemiZozo Dec 11 '18 at 15:27

2 Answers2

0

Try echo instead of var_dump SimpleXMLElements have a __toString() method that means if you try to echo it, even though it is an object it will convert to string for you. http://php.net/manual/fr/simplexmlelement.tostring.php

RMcLeod
  • 2,561
  • 1
  • 22
  • 38
0

I had some difficulties, if simple_xml has to deal with namespace prefix.

Try to remove

$clean_xml = str_ireplace(['SOAP-ENV:', 'SOAP:', 'ns2:'], '', $xml);

$xmlArray = simplexml_load_string($clean_xml);

$json = json_encode($xmlArray);
$array = json_decode($json,TRUE);

$value = $array['foo']['bar'];

The $array should contain the SOAP Result as an array

ivion
  • 567
  • 1
  • 9
  • 14
  • Wow, It's exactly what I wanted ! Thanks a lot ! It contain all the SOAP Result in the array. Thank you all for your help ! – RemiZozo Dec 11 '18 at 16:00