Multidimensional Array with PHP Soap Client - php

I'm trying to create a SOAP Client, but I need to send a multi-dimensional array (I think) as requested in the documentation, example below:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.senior.com.br">
<soapenv:Body>
<ser:ColaboradoresAdmitidos>
<user>String</user>
<password>String</password>
<encryption>Integer</encryption>
<parameters>
<NumEmp>Integer</NumEmp>
<AbrTipCol>String</AbrTipCol>
<IniPer>DateTime</IniPer>
<FimPer>DateTime</FimPer>
</parameters>
</ser:ColaboradoresAdmitidos>
</soapenv:Body>
</soapenv:Envelope>
I create a array like this:
$arguments = [
'user' => 'xxxxx',
'password' => 'xxxxxxxxxx',
'encryption' => 0,
'parameters' => array(
'NumEmp' => 1,
'AbrTipCol' => '1',
'IniPer' => '01/01/2019',
'FimPer' => null
),
];
$client = new SoapClient($url, array('trace' => 1));
dd($arguments, $client->__soapCall("colaboradoresAdmitidos", $arguments), $client->__getLastRequest());
However in the request, it seems that it does not accept the array, in fact I did some tests, and in addition to the first 3 parameters, but none enter the generated XML.
I already made the array in different ways, and changed the parameter value, but it always returns as below:
<?xml version="1.0" encoding="UTF-8"?>\n
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://services.senior.com.br">
<SOAP-ENV:Body>
<ns1:ColaboradoresAdmitidos>
<user>xxxxxxx</user>
<password>xxxxxxxxx</password>
<encryption>0</encryption>
<parameters/>
</ns1:ColaboradoresAdmitidos>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Someone could help me, because the server understands that the parameter is missing, so I can't execute the query.

Searching the internet, I discovered a lot of people with this problem and who couldn't solve it. I discovered the error.
This happens when the parameters do not match those registered in the WSDL. in my case: the documentation mentions the parameters with a capital letter, however, I had to use it with a small letter.
It seems that the parameter key has to be the same value as the one registered in wsdl.
ex:
$arguments = [
'user' => 'xxxxx',
'password' => 'xxxxxxxxxx',
'encryption' => 0,
'parameters' => array(
'numEmp' => 1,
'abrTipCol' => '1',
'iniPer' => '01/01/2019',
'fimPer' => null
)];

Related

PHP SOAPCall Input is null

I'm getting the following error:
Fatal error: Uncaught SoapFault exception: [s:Client] Service
operation Pickup_Cancel failed due to validation errors: Input is null
Here's my code:
$client = new SoapClient("https://etrack.postaplus.net/APIService/PostaWebClient.svc?singleWsdl", array("trace" => 1, "exception" => 0));
$params = array(
"CodeStation" => `BEY`,
"PickupNumber" => `1`,
"Reason" => `test reason`,
"Password" => `sss`,
"ShipperAccount" => `acc`,
"UserName" => `acc`,
);
$client->Pickup_Cancel($params);
The awnser to the question you didn't ask is probably: replace the backticks (`) by singlequotes (')
-- Edit. That was not the problem.
Here is the case. SOAP can be a pain to get going. My experience is to use a good soap class or just non-wsdl mode. Read up on this in the docs: https://www.php.net/manual/en/soapclient.soapcall.php
So don't do this:
$return = $soapClient->functionName($data);
But this:
$return = $soapClient->__SoapCall('functionName', $data);
Also get a grip on what the server wants, load the WSDL url into a client like SoapUI https://www.soapui.org/ (its free). This lets you check if the SoapServer works and how you should approach it with your call.
In your case the WSDL states this:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:pos="http://schemas.datacontract.org/2004/07/PostaWebClient">
<soapenv:Header/>
<soapenv:Body>
<tem:Pickup_Cancel>
<!--Optional:-->
<tem:CLIENTINFO>
<!--Optional:-->
<pos:CodeStation>asd</pos:CodeStation>
<!--Optional:-->
<pos:Password>asd</pos:Password>
<!--Optional:-->
<pos:ShipperAccount>asd</pos:ShipperAccount>
<!--Optional:-->
<pos:UserName>asd</pos:UserName>
</tem:CLIENTINFO>
<!--Optional:-->
<tem:PickupNumber>asd</tem:PickupNumber>
<!--Optional:-->
<tem:Reason>asd</tem:Reason>
</tem:Pickup_Cancel>
</soapenv:Body>
</soapenv:Envelope>
Which translates to this PHP code:
$client = new SoapClient("https://etrack.postaplus.net/APIService/PostaWebClient.svc?singleWsdl");
$params = [
'Pickup_Cancel' => [
'CLIENTINFO' => [
'Password' => 'sss',
'ShipperAccount' => 'acc',
'UserName' => 'acc',
'CodeStation' => '',
],
'PickupNumber' => '',
'Reason' => '',
],
];
$client->__SoapCall("Pickup_Cancel", $params);
See how the array matches the WSDL xml format?
This should also give you the feedback you need to proceed. If not, its probably time to get 'real' help :) Goodluck.

How to deal with abstract types in WSDL-enabled SoapClient?

I need to handle external SOAP service with WSDL. Example of valid request (simplified) is:
<?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>
<RunCommand xmlns="https://me.com/MyService">
<request>
<Credentials>
<User>jack</User>
<Password>abc123</Password>
</Credentials>
<Command xsi:type="SomeCommand">
<Foo>bar</Foo>
</Command>
</request>
</RunCommand>
</soap:Body>
</soap:Envelope>
The problem is that according to WSDL, Command element is of abstract CommandType type, so I need to specify xsi:type. I know how to do it with SoapVar, but it seems like if I use SoapVar, all WSDL-mode automation is gone.
For simple WSDLs, that would be enough:
$soapClient->RunCommand([
'Request' => [
'Credentials' => [
'user' => 'jack',
'password' => 'abc123',
],
'Command' => [
'Foo' => 'bar'
]
]
]);
But in this case, I'm getting an exception:
The specified type is abstract: name='CommandType'
I know how to make Command element for SoapClient, but I'm not sure how to mix it all together. I tried this:
$soapClient->RunCommand([
'Request' => [
'Credentials' => [
'user' => 'jack',
'password' => 'abc123',
],
new SoapVar(
['Foo' => 'bar'],
SOAP_ENC_OBJECT,
'CommandType',
null,
'Command'
)
]
]);
But it doesn't create a request I need. How can I form the request I need with SoapClient to keep as much WSDL goodies as possible?

php soapclient Array to string conversion

I have an array a couple of levels deep in my SOAP request like below. When I run my SoapRequest I get Notice (8): Array to string conversion and my XML response does not convert the Array in RTrans to XML and I have no idea why. How I am creating the SOAP request and the XML version of it can be found below.
The Request:
$r['request'] = array(
'request' => array(
'user' => 'test',
'password' => 'test',
'RTrans' => array(
'Transactions' => array(
'Criteria' => array(
'Name' => 'Thomas'
)
)
)
)
);
try{
$response = $this->apiClient->DoQuery($r);
}
catch(Exception $e){
debug($e);
}
The XML Version
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://webServices/">
<SOAP-ENV:Body>
<ns1:DoNormalEnquiry>
<request>
<username>test</usernmae>
<password>test</password>
<RTrans>Array</RTrans>
</request>
</ns1:DoNormalEnquiry>
</SOAP-ENV:Body>
I think RTrans is defined as a String. Please have a look at the wsdl file.
Maybe thats the reason you got "Array" in the xml.
To send an array to your soapservice you could convert it to json.
json_encode( array('Transactions' => array('Criteria' => array('Name' => 'Thomas')));
or define a complex datatype.
Maybe SoapVar will help you.

Problems with soap php call because of xml error

i'm trying to make a soap call to a web service to pass shipping data. Here is the request i'm sending so far:
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://weblabeling.gls-italy.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:enc="http://www.w3.org/2003/05/soap-encoding">
<env:Body>
<ns1:AddParcel env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<!-- bof shipping array --->
<XMLInfoParcel>
<Info>
<SedeGls>XXXX</SedeGls>
<CodiceClienteGls>XXXXX</CodiceClienteGls>
<PasswordClienteGls>XXXXXX</PasswordClienteGls>
<Parcel>
<CodiceContrattoGls>XXXXXX</CodiceContrattoGls>
<RagioneSociale>XXXXXX</RagioneSociale>
<!-- other stuff here -->
</Parcel>
</Info>
</XMLInfoParcel>
<!-- eof shipping array --->
</ns1:AddParcel>
</env:Body>
</env:Envelope>
this is instead what webservice is asking me
<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>
<AddParcel xmlns="http://weblabeling.gls-italy.com/">
<XMLInfoParcel>string</XMLInfoParcel>
</AddParcel>
</soap12:Body>
</soap12:Envelope>
I always get as response xml format error.
What is wrong?
Shipping array is correct and match perfectly the request one.
Thanks in advance.
Edited:
Here is how i build xml:
//inseriamo i dati nei corretti array
$Label = array(
'XMLInfoParcel' => array(
'Info' => array(
'SedeGls' => $SedeGls,
'CodiceClienteGls' => $CodiceClienteGls,
'PasswordClienteGls' => $PasswordClienteGls,
'Parcel' => array(
'CodiceContrattoGls' => $cod_cont,
'RagioneSociale' => $rag_soc,
'Indirizzo' => $delivery_indirizzo,
'Localita' => $delivery_city,
'Zipcode' => $data['delivery_postcode'],
'Provincia' => $data['zone_code'],
'Bda' => '',
'DataDocumentoTrasporto' => '',
'Colli' => '1',
'Incoterm' => '',
'PesoReale' => '1,00',
'ImportoContrassegno' => $imp_cont,
'NoteSpedizione' => $data['customers_telephone'],
'TipoPorto' => 'F',
'Assicurazione' => $ass_ins,
'PesoVolume' => '',
'TipoCollo' => $tipo_collo,
'FrancoAnticipata' => '',
'RiferimentoCliente' => '',
'NoteAggiuntive' => '',
'CodiceClienteDestinatario' => '',
'Email' => '',
'Cellulare1' => $telefono_1,
'Cellulare2' => '',
'ServiziAccessori' => '',
'ModalitaIncasso' => $mod_inc
),),),
);
$dom = new DOMDocumentExt('1.0', 'utf-8');
$chiamta = $dom->loadArray($Label);
$dudy = $dom->saveXML();
#Iserni i put ---> only here, this is not in my code, i'm going to Test the online tool, i really don't see errors.
I still get error of xml format, any other clue guys?
Edited:
I just noticed my request has:
<ns1:AddParcel env:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
while the required is:
<AddParcel xmlns="http://weblabeling.gls-italy.com/">
So i noticed also in envelope there is something different with namespaces.
Could it be the problem?
WSDL seems to not work on gls server, they did not provide any info in documentation about it.
Edited:
Maybe i'm dumb but this is the request xml schema:
So now shall i place more whitespace before ?
What do you think? and if so how?
You have three errors in your XML, but you can easily find them (and future ones) using xmllint (also online).
Basically, your XML comments ought to end with --> and not --->, and you should check tag opening/closing.
Also, the request is for <XMLInfoParcel>string</XMLInfoParcel>, but that is not what you're sending...?

PHP SoapClient fails to include function-params in XML properly

I try to make a request to a .NET WSDL function called GetPeriodicValues. The function requires some params and the problem is that SoapClient creates an incorrect XML.
This PHP-code...
$client = new SoapClient(self::URL , array('trace' => 1, 'encoding' => 'UTF-8', 'soap_version' => SOAP_1_1));
$params = array('name' => 'myname', 'address' => 'myaddress');
$result = $client->__soapCall('GetPeriodicValues', array('parameters' => $params), array());
...genereates the following request-XML (I have excluded some irrelevant content):
<SOAP-ENV:Body>
<ns1:RequestOf_GetPeriodicValuesParameters/>
</SOAP-ENV:Body>
But I would expect it to create this
<SOAP-ENV:Body>
<ns1:RequestOf_GetPeriodicValuesParameters>
<ns1:name>myname</ns1:name>
<ns1:address>myaddress</ns1:address>
</ns1:RequestOf_GetPeriodicValuesParameters>
</SOAP-ENV:Body>
How should i include the params in the function-call?
Problem solved. What I learned was that the XML-output is dependent on the WSDL and in my case it wanted me to put params in an array with the key "Params". However, looking at the XML return by the WSDL I found no such information, instead it looked like the key "parameters" should be used. I'm not sure, but perhaps something was wrong with the service.

Categories