php soapclient Array to string conversion - php

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.

Related

Passing xml value as array to soap client PHP

I have xml data to passing to SOAPClient as array
this is my xml data that I want to pass to SOAPClient
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cb5="http://tes.com" xmlns:smar="http://tes.com/Search">
<soapenv:Header />
<soapenv:Body>
<cb5:SmartSearchIndividual>
<cb5:query>
<smar:InquiryReason>Reason</smar:InquiryReason>
<smar:InquiryReasonText />
<smar:Parameters>
<smar:DateOfBirth>1985-05-01</smar:DateOfBirth>
<smar:FullName>J Doe</smar:FullName>
<smar:IdNumbers>
<smar:IdNumberPairIndividual>
<smar:IdNumber>123456789</smar:IdNumber>
<smar:IdNumberType>KTP</smar:IdNumberType>
</smar:IdNumberPairIndividual>
</smar:IdNumbers>
</smar:Parameters>
</cb5:query>
</cb5:SmartSearchIndividual>
</soapenv:Body>
</soapenv:Envelope>
I passing this xml to array like this
$params = array(
'InquiryReason' => 'Reason',
'InquiryReasonText' => '',
'Parameters' => array(
'DateOfBirth' => '1985-05-01',
'FullName' => 'J Doe',
'IdNumbers' => array(
'IdNumberPairIndividual' => array(
'IdNumber' => '123456789',
'IdNumberType' => 'KTP'
)
)
)
);
$response = $client->SmartSearchIndividual($params);
when I submit this show an error The formatter threw an exception while trying to deserialize ....
please help.
thanks!

Multidimensional Array with PHP Soap Client

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
)];

WSDL interpret supossed operation parameters

I'm trying to invoque this operation getCountries but I can't figure out reading the WSDL file wich parameters are needed and in which structured way:
http://webservice.nizacars.es/Rentway_WS/getCountries.asmx?WSDL
I've already tried with:
$this->soap_client->getCountries(
array(
'countriesRequest' => array(
'companyCode' => $this->login,
'allCountries' => true
)
)
)
$this->soap_client->getCountries(
array(
'companyCode' => $this->login,
'allCountries' => true
)
)
$this->soap_client->getCountries(
'companyCode' => $this->login,
'allCountries' => true
)
But it seems I'm not matching the specs, since I'm getting a "[Server was unable to process request. ---> Object reference not set to an instance of an object.]"
The final request with SoapClient::__getLastRequest is:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.jimpisoft.pt/Rentway_Reservations_WS/getCountries">
<SOAP-ENV:Body>
<ns1:getCountries/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Edit, Solution:
$data = array(
'getCountries' => array(
'objRequest' => array(
'companyCode' => $this->login,
'allCountries' => true
)
)
);
$result = #$this->_client->__call('getCountries',$data);
wich parameters are needed and in which structured way:
You can use the soapUI-tool for generating a valid soap-request and response.
I propose to compare your soap-request (by logging it) with the one generated by soapUI:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:get="http://www.jimpisoft.pt/Rentway_Reservations_WS/getCountries">
<soap:Header/>
<soap:Body>
<get:getCountries>
<!--Optional:-->
<get:objRequest>
<!--Optional:-->
<get:companyCode>?</get:companyCode>
<get:allCountries>?</get:allCountries>
</get:objRequest>
</get:getCountries>
</soap:Body>
</soap:Envelope>

Soap PHP WSDL XML formatting

So I am using Soap client in PHP and I'm trying to make a request on some WSDL. I try to have a specific output but it seems a bit far from what I try to do. Maybe I don't do this right. I'm a bit new to WSDL.
Here is the result I want:
<SOAP-ENV:Body>
<loc:sendSms>
<loc:addresses>9000000000</loc:addresses>
<loc:senderName>9000000</loc:senderName>
<loc:message>test SMS</loc:message>
</loc:sendSms>
</SOAP-ENV:Body>
This is what I try to fit my requirement, but it ends with a server error:
$MCIResp = (array) $_soapClient->__soapCall('sendSms',
'sendMessage' => array(
'addresses' => '9000000000',
'senderName' => '9000000',
'message' => 'test SMS '
)
);
And this is the error I get from this request:
"SoapFault exception: [Client] SOAP-ERROR: Encoding: object has no
'sendmessage' property";
EDIT: Made some improvements on that by getting inspiration on some posts
Here is my code now:
$_soapClient = new SoapClient(
null,
array(
'location' => 'http://someIP/parlayxsmsgw/services/SendSmsService?wsdl',
'uri' => 'http://someIP/parlayxsmsgw/services/SendSmsService?wsdl',
'trace' => 1,
'cache_wsdl' => WSDL_CACHE_NONE,
'use' => SOAP_LITERAL,
'style' => SOAP_DOCUMENT,
'stream_context' => stream_context_create(array(
'http' => array(
'header' => 'servicekey: someservicekey'
),
)),
)
);
$params = new \SoapVar("<sendSms><addresses>90000000000</addresses><senderName>90000000</senderName><message>test SMS</message></sendSms>",XSD_ANYXML);
And then I do a
$_soapClient->whatEverNameHereWillBeIgnored($params);
This is now the XML I generate:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<sendSms>
<addresses>989127184955</addresses>
<senderName>98307066</senderName>
<message>test SMS</message>
</sendSms>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
But I receive this as a response:
Unexpected subelement sendSms
do the "loc:" are mandatory to solve this ? Because when I add it in the raw xml it says that
Undeclared namespace prefix "loc"
Thanks,
I strongly advise you to use a WSDL to PHP generator such as PackageGenerator, it will ease you the request construction so you won't have to wonder how to construct the request, it will be cristal clear.

PHP Soap And .Net Dataset

I got an issue here. I am trying make a request to a Web Appi: http://www.speedex.gr/getvoutrans/getvoutrans.asmx?WSDL
And I am sending a request to insertPodData();
I am using PHP and SOAP.
I am succesfull at connecting and giving the correct credentials. However I am not able to send a Dataset (cause I do not know the right way), so i get an empty dataset.
Datasets are for .NET lang. So it is kind of tricky with the php.
I tried already to send it as an array, i still get an empty result.
Here are some coding.
PHP:
$dataset = array(
'schema' => array(
'Enter_Branch_Id' => $speedex_branch_id,
'SND_Customer_Id' => $speedex_cust_id,
'SND_Agreement_Id' => $speedex_appi_key,
'RCV_Name' => 'Test',
'RCV_Addre1' => 'Test Adress',
'RCV_Zip_Code' => '54636',
'RCV_City' => 'Thessaloniki',
'RCV_Country' => 'Greece',
'RCV_Tel1' => '*******',
'Voucher_Weight' => '0',
),
'any' => ''
);
try {
$soap = new SoapClient("http://www.speedex.gr/getvoutrans/getvoutrans.asmx?WSDL",array('trace' => true));
$oAuthResult = $soap->insertPodData(
array(
'username' => $speedex_usrn,
'password' => $speedex_psw,
'VoucherTable' => $dataset,
'_tableFlag' => 3
)
);
$resultVoucher = $oAuthResult;
print_r($resultVoucher);
echo '<br>';
echo "REQUEST:\n" . htmlentities($soap->__getLastRequest()) . "\n";
die();
}catch(SoapFault $fault) {
die('<h1>Ooooops something is broken. Refresh or contact module creator </h1><br>'.$fault);
}
This is returning this result
RESULT: stdClass Object ( [insertPodDataResult] => 1 [newVoucherTable] => stdClass Object ( [schema] => [any] => ) )
REQUEST:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:insertPodData>
<ns1:username>****</ns1:username>
<ns1:password>****</ns1:password>
<ns1:VoucherTable>********************TestTest Adress54636ThessalonikiGreece********</ns1:VoucherTable>
<ns1:_tableFlag>3</ns1:_tableFlag>
</ns1:insertPodData>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
As you can observe the Dataset is not created and all the values are passed with out a reference.
Any ideas clues? Thanks in advance!

Categories