如何在 nusoap 中通过 ref 设置参数。 在下面的代码中,我应该通过 ref(status 和 recId)设置两个参数。 请注意,&
不起作用:
$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);
关于通过引用传递值:
function test_reference_in(&$array) {
$array['a'] = 2;
}
$test_array['a'] = 1;
test_reference_in($test_array);
echo $test_array; //-> it prints 2
关于努肥皂:
现在在nusoap
客户端类是nusoap_client
。
在该类中,您必须nusoap_client::call()
进行肥皂调用。
这是 appen nusoap_client::call()
数组$params
这是您在示例中$sendParams
的内容。
我将省略与$params
无关的所有其他方法,以更好地解释正在发生的事情。
/**
* 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;
}
}
因此,正如您在此处看到的,通过引用传递$params没有任何优势。
因为:
- $params在 nusoap_client::call() 中没有以任何方式修改
- 即使您在必须再次使用后在其他地方修改$params nusoap_client::call()
如果您在任何情况下都想通过引用传递$params怎么办?我能做到吗?
嗯,是的,你可以!
为此,您必须复制nusoap_client.php
并将其称为 nusoap_client_new.php
.
在那里修改类名形式nusoap_client
nusoap_client_new
。
// From this
class nusoap_client extends nusoap_base {
// To this
class nusoap_client_new extends nusoap_base {
修改在参数中添加 ref 的 nusoap_client_new::call()
方法,如下所示:
/*
* 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
.
.
.
*/
}
最后更新您的代码以要求并使用nusoap_client_new::call()
而不是nusoap_client::call()
。
对古斯塔沃来说情有可原
。从 PHP 5.4 版本开始:
已删除按引用传递的调用时间传递。
这意味着,在版本<= PHP 5.3
这样的代码应该可以工作:
function appendA($string) {
$stringA .= 'A';
}
$string = '';
appendA(&$string);
但以后不再工作。为了让 PHP 5.4+ 创建一个通过引用编辑其参数的函数,你必须从一开始就以这种方式声明它。例如:
function appendA(&$string) { // <- first change
$stringA .= 'A';
}
$string = '';
appendA($string); // <- second change
请注意,在 PHP 5.4+ 中,尝试使用 appendA(&$string)
调用函数将产生通知,并且不会通过引用更改值。
根据最初的问题,我没有找到在 nusoap 中client::call()
功能期间更改$params
的地方,所以我认为通过引用提供它们没有意义,但我可能是盲目的。无论如何,方法不是以与 PHP 5.4+ 兼容的方式声明的,因此在方法调用期间&
near 参数不适用于这些 PHP 版本。在这种情况下,需要修改方法。