加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 16:45:34 所属栏目:PHP教程 来源:网络整理
导读:如何在nusoap中通过ref设置参数.在下面的代码中,我应该通过ref(status和recId)设置两个参数.请注意不工作: $params = array( 'username' = GATEWAY_USERNAME,'password' = GATEWAY_PASSWORD,'from' = GATEWAY_NUMBER,'to' = array($to),'text' = $message,'
如何在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.
我将省略与$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,$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()中没有以任何方式修改
>即使你必须再次使用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(),$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().

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读