Problems in using PHP Soap Client - php

I have this web service to access with this kind of configuration:
<?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>
<Executar xmlns="http://localhost/I9ProWebService">
<Servico>string</Servico>
<conteudoXML>string</conteudoXML>
</Executar>
</soap:Body>
</soap:Envelope>
So I tried this:
$client = new soapclient('https://domain/webservice/I9ProWebService.asmx?WSDL');
printf($client->Executar("ListarTomador","<i9proerp><listar_tomador id_pessoa_corretor =\"205\" /></i9proerp>"));
it shows this error:
System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.NullReferenceException: Object reference not set to an instance of an object. at I9ProWebService.Executar(String Servico, String conteudoXML) --- End of inner exception stack trace ---

Have you tried using htmlentities or similar to encode the XML you are sending as your second parameter? That might be causing problems with the XML sent by the SOAP client.
EDIT: In order to see what XML the PHP SOAP client is sending and what it's getting back, you can use $client->__getLastRequest() and $client->__getLastResponse() and compare the results with your soapUI call.

I found the solution of my problem.
$client = new SoapClient("http://domain/webservice/I9ProWebService.asmx?WSDL", array("features" => SOAP_SINGLE_ELEMENT_ARRAYS, "encoding" => "utf-8","trace"=> TRUE));
$strVariavel = "<ns1:conteudoXML><i9proerp><listar_tomador id_pessoa =\"999\"/></i9proerp></ns1:conteudoXML>";
$soapvar = new SoapVar($strVariavel, 147);
$xml = $client->ExecutarXML(array('Servico'=>'ListarTomador','conteudoXML'=>$soapvar));
print_r($xml);
and voilá
The problem is that it returns and object and not a XML as I expected...
It returns:
stdClass Object
(
[ExecutarXMLResult] => stdClass Object
(
[any] => <i9proerp xmlns=""><listatomador id_pessoa="5251" nm_pessoa="nome1" nr_cnpj_cpf="132132121332"/><listatomador id_pessoa="939" nm_pessoa="nome2" nr_cnpj_cpf="3213213123213"/></i4proerp>
)
)
So, I have to discover how to deal with it. Is it a case to open another question?

Related

PHP Soap call xml isnt formated correctly

I am trying to talk to a SOAP api a partner provided us with.
I create the client using a WSDL:
$client = new SoapClient('Path/to/WSDL',array('soap_version' => SOAP_1_2,'trace' => 1));
and then call a function like this:
var_dump($client->__call('gsLogin',[new SoapParam("0", '0'),
new SoapParam("User", 'sUser'),
new SoapParam("Password", 'sPassword'),
new SoapParam("Database", 'sDatabase'),
new SoapParam(1, 'lDivision'),
new SoapParam("", 'sError')]));
out comes this xml:
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://www.namespace.de/">
<env:Body>
<ns1:gsLogin/>
<sUser>User</sUser>
<sPassword>Password</sPassword>
<sDatabase>Database</sDatabase>
<lDivision>1</lDivision>
<sError></sError>
</env:Body>
</env:Envelope>
As you can see gsLogin is closed immediately with the /.
The parameters go nowhere and I get back the error that no user was given.
Any idea what causes this problem or how to fix it?

Getting the correct data from an XML file for a SoapClient request

To have a point of reference, let's use this public WSDL: https://www.dataaccess.com/webservicesserver/NumberConversion.wso?WSDL
Now this thing should accept the following xml:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<NumberToWords xmlns="http://www.dataaccess.com/webservicesserver/">
<ubiNum>500</ubiNum>
</NumberToWords>
</soap:Body>
</soap:Envelope>
And here is the code:
$requestData = simplexml_load_file($file);
//enabling or disabling the following line does not seem to make a difference, but I used it at some point to see that it does load something in there
$requestData->registerXPathNamespace("soap", "http://www.w3.org/2003/05/soap-envelope");
//print_r($requestData->xpath('//soap:Body')); //I was using this to check that the data is actually there, and it is...
$webService = new SoapClient($url);
$result = $webService->NumberToWords($requestData);
print_r($result)
And I'm getting this beautiful response:
stdClass Object
(
[NumberToWordsResult] => zero
)
I think it has something to do with how simpleXML load the data in, but I had no luck figuring out what I should do.
As a side note, if I try just manually setting the data:
$requestData = ["ubiNum"=>500];
it works, but I really want to figure out what is going on with the xml parsing/sending
Also if interested, my commented out print_r's result is the following
Array
(
[0] => SimpleXMLElement Object
(
[NumberToWords] => SimpleXMLElement Object
(
[ubiNum] => 500
)
)
)
If you're using SoapClient, you don't need to also construct the whole XML yourself. Depending on the service, you either need to pass style individual variables, or the contents of the "body".
As you say, you can just run:
$webService = new SoapClient($url);
$result = $webService->NumberToWords(["ubiNum"=>500]);
Underneath, the SoapClient class is generating the rest of the XML for you and sending it as an HTTP request.
If you want to get the data to send out of an XML document, you need to extract just that part, rather than trying to send the whole SOAP envelope inside the parameter. In this example, you need to navigate to the "NumberToWords" element; see this reference question for tips on navigating the XML namespaces but in this example you'd use something like this:
$requestData = simplexml_load_file($file);
$soapBody = $requestData->children('http://schemas.xmlsoap.org/soap/envelope/')->Body;
$numberToWords = $soapBody->children('http://www.dataaccess.com/webservicesserver/')->NumberToWords;
// Or to get the 500 directly:
$ubiNum = (int)$numberToWords->ubiNum;
Alternatively, you can just ignore the SoapClient class, construct the XML yourself, and post it with an HTTP client like Guzzle. Often the only extra step you'll need is to set the correct "SOAPAction" HTTP header.

Stuck at converting xml request to corresponding Savon request

I am trying to communicate with a 3-rd party API. I tried both savon and Soap UI but both failed, then I asked them and they gave me equivalent that works. Confused, I toggled the logger and got following XML request which is quite different from what Savon is sending.
Here is the PHP code (I guess that's not needed, but still)
$apiauth =array('UserName'=>'XXXXXX','Password'=>'XXXXXXX','ClientCode'=>'1234')
$wsdl = 'http://stagewebapi.mylescars.com/service.asmx?wsdl';
$soap = new SoapClient($wsdl);
$header = new SoapHeader('http://tempuri.org/', 'AuthHeader', $apiauth);
$soap->__setSoapHeaders($header);
$data = $soap->fetchCities($header);
print_r($data);
Here is the XML request that PHP is sending and which is working just fine
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/"><SOAP-ENV:Header><ns1:AuthHeader><ns1:UserName>XXXXXXX</ns1:UserName><ns1:Password>XXXXXXX</ns1:Password><ns1:ClientCode>1234</ns1:ClientCode></ns1:AuthHeader></SOAP-ENV:Header><SOAP-ENV:Body><ns1:fetchCities/></SOAP-ENV:Body></SOAP-ENV:Envelope>
Here is my ruby code so far
require 'savon'
auth_header = { 'UserName'=>'XXXXXXX','Password'=>'XXXXXXXX','ClientCode'=>'1234'}
client = Savon.client(:wsdl => "http://stagewebapi.mylescars.com/service.asmx?wsdl",
:namespace_identifier => :ns1 ,:namespace => "http://tempuri.org/" , :log => true, :soap_header => auth_header,
:pretty_print_xml => true, :env_namespace => 'SOAP-ENV')
puts client.call(:fetch_cities)
Here is what Savon is producing
<?xml version="1.0" encoding="UTF-8"?>
<soap-env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="http://tempuri.org/" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Header>
<UserName>XXXXXXXXXXXXXXX</UserName>
<Password>XXXXXXXXXXXXX</Password>
<ClientCode>2873</ClientCode>
</soap-env:Header>
<soap-env:Body>
<ns1:fetchCities/>
</soap-env:Body>
</soap-env:Envelope>
I don't know how to make it equivalent to the PHP one. Am I missing something obvious ?
I tried to look at Savon documentation to find if there are some methods to manipulate tags with fine grain control but couldn't find much about it.
Questions similar to this are either unanswered or are specific to the requests.
Some SO questions that helped a little
Convert this xml request to a proper savon request
Replicating xml requests with savon ruby
Little curious, how PHP gets it right most of the time. Is it the parser they use for wsdl?

Reading a Simple SOAP XML response in PHP

I have looked for a solid working answer on this can't find one.
I am also New at SOAP, but very familiar with PHP.
I send out my SOAP request with CURL and my response comes back like 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>
<GetFeatureResponse xmlns="http://www.supportsite.com/webservices/">
<GetFeatureResult>**string**</GetFeatureResult>
</GetFeatureResponse>
</soap:Body>
</soap:Envelope>
I need to save the ->GetFeatureResult 'string' in a MySQL database without the XML. Everything I try returns blank. Here's what I'm using now:
$result = curl_exec($curl);
curl_close ($curl);
$resultXML = simplexml_load_string($result);
$item = $resultXML->GetFeatureResponse->GetFeatureResult;
PHP has a built in soap client. It is a LOT easier. As long as you point it to a proper WSDL file, it will return a PHP object ready to use.
EDIT: Dug up an example I had around...
$sc = new SoapClient($wsdl, array(
'location' => "https://$host:$port/path/",
'login' => $user,
'password' => $pass
));
//$sc will now contain methods (maybe properties too) defined by the WSDL file
//Getting the info you need could be as easy as
$info = $sc->getInfo(array('parameter'=>'value'));

PHP Parse Soap Response Issue - SimpleXMLElement

I'm having problems using PHP SimpleXMLElement and simpleSMLToArray() function to parse a SOAP Response. I'm getting the SOAP response from my SOAP Server just fine. I'm writing both the SOAP Client and Server in this case. I'm using NuSoap for the server. To me, the soap response looks perfect, but the PHP5 Soap Client doesn't seem to parse it. So, as in the past, I'm using SimpleXMLElement and the function simpleXMLToArray() from PHP.NET (http://php.net/manual/en/book.simplexml.php), but can't seem to get an array.
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 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:tns="urn:eCaseWSDL">
<SOAP-ENV:Body>
<ns1:registerDocumentByPatientResponse xmlns:ns1="urn:eCaseWSDL">
<returnArray xsi:type="tns:ReturnResult">
<id xsi:type="xsd:int">138</id>
<method xsi:type="xsd:string">registerDocumentByPatient</method>
<json xsi:type="xsd:string">0</json>
<message xsi:type="xsd:string">success</message>
<error xsi:type="xsd:string">0</error>
</returnArray>
</ns1:registerDocumentByPatientResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
PHP Code from my class ($this references apply to my library code).
// Define SimpleXMLElement
$xml_element = new SimpleXMLElement($response_string); // SOAP XML
$name_spaces = $xml_element->getNamespaces(true);
var_dump($name_spaces);
$soap = $xml_element->children('ns1');
var_dump($soap);
$soap_array = $this->simpleXMLToArray($soap);
var_dump($soap_array);
return $soap_array;
I can see the namespaces; ns1, etc. But, the array is not returned. SimpleXMLElement looks like it's returning an object, but empty.
<pre>array(3) {
["SOAP-ENV"]=>
string(41) "http://schemas.xmlsoap.org/soap/envelope/"
["ns1"]=>
string(13) "urn:eCaseWSDL"
["xsi"]=>
string(41) "http://www.w3.org/2001/XMLSchema-instance"
}
object(SimpleXMLElement)#23 (0) {
}
bool(false)
Anyone have any ideas as to what I'm doing wrong. I must not have drunk enough coffee this morning. I'm tempted to just parse it with regular expressions.
SimpleXML creates a tree object, so you have to follow that tree to get to the nodes you want.
Also, you have to use the actual namespace URI when accessing it, e.g.: urn:eCaseWSDL instead of ns1:
Try this:
$soap = $xml_element->children($name_spaces['SOAP-ENV'])
->Body
->children($name_spaces['ns1'])
->registerDocumentByPatientResponse
->children();
var_dump((string)$soap->returnArray->id); // 138

Categories