如何使用struct输入参数调用soap webservice?

时间:2016-06-05 12:26:26

标签: php soap struct wsdl nusoap

我想与SOAP(作为客户端)进行交互,但无法获得输入参数的正确语法。我有一个WSDL URL,我用SoapUI测试它,它正确返回结果。 WSDL中定义了两个函数,但我只需要一个(下面的“FirstFunction”)。这是我运行的脚本,用于获取有关可用功能和类型的信息:

$client = new SoapClient("http://example.com/webservices?wsdl");
var_dump($client->__getFunctions()); 
var_dump($client->__getTypes()); 

这是它产生的输出:

array(
  [0] => "FirstFunction Function1(FirstFunction $parameters)",
  [1] => "SecondFunction Function2(SecondFunction $parameters)",
);

struct Amount {
 anyURI Identifier;
 Information charge;
 string referenceCode;
}

struct Information {
 string description;
 decimal amount;
 string code;
}

根据上述结果,我使用nusoapphp开发了我的客户端,如下所示:

 class Information 
  {
    public $description;
    public $amount;
    public $code;
  }

  class Amount {

        public $Identifier;
        public $charge;
        public $referenceCode;
  }
$charge = new Information();
$charge->description = "ROUTE=XXX|abc=".$code;
$charge->amount = "NULL";
$charge->code = $chargecode;

$params = new Amount();
$params->Identifier =$num;
$params->charge = $charge;
$params->referenceCode = $refcode;

$header = new SoapHeader('key', $key);

$client->__setSoapHeaders($header);


try
    {
        $res = $client->__call('charge',array('parametrs'=>$params)); 

        print_r($res->return);
    }
catch(PDOException $e)
    {
    print_r($e->getMessage());
    }

我收到以下错误:

Uncaught SoapFault exception: [soapenv:Server] unknown

1 个答案:

答案 0 :(得分:1)

在我看来,实现它的最好方法是使用WSDL到php生成器,例如PackageGenerator项目。它抽象了整个过程,因此您只需要处理对象而不必担心SOAP。