Generate XML document with namespace in php - php

I want to create an XML document (with namesapace) as per structured below, using PHP. My objective is to create xml which will be pushed to server (using curl).
<?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>
<InsertData xmlns="http://tempuri.org/">
<userName>string</userName>
<password>string</password>
<costCenterCode>string</costCenterCode>
<consigneeName>string</consigneeName>
<consigneeAddress>string</consigneeAddress>
<consigneeMobNo>string</consigneeMobNo>
<consigneeEmail>string</consigneeEmail>
<originCityName>string</originCityName>
<destinationCityName>string</destinationCityName>
<pieces>string</pieces>
<weight>string</weight>
<codAmount>decimal</codAmount>
<custRefNo>string</custRefNo>
<productDetails>string</productDetails>
<fragile>string</fragile>
<services>string</services>
<remarks>string</remarks>
<insuranceValue>string</insuranceValue>
</InsertData>
</soap:Body>
</soap:Envelope>
Here is my code to create xml, but issue is that it does not add namespace with child and Web service server is giving me error as it is not structured as per their xml reference.
$xml = new SimpleXMLElement('<?xml version = "1.0" ?><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" />');
$body = $xml->addChild("Body");
$tcsAPI = $body->addChild('InsertData');
$tcsAPI->addChild('userName', "xxxxxx");
$tcsAPI->addChild('password', "xxxxxx");
$tcsAPI->addChild('costCenterCode', "CC001");
$tcsAPI->addChild('consigneeName', "MagDev");
$tcsAPI->addChild('consigneeAddress', "Address");
$tcsAPI->addChild('consigneeMobNo', "00000000");
$tcsAPI->addChild('consigneeEmail', "email#some.com");
$tcsAPI->addChild('originCityName', "ORG");
$tcsAPI->addChild('destinationCityName', "DEST");
$tcsAPI->addChild('pieces', "1");
$tcsAPI->addChild('weight', "1");
$tcsAPI->addChild('codAmount', "0");
$tcsAPI->addChild('custRefNo', "WEBT");
$tcsAPI->addChild('productDetails', "Test Product");
$tcsAPI->addChild('fragile', "0");
$tcsAPI->addChild('services', "0");
$tcsAPI->addChild('remarks', "Testing API services");
$tcsAPI->addChild('insuranceValue', "0");
Thanks

The methods have an additional argument that allows you to specify the namespace. This will add the namespace definitions (the xmlns attributes) as needed.
$xmlns = [
'soap' => 'http://www.w3.org/2003/05/soap-envelope',
't' => 'http://tempuri.org/'
];
$xml = new SimpleXMLElement(
'<?xml version = "1.0" ?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.w3.org/2003/05/soap-envelope" />'
);
$body = $xml->addChild("soap:Body", null, $xmlns['soap']);
$tcsAPI = $body->addChild('InsertData', null, $xmlns['t']);
//...
echo $xml->asXml();
You can add the namespace definitions manually like attributes. But this is only needed to avoid multiple definitions on descendants. If an ancestor already defines the namespace the descendant doesn't need to do this any more.
Be aware that attributes are only in a namespace if they have an prefix, default namespaces are only valid for elements. Attributes without a prefix are always in the 'empty' namespace.

$tcsAPI->addAttribute('xmlns', 'http://tempuri.org/');

Related

Need to delete ns1: from soap call to the server

I use SOAP as a method to communicate with the asp.net service. The problem is that I need to pass the XML without ns1: .
This is the request I send now:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://ex.ex.com/">
<SOAP-ENV:Body>
<ns1:GetInfoFromSending>
<ns1:Key>XXX</ns1:Key>
<ns1:ObjectID>2468</ns1:ObjectID>
</ns1:GetInfoFromSending>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
But the format of the request needed is this:
<?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>
<GetInfoFromSendingList xmlns="http://ex.ex.com/">
<Key>string</Key>
<ObjectID>int</ObjectID>
</GetInfoFromSendingList>
</soap:Body>
</soap:Envelope>
I tried using MSSoapClient from PHP Manual but doesn't seem to work
try {
$soap = new MSSoapClient($wsdl, $options);
var_dump($soap);
$data = $soap->__soapCall('GetInfoFromSending', array('parameters'=>$params));
var_dump($soap);
}
catch(Exception $e) {
die($e->getMessage());
}
What is $namespace = "http://tempuri.com"; in MSSoapClient
Resolved by using the default __soapCall from SOAP.

Wrong SOAP Tags in PHP SOAP Request

I'm trying to send an SOAP request.
My request looks like this
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="someurl">
<SOAP-ENV:Body>
<RequestIndBApplication>
But my customer told me it has to look 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:Body>
<process xmlns="someurl">
<RequestIndBApplication>
I'm using an WSDL file i got from my customer. But i don't know how i can change the soap tag. is there a way to do this?
Here is my php code
$client = new SoapClient('urlofwsdlfile',array('trace' => true, 'exceptions' => true)); $params = new \SoapVar($xmlstr), \XSD_ANYXML);
$result = $client->__SoapCall('process', array($params));
$xmlstr contains the data as xml

Set custom SoapServer response's namespace

I am creating Soap server side.
Is there a way to change default namespace for soap envelope, body and header tags?
To proceed requests I'm using following code:
ini_set( "soap.wsdl_cache_enabled", "0");
$server = new SoapServer('myWsdl.wsdl');
$server->addFunction("GetDetails");
$server->handle();
function GetDetails(){
$body_xml = '<GetDetailsResponse>
<Name>My name</Name>
</GetDetailsResponse>';
$result = new SoapVar ($body_xml, XSD_ANYXML);
return $result;
}
This is what I get in response:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<GetDetailsResponse>
<Name>My name</Name>
</GetDetailsResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
This is what I want to get in response:
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetDetailsResponse>
<Name>My name</Name>
</GetDetailsResponse>
</s:Body>
</s:Envelope>

PHP - How to generate Soap request XML from object

I have an std object :
$args = new stdClass();
$args->userName = 'username';
$args->password = 'password';
$args->criteria->customer->customerId = '0002003';
I want create SoapRequest XML :
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="wsclient.dms.tecsys.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<mns1:search xmlns:mns1="wsclient.dms.tecsys.com">
<arg0>
<userName>username</userName>
<password>password</password>
<criteria>
<customer>
<customerId>0002003</customerId>
</customer>
</criteria>
</arg0>
</mns1:search>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
But i can't find solution. Can any one help me.
Regards
By using the SoapClient class you can create a soap request easily
Take a look at this
SoapClient: __doRequest — Which performs a SOAP request

Add namespace Prefix in Nusoap PHP

I'm using nusoap v 1.123, I'm trying to add the prefixes urn in my generated request of nusoap
This is what I am expecting
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:urn="urn:mnop:acceptor:contract">
<soapenv:Header>
<urn:authenticationHeader>
<urn:name>abctest</urn:name>
<urn:userid>dddd</urn:userid>
<urn:pass>errerere</urn:pass>
</urn:authenticationHeader>
</soapenv:Header>
and this is what the code is generating
<SOAP-ENV:Header>
<authenticationHeader>
<name>abctest</urn:name>
<userid>dddd</urn:userid>
<pass>errerere</urn:pass>
</authenticationHeader>
</SOAP-ENV:Header>
$header = new SoapHeader($wsdl, 'authenticationHeader',$headerParam);
$soapAction='urn:mnop:acceptor:contract';
$client = new nusoap_client($wsdl);
$client->setHeaders($headerParam);
$data=$client->call('myoperation',$bodyParam,$namespace,$soapAction,null,null,'document', 'literal');
the urn prefix is missing and SOAP-ENV: need to be replaced with soapenv: , kindly tell me to resolve this issue.
Thanks
this is how I solved this problem
$bodyParam = array('urn:operationName'=>array(
'urn:amount'=>'23232',
'urn:automati'=>'monUrl',
'urn:context'=>'',
'urn:currencyCode'=>'978',
'urn:customerId'=>'',
'urn:customerIpAddress'=>'',
'urn:customerLanguage'=>'fr',
));
Solved this problem, nusoap parameters must be configured
add global name space
$client->namespaces['tem'] = "http://tempuri.org/";
and add local namespace for elements
$responseService = $client->call(
'CAV_GET_DATA',
$param,
$namespace = 'tem'
);
Resolve
<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tem="http://tempuri.org/" xmlns:ns6704="tem"><SOAP-ENV:Body><tem:CAV_GET_DATA><tem:UsuarioID>example</tem:UsuarioID><tem:Password>example</tem:Password><tem:idEmpleado>000000</tem:idEmpleado></tem:CAV_GET_DATA></SOAP-ENV:Body></SOAP-ENV:Envelope>

Categories