Difference between two soap requests - php

My SOAP Request
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://ws.dgpys.deloitte.com" xmlns:ns2="ws.apache.org/namespaces/axis2">
<env:Header>
<ns2:ServiceGroupId>
<BOGUS>urn:uuid:7C2F61BDE7CB9D9C6D1424938568724</BOGUS>
</ns2:ServiceGroupId>
</env:Header>
<env:Body>
<ns1:getGunlukParametreRapor>
<date>2015-02-22T00:00Z</date>
</ns1:getGunlukParametreRapor>
</env:Body>
</env:Envelope>
Expected SOAP Request
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ws="http://ws.dgpys.deloitte.com">
<soap:Header>
<axis2:ServiceGroupId xmlns:axis2="http://ws.apache.org/namespaces/axis2">urn:uuid:479731898147E116AD1424691518968</axis2:ServiceGroupId>
</soap:Header>
<soap:Body>
<ws:getGunlukParametreRapor>
<date>2015-02-22T00:00Z</date>
</ws:getGunlukParametreRapor>
</soap:Body>
</soap:Envelope>
Tried with following codes:
$options = array(
'trace' => 1,
'exceptions' => 1,
'soap_version' => SOAP_1_2
);
$client = new SoapClient("http://dgpysws.pmum.gov.tr/dgpys/services/EVDServis.wsdl", $options);
$p1 = new stdCLass();
$p1->loginMessage = new stdCLass();
$p1->loginMessage->UserName = new stdCLass();
$p1->loginMessage->UserName->v = "Username";
$p1->loginMessage->Password = new stdCLass();
$p1->loginMessage->Password->v = "Passwor";
$client->login($p1);
$headers[] = new SoapHeader('http//ws.apache.org/namespaces/axis2', 'ServiceGroupId', "UNIQUEID", false);
$client->__setSoapHeaders($headers);
$result = $client->getGunlukParametreRapor(array('date' => '2015-02-22T00:00Z'));
Question is:
These SOAP requests are same?
I'm using SOAP_1_2 and it should be like Expected SOAP Request but my request doesnt looks like to expected format. Missing where?
How can i get the output like as expected?
Note: dgpysws.pmum.gov.tr wsdl address is private area.

They are not the same. To get rid of the BOGUS node you need to use this:
$strHeaderComponent_Session = "<SessionHeader><ServiceGroupId>$theVarWithTheIDGoesHere</ServiceGroupId></SessionHeader>";
$objVar_Session_Inside = new SoapVar($strHeaderComponent_Session, XSD_ANYXML,
null, null, null);
$objHeader_Session_Outside = new SoapHeader('http//ws.apache.org/namespaces/axis2',
'SessionHeader', $objVar_Session_Inside);
// More than one header can be provided in this array.
$client->__setSoapHeaders(array($objHeader_Session_Outside));

try the following
$ns = 'http//ws.apache.org/namespaces/axis2'; //Namespace of the WS.
//Body of the Soap Header.
$headerbody = array('ServiceGroupId' => $UNIQUEID_Token);
//Create Soap Header.
$header = new SOAPHeader($ns, 'axis2', $headerbody);
//set the Headers of Soap Client.
$soap_client->__setSoapHeaders($header);

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://ws.dgpys.deloitte.com" xmlns:ns2="ws.apache.org/namespaces/axis2">
<env:Header>
<ns2:ServiceGroupId>
urn:uuid:7C2F61BDE7CB9D9C6D1424938568724
</ns2:ServiceGroupId>
</env:Header>
<env:Body>
<ns1:getGunlukParametreRapor>
<date>2015-02-22T00:00Z</date>
</ns1:getGunlukParametreRapor>
</env:Body>
</env:Envelope>
And
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ws="http://ws.dgpys.deloitte.com">
<soap:Header>
<axis2:ServiceGroupId xmlns:axis2="http://ws.apache.org/namespaces/axis2">urn:uuid:479731898147E116AD1424691518968</axis2:ServiceGroupId>
</soap:Header>
<soap:Body>
<ws:getGunlukParametreRapor>
<date>2015-02-22T00:00Z</date>
</ws:getGunlukParametreRapor>
</soap:Body>
</soap:Envelope>
Are the same.
env=soap, ns2=ws and ns2=axis2. You can have any prefix to refer to these namespaces as you like. Once you assign the prefix you just refer to it using that in the other places. Only diff was the bogus tag tin first request. Just remove that.

Related

How do I access XML Data

I am using PHP to connect to a SOAP API using GuzzleHttp.
Code
$client = new Client();
$headers = [
'Host' => 'server',
'Content-Type' => 'application/soap+xml; charset=utf-8',
'SOAPAction' => 'action'
];
$body = '<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GetInventoryStatus xmlns="action">
<Credentials>
<Username></Username>
<Password></Password>
</Credentials>
<MaterialCode>' . $_GET['matcode'] . '</MaterialCode>
</GetInventoryStatus>
</soap12:Body>
</soap12:Envelope>';
$request = new Request('POST', 'web service here', $headers, $body);
$res = $client->sendAsync($request)->wait();
$xml1 = ($res->getBody());
After running the API, I get the result back in text but upon viewing the source code the below XML is getting returned back
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetInventoryStatusResponse
xmlns="http://localhost/EnterpriseWebService/Enterprise Connect">
<GetInventoryStatusResult>
<MaterialCode>10029</MaterialCode>
<MaterialDescription>12 PT TANGO C2S</MaterialDescription>
<QuantityOnHand>138000.00</QuantityOnHand>
<QuantityAllocated>20400.00</QuantityAllocated>
<QuantityAvailable>117600.00</QuantityAvailable>
<QuantityOnOrder>0.00</QuantityOnOrder>
<QuantityInProduction>0.00</QuantityInProduction>
<ReorderQuantity>0.00</ReorderQuantity>
<ReorderLevel>0.00</ReorderLevel>
<DesiredLevel>0.00</DesiredLevel>
</GetInventoryStatusResult>
</GetInventoryStatusResponse>
</soap:Body>
</soap:Envelope>
If I echo the $xml1, I get everything in text format.
How do I access the variables through the XML?
You need to parse it. I have used DOMDocument but one could use other techniques.
<?php
$str = '<' . '?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetInventoryStatusResponse
xmlns="http://localhost/EnterpriseWebService/EnterpriseConnect">
<GetInventoryStatusResult>
<MaterialCode>10029</MaterialCode>
<MaterialDescription>12 PT TANGO C2S</MaterialDescription>
<QuantityOnHand>138000.00</QuantityOnHand>
<QuantityAllocated>20400.00</QuantityAllocated>
<QuantityAvailable>117600.00</QuantityAvailable>
<QuantityOnOrder>0.00</QuantityOnOrder>
<QuantityInProduction>0.00</QuantityInProduction>
<ReorderQuantity>0.00</ReorderQuantity>
<ReorderLevel>0.00</ReorderLevel>
<DesiredLevel>0.00</DesiredLevel>
</GetInventoryStatusResult>
</GetInventoryStatusResponse>
</soap:Body>
</soap:Envelope>';
// $xml = simplexml_load_string($str);
$xml = new DOMDocument();
$xml->loadXML($str);
$xpath = new DOMXpath($xml);
$nodes = $xpath->query('//*');
$names = array();
foreach ($nodes as $node)
{
$names[] = [
'name' => $node->nodeName,
'value' => $node->textContent,
];
}
print_r($names);

parse the soap xml response to array

I had used curl to call the soap server and i got the response like this:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GenerateAuthPasswordResponse xmlns="NepalTelecom.AuthGateway">
<GenerateAuthPasswordResult>abcd-efgh</GenerateAuthPasswordResult>
<ResultCode>1</ResultCode>
</GenerateAuthPasswordResponse>
</soap:Body>
</soap:Envelope>
and when i tried to parse the soap xml as:
$response = $this->SoapModel->soapCall($xml , $this->vas_wsdl_url);
$obj = simplexml_load_string($response);
echo $obj;die();
[Note: where $response is the above soap response provided in soap xml]
and i get the $obj as some error like this:
Severity: Warning
Message: simplexml_load_string(): namespace warning : xmlns: URI NepalTelecom.AuthGateway is not absolute
please any body could help so fix this issue.
Thank you in advance.
Try this.
<?php
$xml = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GenerateAuthPasswordResponse >
<GenerateAuthPasswordResult>abcd-efgh</GenerateAuthPasswordResult>
<ResultCode>1</ResultCode>
</GenerateAuthPasswordResponse>
<GenerateAuthPasswordResponse >
<GenerateAuthPasswordResult>abcd-efgh</GenerateAuthPasswordResult>
<ResultCode>1</ResultCode>
</GenerateAuthPasswordResponse>
</soap:Body>
</soap:Envelope>';
$xml = simplexml_load_string($xml, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$ns = $xml->getNamespaces(true);
$soap = $xml->children($ns['soap']);
$res = $soap->Body->children();
print_r($res);

Assistance required with PHP and XML soap response

I'm junior to PHP and XML and require your assistance please.
How would I display only the values of the nodes in the xml response.
Example of what I would like to see:
name - Africa Direct
parent - 41711
invoiced - 1470002400
My code,
$wdsl = '*******';
$params = array(
'username'=>'********',
'password'=>'*******',
'parent'=>0
);
$options = array(
'trace'=>1
);
$client = new SoapClient($wdsl, $options);
$data = $client->__soapCall('get_customers', $params);
$response = $client->__getLastResponse();
var_export($response);
the results looks as follows:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<get_customersResponse xmlns="*********/API">
<s-gensym3>
<name xsi:type="xsd:string">Africa Direct</name>
<parent xsi:type="xsd:string">41711</parent>
<invoiced xsi:type="xsd:string">1470002400</invoiced>
<auto_topup xsi:type="xsd:float">0.000000</auto_topup>
<contact xsi:type="xsd:string">0</contact>
<invoices_due xsi:type="xsd:string">-2</invoices_due>
</s-gensym3>
</get_customersResponse>
</soap:Body>
</soap:Envelope>'

Formatting namespace in soap header using PHP

I need to construct an XML SOAP request header using PHP where the output looks like this:
<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:Header>
<AuthTokenHeader xmlns="SPP.net/ContractData">
<Token>B1912A8BA7AB6E56D688244D7B</Token>
</AuthTokenHeader>
</soap:Header>
....but my PHP is producing this instead:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="SPP.net/ContractData">
<SOAP-ENV:Header>
<ns1:AuthTokenHeader>
<Token>373161D1C28</Token>
</ns1:AuthTokenHeader>
</SOAP-ENV:Header>
Here is my PHP code:
// define namespace
$NameSpace = 'SPP.net/ContractData';
// build AuthTokenHeader vars
$authTokenVar[] = new SoapVar($TOKEN, XSD_STRING, null, null, 'Token');
// wrap in AuthTokenHeader tags
$header = new SoapVar($authTokenVar, SOAP_ENC_OBJECT, null, null, 'AuthTokenHeader');
// build soap header
$soapHeader = new SoapHeader($NameSpace, 'AuthTokenHeader', $header , false);
// instanciate soap client
$wsdlUrl= "https://gmwstest.sppinc.net/VPP/ContractExchange.asmx?wsdl";
$client = new SoapClient($wsdlUrl,
array(
"trace" => 1,
"exceptions" => 0,
"cache_wsdl" => 0));
// set soap headers
$client->__setSoapHeaders($soapHeader);
Here is the entire XML soap request I need to build:
<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:Header>
<AuthTokenHeader xmlns="SPP.net/ContractData">
<Token>BEB40F7FD43168017ACF3772A91F2B7C2B37A722F6421598D0B61CCD3DBF8928CA18B25970AB6D43FD31A761F15D84A52C9FC315BE9F708F30EE4F08DAB4C74F94894A7D3242102831FB8303684F7FE492A2249CB35FB50030C905DD91F771103C5A1D486CCEA9AAB0D42AC37252B2E350B4E3F24CEE8D0B8CFCE04F1DD7D1B4D7C2A4F1912A8BA7AB6E56D688244D7B</Token> <!--GET TOKEN FROM LOGONRESULT SESSION VARIABLE-->
</AuthTokenHeader>
</soap:Header>
<soap:Body>
<SendContractData xmlns="SPP.net/ContractData">
<contract>
<ContractID>VPP00253221</ContractID> <!--GET ContractID FROM CONTRACT RESPONSE SESSION VARIABLE-->
<AccountNumber/>
<Status>Pending</Status>
<Company>VPP</Company>
<ContractPrefix/>
<DealerCode>10456</DealerCode>
<FirstName>FirstName</FirstName>
<Mi/>
<LastName>Lastname</LastName>
<PhoneNumber>(312)980-6000</PhoneNumber>
<Address1>303 Anytown Lane</Address1>
<Address2>Unit A</Address2>
<City>Yourtown</City>
<StateProvinceAbbr xsi:type="StateEnum">IL</StateProvinceAbbr>
<ZipCode>60601</ZipCode>
<Country>USA</Country>
<OriginatingDepartment>Other</OriginatingDepartment>
<Language>English</Language>
<VIN>1HGCM56705AJMD227</VIN>
<PurchasePrice>2000.00</PurchasePrice>
<SalesTax>0.00</SalesTax>
<DownPayment>200.00</DownPayment>
<AmountFinanced>1800.00</AmountFinanced>
<PolicyType>ServiceContract</PolicyType>
<InServiceDate>2010-03-12T00:00:00</InServiceDate>
<FirstPaymentDate xsi:nil="true"/>
<DealerCost>500</DealerCost>
<ContractHoldersInitialsCoordinates xsi:nil="true"/>
<ContractHoldersInitialsDateCoordinates xsi:nil="true"/>
<ContractHoldersSignatureCoordinates xsi:nil="true"/>
<SignatureDateCoordinates xsi:nil="true"/>
<SellersSignatureCoordinates xsi:nil="true"/>
<PurchaseDate>2014-06-23T14:24:46</PurchaseDate>
<BeginMileage>43650</BeginMileage>
<TermMonths>84</TermMonths>
<TermMileage>100000</TermMileage>
<NumberOfPayments>12</NumberOfPayments>
<CallBackURL>http://NewWarrantyVehicle_Example/Success.php</CallBackURL>
</contract>
</SendContractData>
</soap:Body>
</soap:Envelope>
The two XML snippets you presented are logically the same. Both reference the XML namespace for the AuthTokenHeader element differently, but in terms of a correctly working XML parser they express the same.
You cannot change the way PHP creates the XML of a Soap request. If the created request does not work with the server you have to use, please be more specific what problems you encounter.

PHP sending SOAP request with SOAP client

It's my first time with SOAP, I already read all manuals and still stacked with the following example:
<?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:Header>
<AuthenticationInfo xmlns="https://blabla.com/ws/Quoting">
<strUserName>[username]</strUserName>
<strPassword>[password]</strPassword>
</AuthenticationInfo>
</soap:Header>
<soap:Body>
<GetQuotes xmlns="https://blabla.com/ws/Quoting">
<CallerClientID>0</CallerClientID>
<quoteRequest>
<CapacityCode>2</CapacityCode>
<BabiesCount>0</BabiesCount>
<QuotingOptions>0</QuotingOptions>
</quoteRequest>
<clientIDs>
<int>20295</int>
</clientIDs>
<withUrlParam>false</withUrlParam>
</GetQuotes>
</soap:Body>
</soap:Envelope>
I do not understand how to start the soapclient with authorization, please help.
You need to use SoapHeader. Below is the example.
$ns = 'https://blabla.com/ws/Quoting'; //Namespace of the WS.
//Body of the Soap Header.
$headerbody = array('strUserName' => $someUser,
'strPassword' => $somePass);
//Create Soap Header.
$header = new SOAPHeader($ns, 'AuthenticationInfo', $headerbody);
//set the Headers of Soap Client.
$soap_client->__setSoapHeaders($header);

Categories