hello, i am using the following code in order to call a server but i get this error:
Fault
Array
(
[faultcode] => soap:Server
[faultstring] => Server was unable to process request. --> Object reference not set to an instance of an object.
[detail] =>
)
what should i try so as the server accept the xml code i am sending him?
<?php
// Pull in the NuSOAP code
require_once('nusoap.php');
// Create the client instance
$client = new soapclient('http://xxx.xxx.xxx.xxx/services/filename.asmx?wsdl', true);
// Check for an error
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
// At this point, you know the call that follows will fail
}
$xml_4_sending="xml code";
// Call the SOAP method
$result = $client->call('GradeActivity', array('xmlContent' => $xml_4_sending));
// Check for a fault
if ($client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
// Display the result
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
}
}
// Display the request and response
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
// Display the debug messages
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
?>
thank you
SoapClient->__soapCall()
(No version information available, might be only in CVS)
SoapClient->__soapCall() — Calls a SOAP function
Popis
class SoapClient {mixed __soapCall ( string $function_name, array $arguments [, array $options [, mixed $input_headers [, array &$output_headers]]] )
}
This is a low level API function that is used to make a SOAP call. Usually,
in WSDL mode,
you can simply call SOAP functions as SoapClient
methods. This method useful in non-WSDL mode when soapaction
is unknown, uri differs from the default or when sending
and/or receiving SOAP Headers.
On error, a call to a SOAP function can cause PHP to throw exceptions or return a
SoapFault object if exceptions are disabled.
To check if the function call failed to catch the SoapFault exceptions,
check the result with is_soap_fault().
Návratové hodnoty
SOAP functions may return one, or multiple values. If only one value is returned by the SOAP function, the return value of __soapCall will be a simple value (e.g. an integer, a string, etc). If multiple values are returned, __soapCall will return an associative array of named output parameters.
PÅ™Ãklady
PÅ™Ãklad 1962. SoapClient->__soapCall() Examples
<?php
$client = new SoapClient("some.wsdl");
$client->SomeFunction($a, $b, $c);
$client->__soapCall("SomeFunction", array($a, $b, $c));
$client->__soapCall("SomeFunction", array($a, $b, $c), NULL,
new SoapHeader(), $output_headers);
$client = new SoapClient(null, array('location' => "http://localhost/soap.php",
'uri' => "http://test-uri/"));
$client->SomeFunction($a, $b, $c);
$client->__soapCall("SomeFunction", array($a, $b, $c));
$client->__soapCall("SomeFunction", array($a, $b, $c),
array('soapaction' => 'some_action',
'uri' => 'some_uri'));
?>
Viz také
| SoapClient->__construct() |
| SoapParam->__construct() |
| SoapVar->__construct() |
| SoapHeader->__construct() |
| SoapFault->__construct() |
| is_soap_fault() |
SoapClient->__soapCall()
28-Aug-2007 02:17
01-Aug-2007 07:30
If you are trying to use a .NET web service and every call you make returns the following error,
"Server was unable to process request. --> Object reference not set to an instance of an object."
... then you need to pass Objects into the method rather than the standard variables.
To do this, you need to create mini classes that have properties of the variables you are trying to pass, map them to the types of the WSDL, then pass them into the methods.
Here is an example,
<?php
class Test {
public $account;
public $password;
}
$parameters = new Test;
$parameters -> account = $username;
$parameters -> password = $password;
try {
$client = new SoapClient ("https://www.somewebsite.com/Service.asmx?wsdl", array('classmap' => array('CheckUser' => 'Test')));
$client -> CheckUser ($parameters);
echo "Valid Credentials!";
}
catch (Exception $e) {
echo "Error!<br />";
echo $e -> getMessage ();
}
?>
16-Jun-2007 04:14
In response to dumbo's comment on the request not always returning an array:
"more than 1 <field>, you'll loop through the <field> tags (as expected)..."
Here's some quick code to get around it:
<?php
function getArrayFromResponse($data)
{
if (sizeof($data) < 2) {
$new = array();
$new[] = $data;
return $new;
}
return $data;
}
?>
Usage:
$result = $soap->__soapCall('DoSomething', array('parameters' => $param))
foreach(getArrayFromResponse($result['ResponseKeyHere']) as $a) {
// $a will always be an object
}
11-May-2007 01:10
When I have to manage a long number in the header,
the __soapCall function returns a float number in exponential format.
The returned value can't be sent to the server because it is different
from the original one ( for the floating point values limited precision ).
Example :
// Server sended value : 339051398236687110
$soapclient->__soapCall('beginSession',$parameters, null, null,$header);
echo $header["sessionID"]; // returns 3.3905139823669E+17 or 339051398236687050 ( converted to sting)
// and that is different from the original 339051398236687110
$strNum = $soapclient->__getLastResponse();
preg_match("([0-9-]{19,20})", $strNum, $args); // Extracts the correct sessionID value as a string
$headers = new SoapHeader( "http://xml.apache.org/axis/session", "sessionID", $args[0]);
$soapclient->__soapCall('nextCall', $parameters, null, $headers);
16-Apr-2007 06:17
Using wsdl file automatically generated by the jboss4.2CR1 deployer,
I tried dozens of methods to marshal parameters to a JAXWS endpoint. And the service constantly got null parameters. At last, I found the right way to call is
<?
$params = array('param1'=>"foo", 'param2'=>"bar");
$client->__soapCall('yourMethod', array('parameters' => $params));
?>
By this way, the parameters' names and values are correctly enclosed by the method name, and the service gives expected replies.
02-Apr-2007 06:02
Note that when a SOAP call returns an array [max_elements=">1"] - you may not actually get an array... (this may be a fault in the wsdl, but it's certainly a potential headache).
foreach($result->field as $field) {
doSomethingWith($field);
}
With more than 1 <field>, you'll loop through the <field> tags (as expected)...
However, with exactly 1 <field>, you'll instead loop through the child elements of the first <field> (almost certainly *not* what you wanted to happen)...
14-Mar-2007 08:20
I would just like to add to OrionI's example that if the result is an array eg int, int, string, string then those values can be retrieved as follows:
<?php
...
$simpleresult = $objectresult->SumResult;
echo $simpleresult->element name;
30-Jan-2007 04:43
The options in the third argument is documented in http://php.net/manual/en/function.soap-soapclient-construct.php
11-Jan-2006 07:44
I was working with SOAP for the first time and I had to create a client that sent a date range to a WSDL (Web Services Description Language) to return some information I needed. I didn't know how to pass the params and there really was no documentation about it. The main thing you have to make sure to do is when you pass params to a method that is definied by the WSDL that you are calling is that you use the same param name for the key of the array or the object variable as shown below. Also, if you don't know what the methods/functions that a WSDL has or the params that you need to pass it you can use the __getFunctions() and __getTypes() methods after you declare your new SoapClient.
<?php
// From and to are the two params that the execute function needs
// when called from the WSDL so make you to have them as the
// key to an array like below
$params["From"] = "06/01/2005"; // also can use $params->From = "date";
$params["to"] = "12/31/2005"; // also can use $params->to = "date";
$client = new SoapClient("some.wsdl");
try {
print($client->execute($params));
} catch (SoapFault $exception) {
echo $exception;
}
?>
21-Sep-2005 09:38
If you want to pass an xml document node as a function parameter, your need to create a SoapVar object with a text represention of the xml node and the XSD_ANYXML encoding constant. However, this constant is not exported by the extension and is not documented for some unknown reason.
Therefore, to get this to work you must either register the XSD_ANYXML #define as a PHP constant, or use the integer value of the constant when creating the SoapVar, which is 147.
$soapvar = new SoapVar($xml_text, 147);
$params = array("ItemXml" => $soapvar, "PropertyView" => "blah");
$result = $this->soapclient->__soapCall("SaveItem", array("parameters"=>$params), null, $this->soapheaders);
However, this still doesnt give the correct result. For some reason, the ItemXml parameter node is not wrapped around the associated xml parameter in the soap request, and the following soap is produced (assumming '<item>blah</item>' is used as the $xml_text):
<SOAP-ENV:Envelope xmlns:SOAP-ENV="..." xmlns:ns1="...">
<SOAP-ENV:Header>...</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:SaveItem>
<item>blah</item>
<ns1:PropertyView>blah</ns1:PropertyView>
</ns1:SaveItem>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
04-May-2005 03:36
Following OrionI's example:
<?php
$client = new SoapClient("http://server/sumservice.asmx?WSDL");
$params->a = 2;
$params->b = 3;
$objectresult = $client->Sum($params);
$simpleresult = $objectresult->SumResult;
print($simpleresult); //produces "-1"
?>
Please note that the lines:
"$client->Sum($params);"
and
"$simpleresult = $objectresult->SumResult;"
are based off of each other. If your web service function is called "Sum", then add "Result" to the end of it to get the results of the call.
EG:
<?php
$client = new SoapClient("http://server/mathservice.asmx?WSDL");
$params->a = 2;
$params->b = 3;
$objectresult = $client->Minus($params); // note the name of the function is "Minus"
$simpleresult = $objectresult->MinusResult; // note the name of the result is referenced as "MinusResult"
print($simpleresult); //produces "5"
?>
18-Apr-2005 07:27
Correction on the previously submitted code snippet...the incoming parameter for .NET also has to be in object or array form for it to be correctly converted to the XML form that .NET expects (as already mentioned by Llu?s P?mies). The full example (when using WSDL) should be like this:
<?php
$client = new SoapClient("http://server/myservice.asmx?WSDL");
$params->param1 = $value1;
$params->param2 = $value2;
$objectresult = $client->MyMethod($params);
$simpleresult = $objectresult->MyMethodResult;
?>
So if you have a C# function like this:
//sumservice.asmx
...
[WebMethod]
public int Sum(int a, int b)
{
return a + b;
}
...
The PHP client would be this:
<?php
$client = new SoapClient("http://server/sumservice.asmx?WSDL");
$params->a = 2;
$params->b = 3;
$objectresult = $client->Sum($params);
$simpleresult = $objectresult->SumResult;
print($simpleresult); //produces "5"
?>
15-Apr-2005 11:46
When calling over SOAP to a .NET application, you may end up with an object as a result instead of a simple type, even if you're just grabbing a simple type (like a boolean result). Use a property accessor to get the actual result, like this:
<?php
$client = new SoapClient("http://server/myservice.asmx?WSDL");
$objectresult = $client->MyMethod($param1, $param2);
$simpleresult = $objectresult->MyMethodResult;
?>
Note that .NET seems to name the result MethodNameResult for method MethodName.
17-Feb-2005 03:19
If your service is a .NET doc/lit, which means the input message has a single part named 'parameters' that is a structure that wraps the parameters. Your call should look like this:
<?php
$params = array('param_name_1'=>$val_1,'param_name_2'=>$val_2);
$client->call('MethodName', array('parameters' => $params));
?>
