I found this question on here:
PHP Soap Issue: Server was unable to process request. ---> Object reference not set to an instance of an object
I have a similar issue, only the WSDL is private, so I figured I'd try and get a basic timezone SOAP Client working.
The solution in the other question isn't possible for me to use with the private WSDL.
$response = $client->getTimeZoneTime(array('timezone'=>'ZULU'));
Really what I need is a way of taking a multidimensional PHP array and putting it into the SOAP formed XML document, without it going crazy and producing stuff like, for this example, this:-
<key>GetTimeZoneTime</key>
<item>ZULU</item>
Here's my PHP:
try {
$WSDL = 'http://www.nanonull.com/TimeService/TimeService.asmx?WSDL';
$client = new SoapClient($WSDL,
array(
"trace" => 1,
"exceptions" => 1,
"soap_version" => SOAP_1_1
));
$xml = '<GetTimeZoneTime><timezone>ZULU</timezone></GetTimeZoneTime>';
$xmlvar = new SoapVar(
$xml,
XSD_ANYXML
);
$response = $client->getTimeZoneTime($xmlvar);
echo "<pre>\n";
echo "Request :\n".htmlspecialchars($client->__getLastRequest()) ."\n";
echo "Response:\n".htmlspecialchars($client->__getLastResponse())."\n";
echo "</pre>";
} catch (SoapFault $exception) {
echo "<pre>\n";
echo "Request :\n".htmlspecialchars($client->__getLastRequest()) ."\n";
echo "Response:\n".htmlspecialchars($client->__getLastResponse())."\n";
echo $exception;
echo "</pre>";
}
This is the request it produces:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.Nanonull.com/TimeService/">
<SOAP-ENV:Body>
<GetTimeZoneTime>
<timezone>ZULU</timezone>
</GetTimeZoneTime>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
And the SOAP Fault is:
Server was unable to process request. ---> Object reference not set to an instance of an object.
What's the correct way of turning a multidimensional PHP array into the appropriate format for a SOAP request?
What does the SOAP fault returned actually mean?
Edit: After some searching around elsewhere I thought I'd try the approach of creating a PHP class to mirror the variables on the server. This doesn't work either.
class TimeZone {
public function __construct ()
{
$this->timezone = 'ZULU';
}
}
$WSDL = 'http://www.nanonull.com/TimeService/TimeService.asmx?WSDL';
$client = new SoapClient($WSDL,
array(
"trace" => 1,
"exceptions" => 1,
"soap_version" => SOAP_1_1
));
$xmlvar = new SoapVar(new TimeZone(), SOAP_ENC_OBJECT, "TimeZone");
$response = $client->getTimeZoneTime($xmlvar);
For the Timezone one, adding the classmap parameter made it work:
$client = new SoapClient($WSDL,
array(
"trace" => 1,
"exceptions" => 1,
"soap_version" => SOAP_1_1,
"classmap" => array('timezone' => 'TimeZone')
));
$obj = new TimeZone();
$response = $client->getTimeZoneTime($obj);
echo "<h1>".$response->getTimeZoneTimeResult."</h1>";
For the main problem I'm having, it warrants a new question.
I may be wrong, but I gather the meaning of the error message to be twofold:
The object passed into the soap call may not be an object at all.
The object passed into the soap call may be an object, but if all of its attributes do not match what the server expects it will return that error.
Related
I'm new with SOAP and trying to connect it with PHP but without positive results. Maybe you can give me a hand
SOAP 1.2 Request
POST /XXXservice.asmx HTTP/1.1
Host: XXX.prueba.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<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>
<GetAcData xmlns="https://prueba.com/">
<Userid>string</Userid>
<Password>string</Password>
</GetAcData>
</soap12:Body>
</soap12:Envelope>
SOAP 1.2 Response
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<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>
<GetAcDataResponse xmlns="https://prueba.com/">
<XX>xml</GetAcDataResult>
</GetAcDataResponse>
</soap12:Body>
</soap12:Envelope>
I used the following code but I'm getting this message:
SOAP-ERROR: Parsing WSDL: Couldn't load from 'https://XXX.YYYY.com/XXXservice.asmx' : Premature end of data in tag html line 3
<?php
$options = array(
'Userid' => 'xx',
'Password' => 'xx',
);
$client = new SoapClient("https://XXX.YYYY.com/XXXservice.asmx", $options);
$result = $client('GetAcData');
?>
First of all you need an endpoint url of your webservice, that provides you the wsdl content. As #camelsWrittenInCamelCase has written in his comment, you should try https://XXX.YYYY.com/XXXservice.asmx?wsdl instead of https://XXX.YYYY.com/XXXservice.asmx.
Next you should wrap all your soap client stuff in a try/catch block, to get all possible exceptions and the most informations in case of an error.
try {
$client = new \SoapClient(
'https://XXX.YYYY.com/XXXservice.asmx?wsdl',
[
'cache_wsdl' => WSDL_CACHE_NONE,
'exceptions' => true,
'soap_version' => SOAP_1_2,
'trace' => true,
]
);
} catch (\SoapFault $fault) {
echo "<pre>";
var_dump($fault);
echo "</pre>";
echo "<pre>";
var_dump($client->__getLastRequest());
echo "</pre>";
echo "<pre>";
var_dump($client->__getLastResponse());
echo "</pre>";
}
As shown in the example above the soap client is initialized with an options array. We use the trace option, to enable tracing for getting the last send request and response. Additionally wie use the exception option, to force the client to throw exceptions in case of any error. Further more we disable caching the wsdl settings as long as you are developing. If you are going into production, this option should be set to WSDL_CACHE_DISK or WSDL_CACHE_MEMORY.
Now you need to know, which functions and types (complex types) your webservice provides. For this purpose you can do the following.
// getting the functions of your webservice
echo "<pre>";
var_dump( $client->__getFunctions() );
echo "</pre>";
// getting the types of your webservice
echo "<pre>";
var_dump( $client->__getTypes() );
echo "</pre>";
Since I do not know the exact scope of your web service, I'll just assume that the webservice delivers a method called DetAcData with two parameters Userid and Password. With this informations I can only guess, how the right call could look like. For detailed informations I 'd need the output of __getFunctions() and __getTypes().
An examplary call could look like this.
try {
$client = new \SoapClient(
'https://XXX.YYYY.com/XXXservice.asmx?wsdl',
[
'cache_wsdl' => WSDL_CACHE_NONE,
'exceptions' => true,
'soap_version' => SOAP_1_2,
'trace' => true,
]
);
$data = new \stdClass();
$data->Userid = 'YourUserId';
$data->Username = 'YourUsername';
$result = $client->GetAcData($data);
// $result will be an object
echo "<pre>";
var_dump($result);
echo "</pre>";
} catch (\SoapFault $fault) {
// error handling
}
Further more you can write data objects for every complex type mentioned in the functions and types output from your webservice. You can add a classmap to the options array when instantiating your soap client, so that every response and request will automatically be parsed in the corresponding data object. How soap client classmaps work is explained in the php documentation.
Just have a try. I 'm sure you will get it on your own. ;)
Webservice : http://webservices.dishtv.in/Services/Mobile/Trade/TradeSubscriberInfo.asmx
Overloaded method is GetSubscriberInfoV2 MessageName="GetSubscriberInfoVCLogV2"
My php code is,
<?php
$mobileno="01523833622";
$url="http://webservices.dishtv.in/Services/Mobile/Trade/TradeSubscriberInfo.asmx?wsdl";
$client = new SoapClient($url);
$soapHeader = array('UserID' => '47','Password' => 'zZa##286##');
$header = new SOAPHeader('http://tempuri.org/', 'AuthenticationHeader', $soapHeader);
$client ->__setSoapHeaders($header);
try
{
$res = $client->GetSubscriberInfoVCLogV2(array('vcNo' => $mobileno, 'mobileNo' => '', 'BizOps' => '1', 'UserID' => '555300', 'UserType' => 'DL' ));
}
catch(SoapFault $e)
{
echo "Invalid No";
print_r($e);
}
print_r($res);
?>
It gives error GetSubscriberInfoVCLogV2 is not found. I need to get the response of GetSubscriberInfoVCLogV2. Can anyone help me to find the solution.
The only way to do this is writing the XML request manually and sending it through the method SoapClient::__doRequest.
It would be something like this:
$request = <<<'EOT'
<?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:Body>
<ns1:TheMessageNameGoesHere>
<ns1:param1>$param1</ns1:param1>
<ns1:param2>$param2</ns1:param2>
<ns1:param3>$param3</ns1:param3>
</ns1:TheMessageNameGoesHere>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
EOT;
$response = $soapClient->__doRequest(
$request,
"http://www.exemple.com/path/to/WebService.asmx",
"http://tempuri.org/TheMessageNameGoesHere",
SOAP_1_1
);
Change "TheMessageNameGoesHere" for the MessageName found in the WebService description page.
The XML structure can also be found in the WebService description page, when you click in the function.
The third parameter of the method __doRequest is the SOAP action, and can be found in the WSDL file as an attribute of the tag <soap:operation>
I need to get data from http://ws.jrtwebservices.com/jrtlowfaresearch/jrtlfs.asmx, this service need credential information.such as ID, userid and system value. I put these information into one string:
$xml_post_string = "<POS><Source> <RequestorID Type='21' ID='xxx'/> </Source> <TPA_Extensions> <Provider><System>xxx</System> <Userid>xxx</Userid> </Provider></TPA_Extensions></POS>"
And i also defined SoapClient:
$client = new SoapClient(null, array('uri' => "http://ws.jrtwebservices.com",
'location => "http://ws.jrtwebservices.com/jrtlowfaresearch/jrtlfs.asmx") );
I call soapCall as:
$response = $client->__soapCall('do_LowfareSearch',array($xml_post_string),array('soapaction' => 'http://jrtechnologies.com/do_LowfareSearch'));
Does anybody know why i get empty response?
Thanks very much!
Using your code, the request looks like this:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xm...">
<SOAP-ENV:Body>
<ns1:do_LowfareSearch>
<param0 xsi:type="xsd:string">
"<POS><Source> <RequestorID Type='21' ID='xxx'/> </Source> <TPA_Extensions> <Provider <System>xxx</System> <Userid>xxx</Userid> </Provider></TPA_Extensions></POS>"
</param0>
</ns1:do_LowfareSearch>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
The client used the method you passed but could not structure the parameters the way you gave them. All of your parameters are just in " " inside of <param0>.
(Also, you are missing a ' after location. 'location => "http:...)
When you make your SOAP client you want to set the WSDL, it will do all the XML formatting for you.
The WSDL should have the location in it so you do not need to worry about that.
I like to use a WSDL validator to test out the methods and see their parameters.
You should structure the information you want to pass as arrays or a classes and let the SOAP client and WSDL convert it into the XML you need.
So something like this is what you are looking for:
<?php
//SOAP Client
$wsdl = "http://ws.jrtwebservices.com/jrtlowfaresearch/jrtlfs.asmx?WSDL";
$client = new SoapClient($wsdl, array( 'soap_version' => SOAP_1_1,
'trace' => true, //to debug
));
try {
$args = array(
'companyname'=> 'xxx',
'name'=> 'xxx',
'system'=> 'xxx',
'userid'=> 'xxx',
'password'=> 'xxx',
'conversationid'=>'xxx',
'entry'=> 'xxx',
);
$result = $client->__soapCall('do_LowfareSearch', $args);
return $result;
} catch (SoapFault $e) {
echo "Error: {$e}";
}
//to debug the xml sent to the service
echo($client->__getLastRequest());
//to view the xml sent back
echo($client->__getLastResponse());
?>
I have this problem, currently I am learning soap and developing online booking system with wsdl of http://vanillatours.com in PHP
their required headers are soapaction, and charset. which I have included, soapaction changes for desired request. for example, currently trying to do checklogin() function.
I tried print_r($client->__getFunctions()) to see if functions are attached, they are!
I tried print_r($client) to see if headers are attached and they are
the problem is that I can not figure out why I get this errormessage.
System.NullReferenceException: Object reference not set to an instance of an object.
at WcfService.Wcf.CheckLogin(LoginHeaderWcfRequest loginHeader)
Tried everything! I am very new with soap and any help would be appreciated. maybe I am not using correctly data "request"?
thank you!
<?php
$wsdl = "http://xmltest.vanillatours.com/Wcf.svc?wsdl";
$client = new SoapClient($wsdl);
$data = array(
"request" => array(
"a:AgentId" => blabla,
"a:Language" => "En",
"a:Password" => "blabla",
"a:Username" => "blabla"
)
);
$header = array();
$header[] = new SoapHeader('http://tempuri.org/IWcf/CheckLogin','SOAPAction');
$header[] = new SoapHeader('text/xml; charset=utf-8','ContentType');
$client->__setSoapHeaders($header);
$response = $client->__soapCall('CheckLogin', $data);
echo '<pre>';
print_r($client->__getFunctions()); // functions seem to show pretty well
echo '<br>------------------------------------------------------<br><br>';
print_r($client); // headers are attached
echo '<br>------------------------------------------------------<br><br>';
print_r($response); // errormessage, can not figure out what is the problem.
echo '</pre>';
?>
this is how to connect from their documentation, if I can use other method, would appreciate too.
CheckLogin function checks the user credentials is valid or not.
SOAPAction value is http://tempuri.org/IWcf/CheckLogin
3.1.2 Request
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<CheckLogin xmlns="http://tempuri.org/">
<loginHeader xmlns:a="http://schemas.datacontract.org/2004/07/WcfService"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:AgentId>Your Agent Id</a:AgentId>
<a:Language>Your preferred language</a:Language>
<a:Password>Your Password</a:Password>
<a:Username>Your username</a:Username>
</loginHeader>
</CheckLogin>
</s:Body>
</s:Envelope>
The structure of $data is not right. It should be:
$data = array(
"loginHeader" => array(
"AgentId" => blabla,
"Language" => "En",
"Password" => "blabla",
"Username" => "blabla"
)
);
Given that the following client.php creates this request XML:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://soap.dev/" 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/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:Test>
<RequestId xsi:type="xsd:int">1</RequestId>
<PartnerId xsi:type="xsd:int">99</PartnerId>
</ns1:Test>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
How do I access the names of the parameters? (RequestId and PartnerId) inside a server.php? The names are clearly there in the payload, but on server side only the values are received (1 and 99)
Sample code follows:
client.php
<?php
$client_params = array(
'location' => 'http://soap.dev/server.php',
'uri' => 'http://soap.dev/',
'trace' => 1
);
$client = new SoapClient(null, $client_params);
try {
$res = $client->Test(
new SoapParam(1, 'RequestId'),
new SoapParam(99, 'PartnerId')
);
} catch (Exception $Ex) {
print $Ex->getMessage();
}
var_dump($client->__getLastRequest());
var_dump($client->__getLastResponse());
server.php
class receiver {
public function __call ($name, $params)
{
$args = func_get_args();
// here $params equals to array(1, 99)
// I want the names as well.
return var_export($args, 1);
}
}
$server_options = array('uri' => 'http://soap.dev/');
$server = new SoapServer(null, $server_options);
$server->setClass('receiver');
$server->handle();
Please note that I can not really change the incoming request format.
Also, I am aware that I could give the names back to parameters by creating a Test function with $RequestId and $PartnerId parameters.
But what I really want to is to get name/value pairs out of incoming request.
So far the only idea I have is to simply parse the XML and this cannot be right.
Back when I had this problem I finally decided to go with the proxy function idea - Test function with parameters named ($RequestId and $PartnerId) to give names back to parameters. Its adequate solution, but certainly not the best.
I have not yet lost the hope for finding a better solution though, and here is my best idea so far
<?php
class Receiver {
private $data;
public function Test ()
{
return var_export($this->data, 1);
}
public function int ($xml)
{
// receives the following string
// <PartnerId xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:int">99</PartnerId>
$element = simplexml_load_string($xml);
$this->data[$element->getName()] = (string)$element;
}
}
$Receiver = new Receiver();
$int = array(
'type_name' => 'int'
, 'type_ns' => 'http://www.w3.org/2001/XMLSchema'
, 'from_xml' => array($Receiver, 'int')
);
$server_options = array('uri' => 'http://www.w3.org/2001/XMLSchema', 'typemap' => array($int), 'actor' => 'http://www.w3.org/2001/XMLSchema');
$server = new SoapServer(null, $server_options);
$server->setObject($Receiver);
$server->handle();
It still means parsing XML manually, but one element at a time which is a bit more sane that parsing entire incoming SOAP message.