How to set parameters by reference in nusoap?

how can i set the ref parameter to nusoap. in the following code I have to set two parameters for ref (status and recId). note that & does not work:

  $params = array( 'username' => GATEWAY_USERNAME, 'password' => GATEWAY_PASSWORD, 'from' => GATEWAY_NUMBER, 'to' => array($to), 'text' => $message, 'flash' => $flash, 'udh' => '', 'status' => &$status, 'recId' => &$recId ); $sendParams=array($params); $res=$this->client->call('Send',$sendParams); 
+4
source share
2 answers

About passing values ​​by reference:

 function test_reference_in(&$array) { $array['a'] = 2; } $test_array['a'] = 1; test_reference_in($test_array); echo $test_array; //-> it prints 2 

About nusoap:

Now in nusoap the client class is nusoap_client .
In this class, you have nusoap_client::call() to make a soap call.

This is what appen in nusoap_client::call() for the $params array, which is your $sendParams in your example.
I am going to omit all other methods that are not related to $params in order to better explain what is happening.

 /** * Pseudocode of call to explain the operations on $params * @see nusoap_client::call() */ function call($operation,$params=array(),$namespace='http://tempuri.org',$soapAction='',$headers=false,$rpcParams=null,$style='rpc',$use='encoded'){ /* some code . . . */ // varDump is just var_dump so print $params $this->appendDebug('params=' . $this->varDump($params)); /* some code . . . */ /* * Here $params has been read, no write operation in there * about serializeRPCParameters @see class.wsdl.php again is just reading */ if (is_string($params)) { $this->debug("serializing param string for WSDL operation $operation"); $payload = $params; } elseif (is_array($params)) { $this->debug("serializing param array for WSDL operation $operation"); $payload = $this->wsdl->serializeRPCParameters($operation,'input',$params,$this->bindingType); } else { $this->debug('params must be array or string'); $this->setError('params must be array or string'); return false; } /* * Here again $params has been read, no write operation in there */ if (is_string($params)) { $this->debug("serializing param string for operation $operation"); $payload = $params; } elseif (is_array($params)) { $this->debug("serializing param array for operation $operation"); foreach($params as $k => $v){ $payload .= $this->serialize_val($v,$k,false,false,false,false,$use); } } else { $this->debug('params must be array or string'); $this->setError('params must be array or string'); return false; } } 

So, as you can see here, there are no advantages to passing $ params by reference.

Because:

  • $ params is in no way modified in nusoap_client :: call ()
  • Even if you change $ params somewhere else after you have to use nusoap_client :: call () again

What if you want to pass $ params by reference anyway? Can I do it?

Well yes you can!
To do this, you need to copy nusoap_client.php and call it nusoap_client_new.php .
There, change the form of the class name nusoap_client to nusoap_client_new .

 // From this class nusoap_client extends nusoap_base { // To this class nusoap_client_new extends nusoap_base { 

Modify the nusoap_client_new::call() method by adding ref to params as follows:

 /* * Please note &$params=array() instead of $params=array() */ function call($operation,&$params = array(),$namespace='http://tempuri.org',$soapAction='',$headers=false,$rpcParams=null,$style='rpc',$use='encoded'){ /** * Of course here you have to modify your code to make some operation on $params * according to your needs. */ /* original code . . . */ } 

At the end, update your code to require and use nusoap_client_new::call() instead of nusoap_client::call() .

+2
source

Excusable for Gustavo.

Prior to PHP 5.4 :

Reference countdown deleted.

What does this mean, in version <= PHP 5.3 this code should work:

 function appendA($string) { $stringA .= 'A'; } $string = ''; appendA(&$string); 

but no longer works later. For PHP 5.4+, to create a function that edits its parameters by reference, you must declare it this way from the very beginning. For instance:

 function appendA(&$string) { // <- first change $stringA .= 'A'; } $string = ''; appendA($string); // <- second change 

Please note that with PHP 5.4+ an attempt to call a function using appendA(&$string) will result in a notification and will not change the value by reference.

According to the original question, I did not find a place where $params change during the client::call() function in nusoap, so I see no reason to provide them by reference, but I can be blind. In any case, the method is not declared as compatible with PHP 5.4+, so the & parameter near will not work with these versions of PHP during the method call. In this case, a modification of the method is required.

+1
source

Source: https://habr.com/ru/post/1415723/


All Articles