Said Marar
Said Marar

Reputation: 105

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

Upvotes: 1

Views: 402

Answers (1)

René Höhle
René Höhle

Reputation: 27325

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.

Upvotes: 1

Related Questions