4

如何在 nusoap.xml 中通过 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);
4

2 回答 2

2

关于通过引用传递值:

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()打肥皂电话。

这就是您在示例中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 没有任何优势。

因为:

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

如果您想在任何情况下通过引用传递 $params 怎么办?我可以做吗?

是的,你可以!
要实现这一点,您必须复制nusoap_client.php并调用它nusoap_client_new.php
在那里将类名形式修改nusoap_clientnusoap_client_new.

// From this 
class nusoap_client extends nusoap_base  {

// To this 
class nusoap_client_new extends nusoap_base  {

修改nusoap_client_new::call()在 params 中添加 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 
       .
       .
       .

     */ 
}

最后将您的代码更新为 require 和 usenusoap_client_new::call()而不是nusoap_client::call().

于 2015-04-04T00:55:14.593 回答
1

专为古斯塔沃而设。

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 函数中$params更改的地方client::call(),所以我认为通过引用提供它们没有意义,但我可能是盲目的。无论如何,方法未以与 PHP 5.4+ 兼容的方式声明,因此&方法调用期间的近参数不适用于那些 PHP 版本。在这种情况下需要修改方法。

于 2015-04-02T09:43:28.097 回答