How convert to array or json php soap __dorequest response? - php

this is _dorequest response. Response is xml string data.
I tried convert with simplexml_load_string() function and later return array but function result is empty.
'<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header/>
<env:Body>
<ns1:createNgiShipmentWithAddressResponse xmlns:ns1="http://yurticikargo.com.tr/NgiShipmentInterfaceServices" xmlns:ns2="http://yurticikargo.com.tr/NgiShipmentInterfaceServices" xmlns:ns3="http://yurticikargo.com.tr/WSExceptions/">
<XShipmentDataResponse>
<outFlag>0</outFlag>
<projectId>18501</projectId>
<specialFieldDataArray>
<specialFieldName>53</specialFieldName>
<specialFieldValue>SIP-1234885679</specialFieldValue>
</specialFieldDataArray>
<specialFieldDataArray>
<specialFieldName>3</specialFieldName>
<specialFieldValue>SIP-1234885679</specialFieldValue>
</specialFieldDataArray>
</XShipmentDataResponse>
</ns1:createNgiShipmentWithAddressResponse>
</env:Body>
</env:Envelope>'

xml converted to array:
$xml = "$result"; //$result xml string
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser,$xml,$vals,$index);
xml_parser_free($xml_parser);
$sonuc = $vals[$index['NODENAME'][0]]["value"];

Related

Extract value from xml tag

I´m trying to extract the RecordID = "1014276" from a tag
I tried with :
$result = curl_exec($ch);
curl_close($ch);
$xml2 = simplexml_load_string($result);
echo $latitude = (string) $xml2['RecordID'];
This is the XML response:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<ns1:createDataResponse xmlns:ns1="http://3e.pl/ADInterface">
<StandardResponse RecordID="1014276" xmlns="http://3e.pl/ADInterface"/>
</ns1:createDataResponse>
</soap:Body>
</soap:Envelope>
This involves a bit more than just accessing the attribute, first you have to select the correct element. Using XPath is the most comment way in this sort of structure.
As this has a default namespace defined for the data, you will need to register this with the SimpleXMLElement first (using $xml2->registerXPathNamespace("ns1","http://3e.pl/ADInterface");.
You can then find the element using the XPAth expression //ns1:StandardResponse. As the xpath() method returns a list of found elements, use [0] to just extract the first match. You should then be able to extract the attribute as in your code using the resultant element...
$xml2 = simplexml_load_string($result);
$xml2->registerXPathNamespace("ns1","http://3e.pl/ADInterface");
$response = $xml2->xpath("//ns1:StandardResponse")[0];
echo (string) $response['RecordID'];
You can apporach this as
$xml = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<ns1:createDataResponse xmlns:ns1="http://3e.pl/ADInterface">
<StandardResponse RecordID="1014276" xmlns="http://3e.pl/ADInterface"/>
</ns1:createDataResponse>
</soap:Body>
</soap:Envelope>';
$p = xml_parser_create();
xml_parse_into_struct($p, $xml, $values, $index);
xml_parser_free($p);
echo $values[3]['attributes']['RECORDID'];

How to convert Sabre SOAP API response to PHP array

I am getting Sabre SOAP API response something like :
<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:header></soap-env:header>
<soap-env:body>
<ota_airlowfaresearchrs xmlns="http://www.opentravel.org/OTA/2003/05" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.1.0" priceditincount="50" brandedonewayitincount="0" simpleonewayitincount="0" departeditincount="0" soldoutitincount="0" availableitincount="0">
<success>
<priceditineraries><priceditinerary sequencenumber="1"></priceditinerary>
<priceditinerary sequencenumber="2"></priceditinerary></priceditineraries>
</success>
</ota_airlowfaresearchrs>
</soap-env:body>
</soap-env:envelope>
But when I tried with simplexml_load_string I am getting a problem to convert it to PHP array. what I tried is :
$parser = simplexml_load_string($res);
$parserEnv = $parser->children('soap-env', true);
$response = $parserEnv->body->ota_airlowfaresearchrs;
its return emptry array like : SimpleXMLElement Object ( )
From
$parser = simplexml_load_string($res);
you have to change like below:
$parser = simplexml_load_string($res, "SimpleXMLElement", LIBXML_NOCDATA);
LIBXML_NOCDATA : You can use this function call to have simplexml convert CDATA into plain text.
As ref to : converting SOAP XML response to a PHP object or array
I got the answer by doing the following way:
$soap = simplexml_load_string($res);
$response = $soap->children('http://schemas.xmlsoap.org/soap/envelope/')
->body->children()
->ota_airlowfaresearchrs
->success
->priceditineraries
thanks #lalithkumar any way

Convert CURL XML response into Array in PHP

I am trying to convert a XML response via CURL to an array in PHP. I have tried with the below code but not getting expected result array.
With using simplexml_load_string:
$xml = simplexml_load_string($response);
$json = json_encode($xml);
$arr = json_decode($json,true);
print_r($arr);//giving array () empty
With using SimpleXMLElement:
$xml = new SimpleXMLElement($response);
print_r($xml); //giving SimpleXMLElement Object ( ) empty array
My Curl response :
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
<soap:Header>
<OGHeader transactionID="00239870" timeStamp="2009-02-23T01:55:01.4625+05:30" primaryLangID="E" xmlns="http://webservices.micros.com/og/4.3/Core/">
<Origin entityID="WEST" systemType="ORS" />
<Destination entityID="OWS" systemType="WEB" />
</OGHeader>
<wsa:Action>http://webservices</wsa:Action>
<wsa:MessageID>urn:uuid:a9a70c23-3d94-4640-9aac-8ac63694733a</wsa:MessageID>
<wsa:RelatesTo>urn:uuid:eb565d90-b682-45e9-b18d-c03fa7323019</wsa:RelatesTo>
<wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
</soap:Header>
<soap:Body>
<CreateBookingResponse xmlns:r="http://webservices" xmlns:hc="http://webservices/" xmlns:c="http://webservices" xmlns="http://webservices">
<Result resultStatusFlag="FAIL">
<c:Text>
<c:TextElement></c:TextElement>
</c:Text>
<c:OperaErrorCode>PRIOR_STAY</c:OperaErrorCode>
</Result>
</CreateBookingResponse>
</soap:Body>
</soap:Envelope>
Try this instead :
<?php
$xml = simplexml_load_string($response);
var_dump($xml->asXML());
Your xml is there as you can see.
Simplexml implements ArrayIterator, therefore you can iterate using foreach or use any of the simpleXmlElement methods to navigate through it (such as children() or xpath()).

SimpleXML Returns 0 Element

I'm taking my response from a Soap Request, and passing it into a new SimpleXML construct.
$response = $this->client->$method(array("Request" => $this->params));
$response_string = $this->client->__getLastResponse();
$this->response = new Classes_Result(new SimpleXMLElement($result));
If I echo the $response_string, it outputs a proper xml string. Here is a snippet as it's quite long.
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body><GetClassesResponse xmlns="http://clients.mindbodyonline.com/api/0_5">
<GetClassesResult>
<Status>Success</Status>
<XMLDetail>Full</XMLDetail>
<ResultCount>6</ResultCount>
<CurrentPageIndex>0</CurrentPageIndex>
<TotalPageCount>1</TotalPageCount>
<Classes>
<Class>
<ClassScheduleID>4</ClassScheduleID>
<Location>
<SiteID>20853</SiteID>
....</soap:Envelope>
Hoever, when I try to work with this object, I get errors or if I dump the object it outputs:
object(SimpleXMLElement)#51 (0)
Any ideas why this might be happening?
You are not actually using $response_string, and you have not set $result anywhere, which you have passed to new SimpleXMLElement($result).
Perhaps you intend to build a SimpleXML object with the $response_string string via simplexml_load_string()?
$response = $this->client->$method(array("Request" => $this->params));
$response_string = $this->client->__getLastResponse();
// Load XML via simplexml_load_string()
$this->response = new Classes_Result(simplexml_load_string($response_string));
// Or if you do expect a SimpleXMLElement(), pass in the string
$this->response = new Classes_Result(new SimpleXMLElement($response_string));
The <soap:Body> element of your SOAP response is namespaced with (soap). To loop over it with SimpleXML, you must provide the correct namespace:
// After creating new SimpleXMLElement()
var_dump($this->response->children("http://schemas.xmlsoap.org/soap/envelope/"));
// class SimpleXMLElement#2 (1) {
// public $Body =>
// class SimpleXMLElement#4 (0) {
// }
// }
To loop over the body:
foreach ($this->response->children("http://schemas.xmlsoap.org/soap/envelope/") as $b) {
$body_nodes = $b->children();
// Get somethign specific
foreach ($body_nodes->GetClassesResponse->GetClassesResult as $bn) {
echo $bn->ResultCount . ", ";
echo $bn->TotalPageCount;
}
}
// 6, 1

Trouble Parsing SOAP response in PHP using simplexml

I'm using cURL to POST a SOAP request. The response is as follows:
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
<wsa:To>http://www.w3.org/2005/08/addressing/anonymous</wsa:To>
<wsa:Action>http://www.csapi.org/schema/parlayx/common/v3_1/TerminalLocationPort/getLocationForGroupResponse</wsa:Action>
</env:Header>
<env:Body>
<ns2:getLocationForGroupResponse xmlns:ns2="http://www.csapi.org/schema/parlayx/terminal_location/v3_1/local">
<ns2:result>
<address>234983</address>
<reportStatus>Retrieved</reportStatus>
<currentLocation>
<latitude>12.5665</latitude>
<longitude>43.7708</longitude>
<timestamp>2012-01-03T17:06:16.805+01:30</timestamp>
</currentLocation>
</ns2:result>
<ns2:result>
<address>423903</address>
<reportStatus>Retrieved</reportStatus>
<currentLocation>
<latitude>12.2165</latitude>
<longitude>43.6518</longitude>
<timestamp>2012-01-03T17:06:16.824+01:30</timestamp>
</currentLocation>
</ns2:result>
</ns2:getLocationForGroupResponse>
</env:Body>
</env:Envelope>
I use this to decode:
$err = curl_error($soap_do);
$result = curl_exec($soap_do);
$xml = simplexml_load_string($result);
$ns = $xml->getNamespaces(true);
$soap = $xml->children($ns['env']);
$getaddressresponse = $soap->body->children($ns['ns2']);
foreach ($getaddressresponse->children() as $item) {
echo (string) $item->address . '<br />';
}
I'm having trouble decoding this with SimpleXML. This link seems most relevant to my situation but I'm unable to apply it to my case as the simpleXML element just
Warning: SimpleXMLElement::children() [simplexmlelement.children]: Node no longer exists in C:\.php on line 33 /*(line with the for each statement)*/
Any suggestions?
UPDATE:
If the server responds with the following error, how would I detect it..?
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
<wsa:To>http://www.w3.org/2005/08/addressing/anonymous</wsa:To>
<wsa:Action>http://www.w3.org/2005/08/addressing/fault</wsa:Action>
</env:Header>
<env:Body>
<env:Fault>
<faultcode>env:Server</faultcode>
<faultstring>Service Exception</faultstring>
<detail>
<ns1:ServiceException xmlns:ns1="http://www.csapi.org/schema/parlayx/common/v3_1" xmlns:ns2="http://www.csapi.org/schema/parlayx/terminal_location/v3_1/local">
<messageId>SVC004</messageId>
<text>Trip not Found for this MSISDN</text>
</ns1:ServiceException>
</detail>
</env:Fault>
</env:Body>
</env:Envelope>
Variable and property names are case sensitive and while I was testing it, it turned out there's other stuff as well. The following works:
$soap = $xml->children($ns['env']);
$getaddressresponse = $soap->Body->children($ns['ns2']);
foreach ($getaddressresponse->getLocationForGroupResponse->children($ns['ns2']) as $item)
{
$item = $item->children();
echo $item->address . '<br />';
}
To answer the updated question:
$fault = $soap->Body->children($ns['env']);
if (isset($fault->Fault))
{
// Handle error
}

Categories