gpt4 book ai didi

php - 如何在nusoap中通过引用设置参数?

转载 作者:可可西里 更新时间:2023-10-31 23:23:33 26 4
gpt4 key购买 nike

如何在 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 中,客户端类是 nusoap_client
在该类中,您有 nusoap_client::call() 来进行 soap 调用。

这就是 nusoap_client::call() 中附加到数组 $params 的内容,在您的示例中这是您的 $sendParams。< br/>我将省略所有与 $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 没有任何优势。

因为:

  1. $params 未在 nusoap_client::call() 中以任何方式修改
  2. 即使您在必须再次使用 nusoap_client::call() 之后在其他地方修改 $params

如果在任何情况下都想通过引用传递 $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 {

修改 nusoap_client_new::call() 方法,在参数中添加 ref,如下所示:

/*
* 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 - 如何在nusoap中通过引用设置参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10862836/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com