简体   繁体   中英

SOAP server doesn't get all the arguments

I am using PHP and I use SOAP class that is bundled with PHP. I spent all day trying to figure out why this wasn't working.

I wrote this on client side:

 $client = new SoapClient("Audit.wsdl");                              
 $params=array('SerialNumber'=>'PB4LAX6JLJT7M','PercentProgress'=>'50','ResultID'=>'5');
 $result=$client->__soapCall('HardDriveStatusUpdate',array($params));

I then wrote this on server side:

function HardDriveStatusUpdate($sn, $p, $rs)
{
    $serialNumber=$sn->SerialNumber;
    $percentProgress=$p->PercentProgress;
    $resultID=$rs->ResultID; 
    // process with variables 
    return array('HardDriveStatusUpdateResult' => '$result');
}

$server=new SoapServer('Audit.wsdl');
$server->addFunction('HardDriveStatusUpdate');
$server->handle();

I noticed that nothing was happening. To debug, I had the function to write to a file with "$serialNumber, $percentProgress, $resultID". It turns out that it was getting only the first argument, but the second and third argument were empty. (it showed "PB4LAX6JLJT7M,,") Why?

WSDL says:

 <s:element name="HardDriveStatusUpdate">
    <s:complexType>
      <s:sequence>
        <s:element minOccurs="0" maxOccurs="1" name="SerialNumber" type="s:string" />
        <s:element minOccurs="1" maxOccurs="1" name="PercentProgress" type="s:int" />
        <s:element minOccurs="1" maxOccurs="1" name="ResultID" type="s:int" />
      </s:sequence>
    </s:complexType>
  </s:element>

Was there something wrong with how I constructed the parameters?

Tested with __getLastRequest():

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://127.0.0.1/soap/">
     <SOAP-ENV:Body>
         <ns1:HardDriveStatusUpdate>
             <ns1:SerialNumber>PB4LAX6JLJT7M</ns1:SerialNumber>
             <ns1:PercentProgress>50</ns1:PercentProgress>
             <ns1:ResultID>5</ns1:ResultID>
         </ns1:HardDriveStatusUpdate>
     </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

The real problem is definitely the server side. The function is NOT getting the arguments even though all parameters are already been sent in SOAP request. Why is that? I tried a different method and it still fails to get the second argument. It only works with the FIRST argument.

I tried your code with the wsdl you provided, the arguments are sent in a single object, so here are the changes you need to do:

function HardDriveStatusUpdate($myObject)
{
    $serialNumber = $myObject->SerialNumber;
    $percentProgress = $myObject->PercentProgress;
    $resultID = $myObject->ResultID; 
    // process with variables 
    return array('HardDriveStatusUpdateResult' => true);
}

I tested it in SoapUi ( with print_r($myObject); die(); ) and here is the result

: 在此输入图像描述

I had massive problems when I tried to learn SOAP (thank fully for no other reason than extension of knowledge). Here is how I ended up getting it going:

Step1: Download PHP2WSDL class from PHPClasses.org

Step2: Generate WSDL with the following code require_once("PHPClasses/PHP2WSDL/WSDLCreator.php");

$test = new WSDLCreator("WSDLTester", "http://localhost/soap/SoapServer/");
//$test->includeMethodsDocumentation(false);

$test->addFile("servicefunctions.php");

$test->addURLToClass("servicefunctions", "http://localhost/soap/SoapServer/server.php");

//$test->ignoreMethod(array("example1_1"=>"getEx"));

$test->createWSDL();
$test->saveWSDL("/path/to/dir/SoapServer/test.wsdl");

I stored the above in a file called wsdl.php, which you then visit in the browser to generate the wsdl file. Note: The important thing is to put your class and the url of the soap server you create as the url in addURLToClass - as that is how your server goes into the wsdl to be called properly

The code for ServiceFunctions.php is below for completeness:

class servicefunctions {

    /**
     * stringifies
     *
     * @param string $string
     * @return string
     */
    public function stringify(string $string) {
        return "I took the " . $string . " and stringified it";
    }

    /**
     * Adds two numbers
     *
     * @param int $number1
     * @param int $number2
     * @return string
     */
    public function addify(int $number1,int $number2) {
        $answer = ($number1 + $number2);
        $string = "the answer is " . $answer;
        return (string)$string;
    }

}

Note the use of comments to enter the var types, which is then used by PHP2WSDL and in the resulting wsdl file.

Then this creates the server:

try {
  $server = new SOAPServer("/home/liam/DevEnv/SoapServer/test.wsdl",array('cache_wsdl'=>WSDL_CACHE_NONE));
  $server->setClass("servicefunctions");
  $server->handle();
  }

catch (SOAPFault $f) {
  print "ERROR".$f->faultstring;
}

and this the client:

try {
$client = new SoapClient("/home/liam/DevEnv/SoapServer/test.wsdl",array('cache_wsdl'=>WSDL_CACHE_NONE));
echo print_r($client->__getFunctions(),true) . "<br/>";
echo $client->stringify("test new");
}
catch (SOAPFault $f) {
  print "ERROR" . $f->faultstring;
}

Outputs:

Array ( [0] => string addify(integer $number1, integer $number2) [1] => string stringify(string $string) )
I took the test new and stringified it

I am sure you can easily convert this to your class.

You are sending your data as 1 parameter as an array ( array($params) ) which means that the server only receives one parameter. You can either change your function signature to only accept 1 parameter or change the way you call the function.

If you want to call it using a series of key => value parameters you could change your $params to:

array(array("SerialNumber" => 'PB4LAX6JLJT7M'), array('PercentProgres' .... ));

And then call using $result=$client->__soapCall('HardDriveStatusUpdate',$params);

In your receiving function you are attempting to retrieve your parameters with the object properties syntax, whereas you should probably be using the array syntax eg. $sn["SerialNumber"]

Here is a similar issue that helped me with the solution:

PHP Soap Server : can't access to soap request parameters in my called class

Hope it helps someone,

Daniel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM