Parsing Complex SOAP XML Response using PHP - php

I am using phpcURL to handle the SOAP request method and I got the following response. Looks like it's little different and complex from what I have read and search on google for handling the xml response using php (simpleXML & DOM).
<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<as:QueryResultMsg
xmlns:as="http://www.as.com/as"
xmlns:ac="http://www.ac.com/ac"
xmlns:cs="http://www.cs.com/cs">
<ResultHeader>
<cs:Version>1</cs:Version>
<cs:Result>Success.</cs:Result>
</ResultHeader>
<QueryResult>
<as:List>
<as:Key>4000020023983</ars:Key>
<as:Result>
<ac:Type>TYPE</ac:Type>
<ac:TypeName>Account_Type_Name</ac:TypeName>
<ac:TotalAmount>200000</ac:TotalAmount>
<ac:Detail>
<ac:InstanceID>03610</ac:InstanceID>
<ac:Amount>200000</arc:Amount>
<ac:InitialAmount>0</arc:InitialAmount>
</ac:Detail>
</as:Result>
<as:AccountCredit>
<as:CreditAmount>5000</as:CreditAmount>
</as:AccountCredit>
</as:List>
</QueryResult>
</as:QueryResultMsg>
</soapenv:Body>
If you can share me how to display following TagElements:-
1) TypeName
2) TotalAmount
3) InitialAmount
4) CreditAmount
And which one is better parser SimpleXml of Dom in this case.

You can user simplexml_load_string in PHP
you can refer this issue PHP Parsing SOAP response

Related

NUSOAP Client response is empty when I call SOAP endpoint on upgraded server

I am using NUSOAP client with a hard coded XML request to communicate with a Server to read a response from a Websphere server. I use nusoap_client->send to send the request.
$result = nusoap_client->send.
Then SOAP response is obtained from $result. Perfect.
Now I am trying to get a server response from an upgraded server (probably not using Websphere. Not sure). SOAP namespace is also changed and the response has some prefix.
I no longer get the SOAP response from $result (when I run strlen() I get zero size) . But I can call nusoap_client->responseData to get a response below. So I try to parse with simplexml_load_string on PHP5.x but it doesnt seem to parse and error message => syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING. Where could I be going wrong?
$stringer = <<<XML
<?xml version='1.0' encoding='ISO-8859-1'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ser:getCarResourceResponse xmlns:ser="http://sos.joburg.com/webservice/ecare/services">
<ser:GetCarResourceReply>
<ser:resCode>10652981251</ser:resCode>
<ser:departName>Ingolstadt</ser:departName>
<ser:resStatus>4</ser:resStatus>
<ser:statusDate>2022-05-09 09:24:50</ser:statusDate>
<ser:isScheduled>0</ser:isScheduled>
<ser:departStatus>0</ser:departStatus>
</ser:GetCarResourceReply>
<ser:ResultOfOperationReply>
<ser:resultCode>0</ser:resultCode>
<ser:resultMessage>Successful</ser:resultMessage>
</ser:ResultOfOperationReply>
</ser:getCarResourceResponse>
</soapenv:Body>
</soapenv:Envelope>
XML;
$log->logInfo(print_r(simplexml_load_string($stringer), 1));
As an update to this, I found the solution. Effectively, while I want to take an unstructured XML output and make it structured, it doesn't work like I thought.
It was necessary for me to take the single line of XML and format it as you see in the code markup. In the text file I lifted this from, I lifted a single line of XML which was not structured into multiple lines you see in stack overflow.
Thank you for your comments.

How to get specific tag value of SOAP response in php Laravel

Thanks in advance.
I am consuming SOAP API in laravel application and have some issues during parse.
My XML response from API.
<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><TransactionResponse xmlns="http://www.iponz.govt.nz/XMLSchema/Object"> <DiscussionContentTransactionResponseBody><TransactionResponseContentDetails><TransactionCode>Get Object</TransactionCode><TransactionResponseData><objectSummaryReport><object><ObjectIdentifier>090018032e4f</ObjectIdentifier><ObjectFormat>gif</ObjectFormat><ObjectName>TM Image</ObjectName><ObjectData>R0lGODdhMQAMQFZAEA</ObjectData></object></objectSummaryReport></TransactionResponseData></TransactionResponseContentDetails></DiscussionContentTransactionResponseBody></TransactionResponse></soapenv:Body></soapenv:Envelope>
I need to Get the value of ObjectData tag.
Is there any sort of trick to fetch this record?
If not then how to remove tags soap:envolop XML version etc.
means I just need to get data tags only after DiscussionContentTransactionResponseBody

returning soap xml response for a soap server php

I am trying to create a soap service in php using native php soap server. I have already prepared the wsdl file.
There are basically four methods that can be called with the soap service. The input soap request for one of the request ShowRemittanceDetail is shown below.
<soap-env:body>
<ns1:showremittancedetailrequest>
<username>admin</username>
<password>pass</password>
<refno>USA1956127848</refno>
</ns1:showremittancedetailrequest>
</soap-env:body>
Anyway the soap request does not have a header and I have just shown the body here. I have no problem parsing the soap request. The response should look like this
<?xml version="1.0" encoding="ISO-8859-1"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns2:responseType xmlns:ns2="http://tempuri.org/response">
<code>00</code>
<message>Transaction does not exist or is not available</message>
<responseBody>
<responseStr>4</responseStr>
</responseBody>
</ns2:responseType>
</soapenv:Body>
</soapenv:Envelope>
This is a particular response which is generated when the transaction with the reference number is not available in the server.
I have received the soap request and evaluated it. However I have a problem with the return type of the soap response. I cannot generate a valid response. What type of response should a soap server return?
Things I have tried:
I have tried returning an xml string. But the soap client request throws an exception with the following message.
looks like we got no xml document.
I have also tried returing a native php SoapVar() with the same result.
I have tried returning an object response that is specified in the classmap fo the soap server.
e.g. for the example above, I have tried returning a ShowRemittanceDetailResponse object with the same result. (looks like we got no xml document).
I have tried returning a DomDocument Object . The exception thrown in this case is
the encoded object does not have a responseStr property.
I have tried returning an stdClass object with the same fields as the response expects with similar result.
Please help me.
Thanks in advance.
The solution I found was returning a SoapVar object. I didnt have soapui and the client I wrote in php was incorrect. Hence, I had problem verifying the returned xml as the php soap client was throwing an exception. The correct way for me was to return a SoapVar.

Issues parsing xml array from curl call

I am having serious issues parsing this xml array using curl. I only need the click_id printed on each new row. Does someone have an example of how I can pull this. I am using curl and get the response below in the variable $result. I am using php.
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfClick xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://cakemarketing.com/api/1/">
<Click>
<click_id>7458165</click_id>
<request_session_id>7901644</request_session_id>
<click_date>2011-09-08T13:53:37.143</click_date>
<offer_id>10346</offer_id>
<advertiser_id>1050</advertiser_id>
<campaign_id>6527</campaign_id>
</Click>
<Click>
<click_id>7459318</click_id>
<request_session_id>7903011</request_session_id>
<click_date>2011-09-08T14:41:37.953</click_date>
<offer_id>10346</offer_id>
<advertiser_id>1050</advertiser_id>
<campaign_id>6527</campaign_id>
</Click>
The best way is to use XPath with the help of this class http://php.net/manual/en/class.domxpath.php
A more simple method is to use regex (but I discourage this approach)

How can I Consume a Soap message with Namespace from salesforce in PHP

I am new to salesforce. Proficient in php but I haven't done any xml parsing. I have this file which I can consume. It is created when a salesforce object changes:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<notifications xmlns="http://soap.sforce.com/2005/09/outbound">
<OrganizationId>00D30000000opwSEAQ</OrganizationId>
<ActionId>04k30000000L6QPAA0</ActionId>
<SessionId xsi:nil="true"/>
<EnterpriseUrl>https://na1-api.salesforce.com/services/Soap/c/22.0/00D30000000opwS</EnterpriseUrl>
<PartnerUrl>https://na1-api.salesforce.com/services/Soap/u/22.0/00D30000000opwS</PartnerUrl>
<Notification>
<Id>04l3000000JbKClAAN</Id>
<sObject xsi:type="sf:Account" xmlns:sf="urn:sobject.enterprise.soap.sforce.com">
<sf:Id>0013000000ooziWAAQ</sf:Id>
<sf:BillingCity>New York</sf:BillingCity>
<sf:BillingCountry>US</sf:BillingCountry>
<sf:BillingPostalCode>10000</sf:BillingPostalCode>
<sf:BillingState>New York</sf:BillingState>
<sf:BillingStreet>302 E xxx St Apt C</sf:BillingStreet>
<sf:FN__Mapping_Status__c>Not Located Yet</sf:FN__Mapping_Status__c>
<sf:IsDeleted>false</sf:IsDeleted>
<sf:Member_Status__c>Active</sf:Member_Status__c>
<sf:Name>Joel Test</sf:Name>
</sObject>
</Notification>
</notifications>
</soapenv:Body>
</soapenv:Envelope>
I need to parse it so that I can take the values in the sf namespace and update a database. I can use SimpleXML to read stuff no in namespace, but I haven't been able to read namespace values. Can someone point me to example code or tutorial on how to do this?
It looks like that is a SOAP message from the Outbound Messaging feature in Salesforce. Instead of treating it as just XML, try using the native PHP SoapServer with the WSDL provided in Salesforce, as it is designed to handle SOAP messages like this. You can get the WSDL at Setup | Create | Workflow & Approvals | Outbound Messages | | Endpoint WSDL. You might also want to look at the Salesforce PHP Client, which uses the corresponding PHP SoapClient and has a utility for handling the sf: namespace and converting to a SObject object. It shouldn't be too hard to rewire the toolkit to act as a server instead of a client so you can handle the Outbound Messages.
PHP's SimpleXML will not work for reading namespace information. See this - http://www.w3schools.com/php/php_xml_simplexml.asp . Instead try something like this http://www.php.net/manual/en/function.xml-parser-create-ns.php
PHP's SimpleXML may work for the case. Just need to use children() and use the right syntax.
Therefore, to read the field value BillingCity in Salesforce Outbound Message, you may use following code:
$rcXML->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://soap.sforce.com/2005/09/outbound')->notifications->Notification->sObject->children('urn:sobject.enterprise.soap.sforce.com')->BillingCity
To help you understand how it works, I write a post with sames code and xml which shall be easier to understand.
http://amigotechnotes.wordpress.com/2013/11/16/parse-xml-with-namespace-by-simplexml-in-php/
Once you may read the filed value, you may store it to other database or text file with PHP.

Categories