简体   繁体   English

soap webservice php客户端参数初始化

[英]soap webservice php client parameters initialization

i have a problem when i am calling a remote methode using initilized variable as parameter then i get nothing in resutl, but when i pass a value as parameter everything work fine ! 当我使用初始化变量作为参数调用远程方法时,我遇到了问题,但是在resutl中却什么也没得到,但是当我将值作为参数传递时,一切正常! here is the code in php: 这是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);

You should read a bit about scopes in PHP. 您应该阅读有关PHP 范围的知识。 Your variable client is not set in your function because that is another scope. 您的变量client未在函数中设置,因为这是另一个作用域。 There are some solutions to handle that. 有一些解决方案可以解决这个问题。 You can get the variable with global but that is not really cool. 您可以使用global获取变量,但这并不是很酷。

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

You shouldn't do that. 你不应该那样做。 When you work with globals you don't know where your variable come from. 当您使用全局变量时,您不知道变量的来源。

Another solution is to define your variable as function parameter. 另一个解决方案是将变量定义为函数参数。

function getFirstName($code, $client) {

thats much better. 那就好多了。 If you work with classes you can define the variable as class variable thats much better. 如果使用类,则可以将变量定义为更好的类变量。 For example: 例如:

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;
    }
}

i haven't tested that code but its much better to work with classes. 我还没有测试过该代码,但是与类一起使用会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM