简体   繁体   中英

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 ! here is the code in 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. Your variable client is not set in your function because that is another scope. There are some solutions to handle that. You can get the variable with global but that is not really cool.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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