如何使SoapClient在调用之前不连接

时间:2015-12-11 19:23:44

标签: php soap soap-client

请告知此问题。是否有可能使本机php SoapClient在初始化时不连接到主机,直到没有调用某个soap方法?或者我如何扩展它以实现此行为。

例如我现在所拥有的:

$this->client=new SoapClient($this->host,$this->params); //now it connects to host to load wsdl
$this->client->Add(123); //it performs some action
$this->client->Remove(123); //performs another

我想要的是什么:

$this->client=new SoapClient($this->host,$this->params); //just initializing do not connect to host
$this->client->Add(123); //it connects to host to load or check cached wsdl and performs some action
$this->client->Remove(123); //again checks wsdl and perfoms action

或者:

  class Someklass{
    static protected $host=null;
    static protected $params=null;
    static protected $client=null;

   public function __construct() {
       $this->params=array("connect_on_init"=>false);
       $this->client=new SoapClient($this->host,$this->params); // just wraps the model
   }

   public function doSomeAction(){
     $this->client->connect(); //it  actually connects to host and checks wsdl provided
     $action=$this->client->Add(123); //making some action
     return $action;
   }

1 个答案:

答案 0 :(得分:0)

我无法理解你想要什么(以及为什么)。可能你需要像这种包装器这样的东西:

class someClass extends somethingElse
{
    private $isConnected = false;
    private $soapConnection;
    private $soapHost;
    private $soapParam;

    public soapConnect($host, $param)
    {
        $this->soapHost = $host;
        $this->soapParam = $param;
    }

    private doSoapConnect()
    {
        $this->soapConnection = new SoapClient($this->soapHost, $this->soapParams);
        $this->isConnected = true;
    }

    public wrappedAdd($val)
    {
        if (!$this->isConnected)
            $this->doSoapConnect();
        $this->soapConnection->Add($val);
    }
}