simplexml_load_string unable to retrieve values - php

I am trying to get the value in ResponseId and MAP_IMAGE_ZOOM1000 but i receive empty responses from var_dump.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org /soap/envelope/">
<soapenv:Header xmlns:get="http://tnb.com.my/CGIS/D/getcustareasnapshotpro" xmlns:bsm="http://www.tnb.com.my/CGIS/schemas/bsmfpro" xmlns:cgis="http://tnb.com.my/CGIS/D/cgis_cmccustomermgnt"/>
<soapenv:Body xmlns:get="http://tnb.com.my/CGIS/D/getcustareasnapshotpro" xmlns:bsm="http://www.tnb.com.my/CGIS/schemas/bsmfpro" xmlns:cgis="http://tnb.com.my/CGIS/D/cgis_cmccustomermgnt">
<get1:GetCustAreaSnapshotResponseParam xmlns:get1="http://tnb.com.my/CGIS/D/getcustareasnapshotcon">
<ResponseHdr>
<bsm:ResponseId>gero etgero etgero etgero etgero</bsm:ResponseId>
<bsm:ResTransactionId>123456789012345</bsm:ResTransactionId>
<bsm:ProviderId>CGIS</bsm:ProviderId>
<bsm:ResTimestamp>2004-02-15T02:44:14</bsm:ResTimestamp>
<bsm:ResStatus>SUCC</bsm:ResStatus>
<bsm:MsgCode>IM-001</bsm:MsgCode>
<bsm:MsgDesc>Success</bsm:MsgDesc>
</ResponseHdr>
<ResGetCustAreaSnapshot>
<cmc:GetCustAreaSnapshot xmlns:cmc="http://tnb.com.my/CGIS/D/cmc_customermgnt">
<cmc:MAP_IMAGE_ZOOM1000>abc</cmc:MAP_IMAGE_ZOOM1000>
</cmc:GetCustAreaSnapshot>
</ResGetCustAreaSnapshot>
</get1:GetCustAreaSnapshotResponseParam>
$Envelope = simplexml_load_string($responseXml);
$Envelope->registerXPathNamespace('soap','http://schemas.xmlsoap.org/soap/envelope/');
$Envelope->registerXPathNamespace('bsm','http://www.tnb.com.my/CGIS/schemas/bsmfpro/');
$Envelope->registerXPathNamespace('cmc','http://tnb.com.my/CGIS/D/cgis_cmccustomermgnt/');
$Envelope->registerXPathNamespace('get','http://tnb.com.my/CGIS/D/getcustareasnapshotcon/');
$result = $Envelope->xpath('soap:Envelope/soap:Body/get:GetCustAreaSnapshotResponseParam/ResponseHdr/bsm:ResponseId');
var_dump($result);
die;
any help would be much appreciated. Thank You !!

For some reason the last ResponseId is not working for me.
However, I "cheated", and I can select the first child of ResponseHdr:
$result = $Envelope->xpath('//soapenv:Envelope/soapenv:Body/get1:GetCustAreaSnapshotResponseParam/ResponseHdr/*[1]');
Edit: Here is the best I was able to do with SimpleXML. I'm going to try DOMDocument though, as it may be a better alternative.
$result = $Envelope->xpath('//soapenv:Envelope/soapenv:Body/get1:GetCustAreaSnapshotResponseParam/ResponseHdr');
foreach( $result[0]->children('bsm', true) as $node) var_dump( $node->getName() . ' = ' . (string) $node);
$result = $Envelope->xpath('//soapenv:Envelope/soapenv:Body/get1:GetCustAreaSnapshotResponseParam/ResGetCustAreaSnapshot/*[1]');
foreach( $result[0]->children('cmc', true) as $node) var_dump( $node->getName() . ' = ' . (string) $node);
Given the above code, I was able to get the following output:
string(45) "ResponseId = gero etgero etgero etgero etgero"
string(34) "ResTransactionId = 123456789012345"
string(17) "ProviderId = CGIS"
string(34) "ResTimestamp = 2004-02-15T02:44:14"
string(16) "ResStatus = SUCC"
string(16) "MsgCode = IM-001"
string(17) "MsgDesc = Success"
string(24) "MAP_IMAGE_ZOOM1000 = abc"

Try this:
Save it as ns.xml
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header xmlns:get="http://tnb.com.my/CGIS/D/getcustareasnapshotpro" xmlns:bsm="http://www.tnb.com.my/CGIS/schemas/bsmfpro" xmlns:cgis="http://tnb.com.my/CGIS/D/cgis_cmccustomermgnt"/>
<soapenv:Body xmlns:get="http://tnb.com.my/CGIS/D/getcustareasnapshotpro" xmlns:bsm="http://www.tnb.com.my/CGIS/schemas/bsmfpro" xmlns:cgis="http://tnb.com.my/CGIS/D/cgis_cmccustomermgnt">
<get1:GetCustAreaSnapshotResponseParam xmlns:get1="http://tnb.com.my/CGIS/D/getcustareasnapshotcon">
<ResponseHdr>
<bsm:ResponseId>gero etgero etgero etgero etgero</bsm:ResponseId>
<bsm:ResTransactionId>123456789012345</bsm:ResTransactionId>
<bsm:ProviderId>CGIS</bsm:ProviderId>
<bsm:ResTimestamp>2004-02-15T02:44:14</bsm:ResTimestamp>
<bsm:ResStatus>SUCC</bsm:ResStatus>
<bsm:MsgCode>IM-001</bsm:MsgCode>
<bsm:MsgDesc>Success</bsm:MsgDesc>
</ResponseHdr>
<ResGetCustAreaSnapshot>
<cmc:GetCustAreaSnapshot xmlns:cmc="http://tnb.com.my/CGIS/D/cmc_customermgnt">
<cmc:MAP_IMAGE_ZOOM1000>abc</cmc:MAP_IMAGE_ZOOM1000>
</cmc:GetCustAreaSnapshot>
</ResGetCustAreaSnapshot>
</get1:GetCustAreaSnapshotResponseParam>
</soapenv:Body>
</soapenv:Envelope>
PHP Code to get the nodes:
<?php
$xml = simplexml_load_file( 'ns.xml' );
$xml->registerXPathNamespace('b', 'http://www.tnb.com.my/CGIS/schemas/bsmfpro');
$xml->registerXPathNamespace('c', 'http://tnb.com.my/CGIS/D/cmc_customermgnt');
$xpath = $xml->xpath( '//b:ResponseId | //c:MAP_IMAGE_ZOOM1000' );
foreach( $xpath as $key => $value ) {
// echo the node name and its value
echo $value->getName() . ' => ' . $value . "\n<br>";
}
?>
Hope this helps.

Related

how to ignore <sub></sub> tag from xml file? please see example

Here is the XML file let's say $xml;
<?xml version="1.0"?>
<btps>
<u5_SO2>
<label>Sulphur Dioxide (SO<sub>2</sub>)</label>
<value>..</value>
<unit>mg/Nm<super>3</super></unit>
</u5_SO2>
<u5_NO2>
<label>Nitrogen Dioxide (NO<sub>2</sub>)</label>
<value>..</value>
<unit>mg/Nm<super>3</super></unit>
</u5_NO2>
</btps>
Here is the PHP script
$label = $xml->u5_SO2->label;
$value = $xml->u5_SO2->value;
$unit = $xml->u5_SO2->unit;
echo "<br>".$label;
echo "<br>".$value;
echo "<br>".$unit;
when i echo this $label variable it printing like this Sulphur
Dioxide (SO) but what I accept is Sulphur Dioxide (SO2) is it possible to print what I accepted?
SimpleXML is not a good option if you have mixed type child nodes. With current DOM this is a lot easier because it allows for precise node manipulations. Use Xpath expressions to fetch nodes and DOM methods to manipulate them.
DOM nodes have a property textContent which allows you to read (and write) all descendant text nodes as a string.
Here is an example that replaces elements with text nodes (with unicode characters):
$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);
$replacements = [
'//sub' => ['2' => "\u{2082}", '3' => "\u{2083}" /*,...*/],
'//super' => ['2' => "\u{00B2}", '3' => "\u{00B3}" /*,...*/]
];
foreach ($replacements as $expression => $map) {
// fetch and iterate nodes
foreach ($xpath->evaluate($expression) as $sub) {
$content = $sub->textContent;
// check map
if (isset($map[$content])) {
// replace element with text node
$sub->parentNode->replaceChild(
$document->createTextNode($map[$content]),
$sub
);
}
}
}
echo $document->saveXML();
Output:
<?xml version="1.0"?>
<btps>
<u5_SO2>
<label>Sulphur Dioxide (SO₂)</label>
<value>..</value>
<unit>mg/Nm³</unit>
</u5_SO2>
<u5_NO2>
<label>Nitrogen Dioxide (NO₂)</label>
<value>..</value>
<unit>mg/Nm³</unit>
</u5_NO2>
</btps>
Reading the modified DOM with Xpath expressions:
foreach ($xpath->evaluate('/btps/*') as $element) {
var_dump(
[
'label' => $xpath->evaluate('string(label)', $element),
'value' => $xpath->evaluate('string(value)', $element),
'unit' => $xpath->evaluate('string(unit)', $element),
]
);
}
Output:
array(3) {
["label"]=>
string(23) "Sulphur Dioxide (SO₂)"
["value"]=>
string(2) ".."
["unit"]=>
string(7) "mg/Nm³"
}
array(3) {
["label"]=>
string(24) "Nitrogen Dioxide (NO₂)"
["value"]=>
string(2) ".."
["unit"]=>
string(7) "mg/Nm³"
}
I would argue in favor of using DOMDocument over SimpleXML, in favor of using xpath over dot notation and against regex in xml under any circumstances.
So with that said, after the usual DOMDocument boilerplate, I would use this:
$label = $xpath->evaluate('//u5_SO2/label')[0];
echo $label->textContent;
Output:
Sulphur Dioxide (SO2)
I assume you're using SimpleXML. I don't think you can't easily do it with that extension alone, but you can use it together with DOMDocument (dom_import_simplexml() and DOMNode::nodeValue):
dom_import_simplexml($xml->u5_SO2->label)->nodeValue
Demo.
You can use a regex to find and remove the element. Like it
<?php
$xml = '<?xml version="1.0"?>
<btps>
<u5_SO2>
<label>Sulphur Dioxide (SO<sub>2</sub>)</label>
<value>..</value>
<unit>mg/Nm<super>3</super></unit>
</u5_SO2>
<u5_NO2>
<label>Nitrogen Dioxide (NO<sub>2</sub>)</label>
<value>..</value>
<unit>mg/Nm<super>3</super></unit>
</u5_NO2>
</btps>';
$xml = preg_replace('/(<sub>)(.*)?(<\/sub>)/', '$2', $xml);
$xml = simplexml_load_string($xml);
$label = $xml->u5_SO2->label;
$value = $xml->u5_SO2->value;
$unit = $xml->u5_SO2->unit;
echo "<br>".$label . PHP_EOL;
echo "<br>".$value . PHP_EOL;
echo "<br>".$unit . PHP_EOL;
Check in codepad
http://codepad.org/SbmbmGFl
You else, can put it back when parse the element like that
<?php
$xml = '<?xml version="1.0"?>
<btps>
<u5_SO2>
<label>Sulphur Dioxide (SO<sub>2</sub>)</label>
<value>..</value>
<unit>mg/Nm<super>3</super></unit>
</u5_SO2>
<u5_NO2>
<label>Nitrogen Dioxide (NO<sub>2</sub>)</label>
<value>..</value>
<unit>mg/Nm<super>3</super></unit>
</u5_NO2>
</btps>';
$xml = preg_replace('/(<sub>)(.*)?(<\/sub>)/', '___sub___$2___sub___', $xml);
$xml = simplexml_load_string($xml);
foreach($xml as $row) {
$row->label = preg_replace('/(___sub___)(.*)?(___sub___)/', '<sub>$2</sub>', $row->label);
}
$label = $xml->u5_SO2->label;
$value = $xml->u5_SO2->value;
$unit = $xml->u5_SO2->unit;
echo "<br>".$label . PHP_EOL;
echo "<br>".$value . PHP_EOL;
echo "<br>".$unit . PHP_EOL;
Check in codepad
http://codepad.org/mvnJYnGB

XML Parsing in PHP (using simplexml_load_string)

I have the following code and I have been working to try to get this working.
<?php declare(strict_types=1);
$session_token = '?'; $xml = '';
$result = '<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://ws.careerbuilder.com/resumes/"><Packet><Errors /><SessionToken>3msk323msd-3312-CQ-2</SessionToken></Packet></string>
';
if ($result) {
$xml = simplexml_load_string($result);
print_r($xml);
if ($xml !== false) {
$session_token = $xml->SessionToken;
echo PHP_EOL.'Session: '. $session_token;
} else {
echo 'Error: XML does NOT appear to be valid';
}
} else
echo 'Error: result does NOT appear be valid';
The problem is no matter what I'm not able to extract the <SessionToken> value from the XML. When I use print_r() I get the following:
SimpleXMLElement Object
(
[0] => <Packet><Errors /><SessionToken>3msk323msd-3312-CQ-2</SessionToken></Packet>
)
Your input is entity-encoded. If this is really what it looks like, you'll need to decode it first:
$xml = simplexml_load_string(html_entity_decode($result));
$token = (string) $xml->Packet->SessionToken[0];
You document contains nested XML. The text content of the string element is serialized XML. So you need to parse it after reading it.
$result = '<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://ws.careerbuilder.com/resumes/"><Packet><Errors /><SessionToken>3msk323msd-3312-CQ-2</SessionToken></Packet></string>
';
$string = new SimpleXMLElement($result);
$packet = new SimpleXMLElement((string)$string);
var_dump($packet);
Output:
object(SimpleXMLElement)#2 (2) {
["Errors"]=>
object(SimpleXMLElement)#3 (0) {
}
["SessionToken"]=>
string(20) "3msk323msd-3312-CQ-2"
}

Load Foreach loop into string as XML

I'm trying to load a foreach into a string formatted as XML.
I've tried as follows
<?php
$data = '<?xml version="1.0" encoding="utf-8" ?><markers>';
foreach ($entries as $entry => $marker ) {
'<marker name="' . $marker->getName() . '"'.'lat="' . $marker->getLatitude() . '"'.'lng="' . $marker->getLongitude() . '"'.'address="' . $marker->getAddressLineOne() . '"'.'address2="' . $marker->getAddressLineTwo() . '"'.'city="' . $marker->getCitySuburb() . '"'.'state="' . $marker->getState('fieldName') . '"'.'postal="' . $marker->getPostCode() . '"'.'country="' . $marker->getCountry('fieldName') . '"'.'phone="' . $marker->getPhone() . '"'.'email="' . $marker->getEmail() . '"'.'web="' . $marker->getWebSite() . '"'.'/>';
}
'</markers>';
?>
But what I end up getting is:
nothing in the $data variable
for some reason each item is nesting in the previous item
Basically I would like to achieve the following result:
<?php
$data = '<?xml version="1.0" encoding="utf-8"?>
<markers>
<marker name="Chipotle Minneapolis" lat="44.947464" lng="-93.320826" category="Restaurant" address="3040 Excelsior Blvd" address2="" city="Minneapolis" state="MN" postal="55416" country="US" phone="612-922-6662" email="info#chipotle.com" web="http://www.chipotle.com" />
<marker name="Chipotle St. Louis Park" lat="44.930810" lng="-93.347877" category="Restaurant" address="5480 Excelsior Blvd." address2="" city="St. Louis Park" state="MN" postal="55416" country="US" phone="952-922-1970" email="info#chipotle.com" web="http://www.chipotle.com" />
<marker name="Chipotle Minneapolis" lat="44.9553438" lng="-93.29719699999998" category="Restaurant, Bar" address="2600 Hennepin Ave." address2="" city="Minneapolis" state="MN" postal="55404" country="US" phone="612-377-6035" email="info#chipotle.com" web="http://www.chipotle.com" />
</markers>';
?>
Thanks
Said
Revised code
<?php
$data = simplexml_load_string("<markers />");
foreach ($entries as $entry => $marker ) {
$newMarker = $data->addChild("marker");
$newMarker->addAttribute("name", $marker->getName());
$newMarker->addAttribute("lat", $marker->getLatitude());
$newMarker->addAttribute("lng", $marker->getLongitude());
$newMarker->addAttribute("state", $marker->getPostCode());
}
echo $data->asXML();
?>
<?php
echo var_dump($data);
?>
After the first line where you use
$data = '<?xml version="1.0" encoding="utf-8" ?><markers>';
you don't add any of the further text onto the variable. You need to use something like
$data .='<marker name="' . $marker->getName() .`
(with the dot before the = to add the value), you also need to be careful with the spacing and the quotes.
I would also recommend using something like SimpleXML to build the XML rather than using put text, it is longer code but safer. Something like...
$xml = simplexml_load_string("<markers />");
foreach ($entries as $entry => $marker ) {
$newMarker = $xml->addChild("marker");
$newMarker->addAttribute("name", $marker->getName());
$newMarker->addAttribute("lat", $marker->getLatitude());
$newMarker->addAttribute("lng", $marker->getLongitude());
// Repeat for all attributes
}
echo $xml->asXML();
Since it's not clear to me how $entries are generated, I'll make up some data in couple of arrays and create a simplified version of your expected output, with the help of a function borrowed from here. Obviously, you'll need to modify it to fit your actual code.
$data = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<markers>
</markers>
XML;
$xml = simplexml_load_string($data);
function sxml_append(SimpleXMLElement $to, SimpleXMLElement $from) {
$toDom = dom_import_simplexml($to);
$fromDom = dom_import_simplexml($from);
$toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
}
$stores = ["Chipotle Chicago","Chipotle St. Louis Park","Chipotle Minneapolis"];
$locations = ["Chicago","St. Louis Park","Minneapolis"];
$destination = $xml->xpath('//markers');
foreach(array_combine($stores, $locations) as $store => $location) {
$tm = simplexml_load_string('<marker name="xxx" city="yyy"/>');
$tmxml = $tm->xpath('//marker')[0];
$tmxml['name'] = $store;
$tmxml['city'] = $location;
sxml_append($destination[0], $tmxml);
}
echo $xml->asXML();
Output:
<?xml version="1.0" encoding="UTF-8"?>
<markers>
<marker name="Chipotle Chicago" city="Chicago" />
<marker name="Chipotle St. Louis Park" city="St. Louis Park" />
<marker name="Chipotle Minneapolis" city="Minneapolis" />
</markers>

Simple XML to parse general recordset

I am trying to find a way to iterate through an XML recordset containing a namespace. However, I don't know the field names in advance. Sample XML is below.
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://www.site.com/SMART/Updates">
<NewDataSet>
<record>
<FIELD1>data1</FIELD1>
<FIELD2>data2</FIELD2>
<FIELD3>data3</FIELD3>
</record>
<record>
<FIELD1>data1</FIELD1>
<FIELD2>data2</FIELD2>
<FIELD3>data3</FIELD3>
</record>
</NewDataSet>
Again, I won't know the field names in advance. I need read the namespace, find the name of the root element ("NewDataSet", in this case) and then need to get the field names and values of the individual elements. I have tried to use $xml->getname(), and $xml->xpath('\') to find the root element name, but been unable to crack it.
(as discussed in Chat)
Plain DOM functions are the best way to process XML.
Demo or code:
<?php
header('Content-Type: text/plain');
$xml = <<<END
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://www.site.com/SMART/Updates">
<NewDataSet>
<record>
<FIELD1>data1</FIELD1>
<FIELD2>data2</FIELD2>
<FIELD3>data3</FIELD3>
</record>
<record>
<FIELD1>data1</FIELD1>
<FIELD2>data2</FIELD2>
<FIELD3>data3</FIELD3>
</record>
</NewDataSet>
</string>
END;
$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->normalize();
$dom->loadXML($xml);
echo 'Root element name: ' . $dom->firstChild->firstChild->tagName . PHP_EOL;
echo 'Number of child elements: ' . count($dom->firstChild->firstChild->childNodes) . PHP_EOL;
echo '=====' . PHP_EOL . PHP_EOL;
echo print_node($dom->firstChild->firstChild);
function print_node($node, $level = 0, $prev_level = 0) {
$result = '';
if($node->hasChildNodes()) {
foreach($node->childNodes as $subnode) {
$result .= str_repeat(' ', $level) . $node->tagName . ' =>' . PHP_EOL;
$result .= print_node($subnode, $level + 1, $level) . PHP_EOL;
}
} else {
if(trim($node->nodeValue) !== '') {
$result .= str_repeat(' ', $level) . '**Data: ' . trim($node->nodeValue) . PHP_EOL;
}
}
return $result;
}
?>
Output:
Root element name: NewDataSet
Number of child elements: 1
=====
NewDataSet =>
record =>
FIELD1 =>
**Data: data1
record =>
FIELD2 =>
**Data: data2
record =>
FIELD3 =>
**Data: data3
NewDataSet =>
record =>
FIELD1 =>
**Data: data1
record =>
FIELD2 =>
**Data: data2
record =>
FIELD3 =>
**Data: data3
Your XML is invalid, but assuming the string tag is closed after the </NewDataSet> tag:
You can get the namespaces declared in the document using getDocNamespaces().
$xml = simplexml_load_string($xmlfile);
$namespaces = $xml->getDocNamespaces(); //array of namespaces
$dataset = $xml->children(); //first child (NewDataSet)
echo $dataset->getName(); //NewDataSet
$records = $dataset->children();
$i = 0;
$result = array();
foreach ($records as $key => $value) {
foreach ($value as $fieldName => $fieldData) {
$result[$i][$fieldName] = (string)$fieldData;
}
$i++;
}
var_dump($result);
Now $result contains an array that is easier to read and contains the rows :
array(2) {
[0]=> array(3) {
["FIELD1"]=> string(5) "data1"
["FIELD2"]=> string(5) "data2"
["FIELD3"]=> string(5) "data3"
}
[1]=> array(3) {
["FIELD1"]=> string(5) "data1"
["FIELD2"]=> string(5) "data2"
["FIELD3"]=> string(5) "data3"
}
}
Looking at the chat transcript posted in another answer, it looks like the element actually contains a string which is an escaped XML document. So there is exactly one element in the outer document, called <string>. It has no children, just content. (This looks remarkably like someone using ASP.net's service-builder.)
So the step you are missing is unescaping this inner XML to treat as a new XML document:
// Parse the outer XML, which is just one <string> node
$wrapper_sx = simplexml_load_string($wrapper_xml);
// Extract the actual XML inside it
$response_xml = (string)$wrapper_sx;
// Parse that
$response_sx = simplexml_load_string($response_xml);
// Now handle the XML
$tag_name = $response_sx->getName();
foreach ( $response_sx->children() as $child )
{
// Etc
}
// see http://github.com/IMSoP/simplexml_debug
simplexml_tree($response_sx, true);
I don't actually understand what you problem is, in your said real-life XML you gave in PHP chat, there are no namespaces involved (and even if!).
Just read out the tag-name from the document element:
# echoes NewDataSet / string (depending on which XML input)
echo dom_import_simplexml($simplexml)->ownerDocument->documentElement->tagName;
If you have actually an XML document inside another XML document, you can do the following:
// load outer document
$docOuter = new DOMDocument();
$docOuter->loadXML($xmlString);
// load inner document
$doc = new DOMDocument();
$doc->loadXML($docOuter->documentElement->nodeValue);
echo "Root element is named: ", $doc->documentElement->tagName, "\n";
Or if you prefer SimpleXML:
echo "Root element is named: ",
simplexml_load_string(simplexml_load_string($xmlString))->getName()
;

How do I get XML data from a web service using PHP?

I need to access the response from a .NET web service which returns data in XML format.
How can I split the data returned? For example, I would like to parse the data into some PHP variables:
$name = "Dupont";
$email = "charles.dupont#societe.com";
I have been looking for how to do this for a long time without finding the correct way to do it.
My script is:
$result = $client->StudentGetInformation($params_4)->StudentGetInformationResult;
echo "<p><pre>" . print_r($result, true) . "</pre></p>";
The echo in my page is:
stdClass Object
(
[any] => 0Successful10371DupontCharlescharles.dupont#societe.com1234charles.dupont#societe.comfalsefr-FR1003FIRST FINANCE1778AAA Département
)
The webservice response format is:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<StudentGetInformationResponse xmlns="http://tempuri.org/">
<StudentGetInformationResult>xml</StudentGetInformationResult>
</StudentGetInformationResponse>
</soap:Body>
</soap:Envelope>
I have tried your example. But it doesn't do what i need.
I need to split the value returned. I would like to get the data and put them into PHP variables:
$name = "Dupont";
$email = "charles.dupont#societe.com";
etc...
Unfortunately the echo of your example gives:
object(stdClass)#1 (1) { ["StudentGetInformationResult"]=> object(stdClass)#11 (1) { ["any"]=> string(561) "0Successful10371DupontCharlescharles.dupont#societe.com1234charles.dupont#societe.comfalsefr-FR1003FIRST FINANCE1778AAA Département" } }
The only class you need is SoapClient. There are a lot of examples you can use in the PHP Documentation.
Example:
try {
$client = new SoapClient ( "some.aspx?wsdl" );
$result = $client->StudentGetInformation ( $params_4 );
$xml = simplexml_load_string($result->StudentGetInformationResult->any);
echo "<pre>" ;
foreach ($xml as $key => $value)
{
foreach($value as $ekey => $eValue)
{
print($ekey . " = " . $eValue . PHP_EOL);
}
}
} catch ( SoapFault $fault ) {
trigger_error ( "SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR );
}
Output
Code = 0
Message = Successful
stud_id = 10373
lname = Dupont
fname = Charles
loginid = charles.dupont#societe.com
password = 1234
email = charles.dupont#societe.com
extid =
fdisable = false
culture = fr-FR
comp_id = 1003
comp_name = FIRST FINANCE
dept_id = 1778
dept_name = Certification CMF (Test web service)
udtf1 =
udtf2 =
udtf3 =
udtf4 =
udtf5 =
udtf6 =
udtf7 =
udtf8 =
udtf9 =
udtf10 =
Audiences =

Categories