soap-webservice-php客户端参数初始化

soap-webservice-php客户端参数初始化,php,soap,Php,Soap,当我使用初始化变量作为参数调用远程方法时,我遇到了一个问题,然后我在resutl中什么也得不到,但是当我将一个值作为参数传递时,一切正常!以下是php中的代码: $serviceWsdl = 'http://localhost:8080/Test/services/Test?wsdl'; $client = new SoapClient($serviceWsdl); function getFirstName($code){ $firstname = $client->getFi

当我使用初始化变量作为参数调用远程方法时,我遇到了一个问题,然后我在resutl中什么也得不到,但是当我将一个值作为参数传递时,一切正常!以下是php中的代码:

$serviceWsdl = 'http://localhost:8080/Test/services/Test?wsdl';
$client = new SoapClient($serviceWsdl);

function getFirstName($code){
    $firstname = $client->getFirstName(array('code' => $code));
    return $firstname->return;
}

$c=1;
$result=getFirstName($c);
var_dump($result);

您应该阅读一些关于PHP的知识。函数中未设置变量
client
,因为这是另一个作用域。有一些解决方案可以解决这个问题。您可以使用
global
获取变量,但这并不是很酷

function getFirstName($code){
    global $client;
    $firstname = $client->getFirstName(array('code' => $code));
    return $firstname->return;
}
你不应该那样做。使用全局变量时,您不知道变量来自何处

另一种解决方案是将变量定义为函数参数

function getFirstName($code, $client) {
那好多了。如果您使用类,您可以将变量定义为类变量,这会更好。例如:

class ApiConnection {
    private $serviceWsdl = 'http://localhost:8080/Test/services/Test?wsdl';
    private $client;

    public function __construct() {
        $this->client = new SoapClient($this->serviceWsdl);
    }

    public function getFirstName($code){
        $firstname = $this->client->getFirstName(array('code' => $code));
        return $firstname->return;
    }
}

我还没有测试过该代码,但是使用类要好得多

谢谢你,伙计。。。问题解决了。。很长一段时间我没有用php编写代码-u-。。。有时候简单的事情让事情看起来很难