Creating a SOAP call using PHP with an XML body - php

I'm trying to call a SOAP method using PHP.
Here's the code I've got:
$data = array('Acquirer' =>
array(
'Id' => 'MyId',
'UserId' => 'MyUserId',
'Password' => 'MyPassword'
));
$method = 'Echo';
$client = new SoapClient(NULL,
array('location' => 'https://example.com/ExampleWebServiceDL/services/ExampleHandler',
'uri' => 'http://example.com/wsdl', 'trace' => 1));
$result = $client->$method($data);
Here's the request it creates:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://example.com/wsdl" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<ns1:Echo>
<param0 xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">Acquirer</key>
<value xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">Id</key>
<value xsi:type="xsd:string">mcp</value>
</item>
<item>
<key xsi:type="xsd:string">UserId</key>
<value xsi:type="xsd:string">tst001</value>
</item>
<item>
<key xsi:type="xsd:string">Password</key>
<value xsi:type="xsd:string">test</value>
</item>
</value>
</item>
</param0>
</ns1:Echo>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
And here's what I want the request to look like:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://example.com/wsdl" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<Echo>
<Acquirer>
<Id>MyId</Id>
<UserId>MyUserId</UserId>
<Password>MyPassword</Password>
</Acquirer>
</Echo>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

There are a couple of ways to solve this. The least hackiest and almost what you want:
$client = new SoapClient(
null,
array(
'location' => 'https://example.com/ExampleWebServiceDL/services/ExampleHandler',
'uri' => 'http://example.com/wsdl',
'trace' => 1,
'use' => SOAP_LITERAL,
)
);
$params = new \SoapVar("<Acquirer><Id>MyId</Id><UserId>MyUserId</UserId><Password>MyPassword</Password></Acquirer>", XSD_ANYXML);
$result = $client->Echo($params);
This gets you the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://example.com/wsdl">
<SOAP-ENV:Body>
<ns1:Echo>
<Acquirer>
<Id>MyId</Id>
<UserId>MyUserId</UserId>
<Password>MyPassword</Password>
</Acquirer>
</ns1:Echo>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
That is almost exactly what you want, except for the namespace on the method name. I don't know if this is a problem. If so, you can hack it even further. You could put the <Echo> tag in the XML string by hand and have the SoapClient not set the method by adding 'style' => SOAP_DOCUMENT, to the options array like this:
$client = new SoapClient(
null,
array(
'location' => 'https://example.com/ExampleWebServiceDL/services/ExampleHandler',
'uri' => 'http://example.com/wsdl',
'trace' => 1,
'use' => SOAP_LITERAL,
'style' => SOAP_DOCUMENT,
)
);
$params = new \SoapVar("<Echo><Acquirer><Id>MyId</Id><UserId>MyUserId</UserId><Password>MyPassword</Password></Acquirer></Echo>", XSD_ANYXML);
$result = $client->MethodNameIsIgnored($params);
This results in the following request XML:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<Echo>
<Acquirer>
<Id>MyId</Id>
<UserId>MyUserId</UserId>
<Password>MyPassword</Password>
</Acquirer>
</Echo>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Finally, if you want to play around with SoapVar and SoapParam objects, you can find a good reference in this comment in the PHP manual: http://www.php.net/manual/en/soapvar.soapvar.php#104065. If you get that to work, please let me know, I failed miserably.

First off, you have to specify you wish to use Document Literal style:
$client = new SoapClient(NULL, array(
'location' => 'https://example.com/path/to/service',
'uri' => 'http://example.com/wsdl',
'trace' => 1,
'use' => SOAP_LITERAL)
);
Then, you need to transform your data into a SoapVar; I've written a simple transform function:
function soapify(array $data)
{
foreach ($data as &$value) {
if (is_array($value)) {
$value = soapify($value);
}
}
return new SoapVar($data, SOAP_ENC_OBJECT);
}
Then, you apply this transform function onto your data:
$data = soapify(array(
'Acquirer' => array(
'Id' => 'MyId',
'UserId' => 'MyUserId',
'Password' => 'MyPassword',
),
));
Finally, you call the service passing the Data parameter:
$method = 'Echo';
$result = $client->$method(new SoapParam($data, 'Data'));

Related

SoapRequest formatting in PHP native soapClient

Weird situation using SoapClient
I have the following code to fire a simple soap request.
$client = new \SoapClient($this->wsdlURI, [
'trace' => 1,
'cache_wsdl' => WSDL_CACHE_NONE,
'soap_version' => 'SOAP_1_2',
'features' => SOAP_SINGLE_ELEMENT_ARRAYS|SOAP_USE_XSI_ARRAY_TYPE
]);
$request = [
'dataRequest' => [
'UserId' => '***',
'AccountNumber' => (string) $customer->getAccountNumber(),
'PremiseAddress' => [
'ZipCode' => (string) $customer->getZip()
]
]
];
try {
$return = $client->searchAccounts($request);
} catch (\SoapFault $fault) {
...
}
I would expect the request to look like this:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://com/dom/ec/services" xmlns:java="java:com.dom.ec.beans">
<soapenv:Header/>
<soapenv:Body>
<ser:searchAccounts>
<ser:dataRequest>
<java:UserId>***</java:UserId>
<java:AccountNumber>00000000</java:AccountNumber>
<java:PremiseAddress>
<java:ZipCode>00000</java:ZipCode>
</java:PremiseAddress>
</ser:dataRequest>
</ser:searchAccounts>
</soapenv:Body>
</soapenv:Envelope>
It instead or formatted like this:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://cso.dom.com/ecservices/">
<SOAP-ENV:Body>
<ns1:SearchAccounts>
<ns1:dataRequest>
<item>
<key>UserId</key>
<value>***</value>
</item>
<item>
<key>AccountNumber</key>
<value>0000000000</value>
</item>
<item>
<key>PremiseAddress</key>
<value>
<item>
<key>ZipCode</key>
<value>00000</value>
</item>
</value>
</item>
</ns1:dataRequest>
</ns1:SearchAccounts>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Is there an option I am missing?
What I realized I was missing was the judicious use of SoapVar as well using StdClass rather than associative arrays.
Final code looked like this:
$client = new \SoapClient($this->wsdlURI, [
'trace' => 1,
'cache_wsdl' => WSDL_CACHE_NONE,
'soap_version' => 'SOAP_1_2',
'features' => SOAP_SINGLE_ELEMENT_ARRAYS|SOAP_USE_XSI_ARRAY_TYPE
]);
$premiseAddress = new \stdClass();
$premiseAddress->ZipCode = (string) $customer->getZip();
$dataRequest = new \stdClass();
$dataRequest->AccountNumber = (string) $customer->getAccountNumber();
$dataRequest->CompanyNumber = '04';
$dataRequest->UserId = 'EFI';
$dataRequest->PremiseAddress = new \SoapVar($premiseAddress, SOAP_ENC_OBJECT);
$searchRequest = new \stdClass();
$searchRequest->dataRequest = new \SoapVar($dataRequest, SOAP_ENC_OBJECT);

SOAP HEADERS error

I'm having some problems by making my SOAP headers:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="elementos" xmlns:ns2="elementoscomunes">
<SOAP-ENV:Header>
<ns2:CABECERA>
<item>
<key>Element</key>
<value>
<item>
<key>Key</key>
<value/>
</item>
<item>
<key>Values</key>
<value>
<item>
<key>Value</key>
<value/>
</item>
</value>
</item>
</value>
</item>
</ns2:CABECERA>
So, ITEM tags are added because the namespace must not be found, i've tried to change the namespace root, but i think this is not the solution..
My service is under ssl, does all namespaces must be under ssl?
Can the tag be the error code explanation?
A PHP Error was encountered
Severity: Warning
Message: SoapClient::__doRequest(): SSL: Connection reset by peer
AND
THE SOAP FAULT:
SoapFault Object
(
[message:protected] => Error Fetching http headers
My original .wdsl has the next header definition:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:elem="elementoscomunes" xmlns:elem1="elementos">
<soapenv:Header>
<elem:CABECERA>
<!--Zero or more repetitions:-->
<Element>
<Key>?</Key>
<Values>
<!--Zero or more repetitions:-->
<Value>?</Value>
</Values>
</Element>
</elem:CABECERA>
</soapenv:Header>
</elem:CABECERA>
</soapenv:Header>
Does i have to add a new namespace?
I'm on an Apache Server so key tag must be "Key" i generate the code as shown below:
$headerbody = array('Element' => array('Key' => '', 'Values' => array('Value' => '')));
$header = new SOAPHeader('namespace', 'CABECERA', $headerbody );
$sClient->__setSoapHeaders($header);
Any Suggestions?
Thanks you!
I think it's too old, but my solution to do this was:
just replace this:
$headerbody = array('Element' => array('Key' => '', 'Values' => array('Value' => '')));
with this:
$headerbody = (object) array('Element' => array('Key' => '', 'Values' => array('Value' => '')));
Hope this help.!
When using php soap client sending data to the header or the body will result in the addition of item tags if the data is an array.
i.e.
$this->header(NAMESPACE, 'abc', [
'animal' => 'dog,
]);
becomes
<SOAP-ENV:Header>
<ns1:abc>
<item>
<key>animal</key>
<value>dog</value>
</item>
</ns1:abc>
</SOAP-ENV:Header>
Sending an object
$this->header(NAMESPACE, 'abc', (object)[
'animal' => 'dog,
]);
becomes
<SOAP-ENV:Header>
<ns1:abc>
<animal>dog</animal>
</ns1:abc>
</SOAP-ENV:Header>

How do I create this xml with PHP's SoapClient?

From debugging another application, I found that it sends the following xml to a soap server (code parts in this example are minimized, the xml is about 200 lines long:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:m0="http://schemas.ccs.nl/datacatalogus/modellen/modelrelatie">
<SOAP-ENV:Header>
<m:header xmlns:m="http://schemas.ccs.nl/soap">
<m:account>account</m:account>
<m:naam>naam_header</m:naam>
<m:wachtwoord>wachtwoord</m:wachtwoord>
<m:bedrijfsnummer>bedrijfsnummer</m:bedrijfsnummer>
<m:tussenpersoonnummer>tussenpersoonnummer</m:tussenpersoonnummer>
</m:header>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<m:RelatieMuteren xmlns:m="http://schemas.ccs.nl/services/relatieservice">
<m:relatie pc="W">
<m0:adres>adres</m0:adres>
</m:relatie>
</m:RelatieMuteren>
</SOAP-ENV:Body>
Obviously, the part between <m0:adres></m0:adres> is a lot larger and corresponds to my data that is stored in an array. However, if I try to send the request using __soapCall, PHP builds the following request:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://schemas.ccs.nl/datacatalogus/modellen/modelrelatie"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns2="http://schemas.ccs.nl/services/relatieservice" xmlns:ns3="http://schemas.ccs.nl/soap">
<SOAP-ENV:Header>
<ns3:header>
<ns3:account>account</ns3:account>
<ns3:naam>naam</ns3:naam>
<ns3:wachtwoord>wachtwoord</ns3:wachtwoord>
<ns3:bedrijfsnummer>bedrijfsnummer</ns3:bedrijfsnummer>
<ns3:tussenpersoonnummer>tussenpersoonnummer</ns3:tussenpersoonnummer>
</ns3:header>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns2:RelatieMuteren>
<ns2:relatie pc="I">
<ns1:adres>Postbus 53</ns1:adres>
</ns2:relatie>
</ns2:RelatieMuteren>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
As you can see, the second xml is quite different from the first. Can anyone explain me why it is different and how I can create the first type of xml?
I use the following code to do the request:
$client = new SoapClient( "http://www.cdsverzekeringen.nl/ws_prod/services/RelatieService.asmx?WSDL", array( 'trace' => 1 ) );
$aHeader = array(
'account' => "PRIVATE",
'naam' => "PRIVATE",
'wachtwoord' => "PRIVATE",
'bedrijfsnummer' => "PRIVATE",
'tussenpersoonnummer' => "PRIVATE",
);
$client->__setSoapHeaders( new SoapHeader( "http://schemas.ccs.nl/soap", 'header', $aHeader ) );
$vtResult = $client->__soapCall( "RelatieMuteren", array( $aRelatieInfo ) );
The $aRelatieInfo array is formatted like this:
array
'relatie' =>
array
'adres' => string 'Postbus 53' (length=10)
I hope someone can help me out. Thanks in advance!
How about using SoabVar API like this,
$aHeader = '
<m:header xmlns:m="http://schemas.ccs.nl/soap">
<m:account>account</m:account>
<m:naam>naam_header</m:naam>
<m:wachtwoord>wachtwoord</m:wachtwoord>
<m:bedrijfsnummer>bedrijfsnummer</m:bedrijfsnummer>
<m:tussenpersoonnummer>tussenpersoonnummer</m:tussenpersoonnummer>
</m:header>';
$soap_var_header = new SoapVar( $aHeader , XSD_ANYXML, null, null, null);
$client->__setSoapHeaders( new SoapHeader( "http://schemas.ccs.nl/soap", 'm', $soap_var_header ) );

Can't figure out how to create an XML SOAP Post in PHP

I searched for examples and more, but it won't work. I don't know how i can create the piece of "POST code" for my xml message. I think i need a soapclient but i dont exactly know how i use one.
The WSDL file address: https://secure.intelly.nl/webservices/intellymodule001.asmx?WSDL
This is the xml message i created:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:tns="http://extranet.intelly.nl/module001/"
xmlns:s="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<SOAP-ENV:Body>
<tns:GetDomains xmlns:tns="http://extranet.intelly.nl/module001/">
<tns:credentials>
<ExternalProgramFunction>***</ExternalProgramFunction>
<ExternalProgramID>***</ExternalProgramID>
<Domain>***</Domain>
<Username>***</Username>
<Password>***</Password>
</tns:credentials>
<tns:message>
</tns:message>
<tns:message>
</tns:message>
<tns:searchParameters>
<OnlyActive>True</OnlyActive>
</tns:searchParameters>
</tns:GetDomains>
</SOAP-ENV:Body>
PHP has a native SoapClient, below is an example using your input structure:
$client = new SoapClient('https://secure.intelly.nl/webservices/intellymodule001.asmx?WSDL');
$params = array(
'credentials' => array(
'ExternalProgramFunction' => 'xxx',
'ExternalProgramID' => 'xxx',
'Domain' => 'xxx',
'UserName' => 'xxx',
'Password' => 'xxx'
),
'message' => array(),
'searchParameters' => array(
'OnlyActive' => true
)
);
$response = $client->GetDomains($params);
print_r($response);

How to define a SoapVar namespace?

I need to have this node in my SOAP Request (using 1.1):
<CredentialsHeader xmlns="http://www.url.com/Services/P24ListingService11"
<EMail>ricky#email.net</EMail>
<Password>password</Password>
</CredentialsHeader>
So I have the following PHP:
$client = new SoapClient("https://exdev.www.example.com/Services/example.asmx?WSDL",
array(
"trace" => 1,
"exceptions" => 0,
"cache_wsdl" => 0,
'soap_version' => SOAP_1_1
)
);
$CredentialObject = new SoapVar(array('EMail' => 'ricky#email.net', 'Password' => 'password'), SOAP_ENC_OBJECT);
Which generates:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.example.com/Services/Example">
<SOAP-ENV:Header>
<ns1:CredentialsHeader>
<EMail>ricky#email.net</EMail>
<Password>password</Password>
</ns1:CredentialsHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:EchoAuthenticated/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
All I need to do is prevent it using ns1 and actually define the xmlns in the node like so:
<CredentialsHeader xmlns="http://www.example.com/Services/Example">
<EMail>ricky#email.net</EMail>
<Password>password</Password>
</CredentialsHeader>
I have tested that in Firefox Poster and know for a fact that change fixes the problem.
$CredentialObjectXML = '<CredentialsHeader xmlns="http://www.example.com/Services/Example">
<EMail>'.$UserName.'</EMail>
<Password>'.$Password.'</Password>
</CredentialsHeader>';
$CredentialObject = new SoapVar($CredentialObjectXML,XSD_ANYXML);
This way you can directly use the XML with Type XSD_ANYXML.
Hope this will resolve your problem.
http://www.php.net/manual/tr/soapvar.soapvar.php
Parameter "node_namespace" is what you've been looking for i guess.
I had the same problem and found out that if you map a dummy class to the credential complex type from your WSDL, PHP will output something like:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.example.com/Services/Example">
<SOAP-ENV:Header>
<ns1:CredentialsHeader>
<ns1:EMail>ricky#email.net</ns1:EMail>
<ns1:Password>password</ns1:Password>
</ns1:CredentialsHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:EchoAuthenticated/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
This is not exactly what was requested but although more verbose, it is equivalent.
The code goes like this:
$client = new SoapClient("https://exdev.www.example.com/Services/example.asmx?WSDL",
array(
"trace" => 1,
"exceptions" => 0,
"cache_wsdl" => 0,
"soap_version" => SOAP_1_1,
"classmap" => array(
'credential_complex_type' => 'CredentialObject',
),
)
);
class CredentialObject {}
$credentialObject = new CredentialObject();
$credentialObject->Email = 'ricky#email.net';
$credentialObject->Password = 'password';

Categories