2012-01-11 41 views
0

我在根據WebService WSDL創建適當的變量時遇到了問題。我已經在python中使用suds 0.4 SOAP庫成功實現了這個簡單的功能。PHP中的數據類型SOAP

Python實現(跟蹤器是我的SOAP客戶端對象消耗WSDL):

c = self.tracker.factory.create("ns4:Text") 
c.type = "text/html" 
c.content = "my content goes here" 
self.tracker.service.createComment(c) 

如何在PHP中實現這一點?乍一看,我不太明白如何用PHP SOAP擴展來實現這一點。 「...... factory.create(」ns4:Text「)在python中似乎很方便,我可以檢查對象的屬性並輕鬆將其傳遞給我的可用函數。

我真的需要在PHP方式如下:。

$c->type = "text/html"; 
$c->content = "my content goes here"; 
$this->tracker->__soapCall('createComment',array($c)); 

此實現需要,我知道,將定義對象的所有屬性我有+37的屬性,還嵌套複雜數據類型,只有其中4個需要,我想要將它傳遞給只有4個屬性填充的服務器,但仍然作爲一個完整的對象與所有的屬性定義...?

這是否這樣做任何意義?

綜述:python從wsdl文件中創建我完整的對象,我如何在PHP中獲得這個?

回答

1

PHP可以使用WSDL文件生成一組適當的方法,您可以將通用對象,數組或標量作爲參數傳遞給該方法。您還可以指定哪些類映射到哪些方法(classmap選項),哪些類型聲明使用SoapClient類的第二個參數映射到哪些序列化回調函數(typemap選項)。

class doRequestMethod { 
    public $id; 
    public $attribute; 
} 

class theResponseClass { 
    /* ... */ 
} 

$options = array(
    'classmap' => array(
     'doRequest' => 'doRequestMethod', 
     'theResponse' => 'theResponseClass' 
     /* ... */ 
    ), 
    'typemap' => array(
     0 => array(
      'type_ns' => 'http://example.com/schema/wsdl_type.xsd', 
      'type_name"' => 'wsdl_type', 
      'from_xml'  => function ($xml_string) { /* ... */ }, 
      'to_xml'  => function ($soap_object) { /* ... */ } 
     ) 
     /* ... */ 
    ) 
) 

$client = new SoapClient('/path/to/filename.wsdl', $options); 

$request = new doRequestMethod(); 
$request->id = 0; 
$request->attribute = "FooBar"; 
$result = $client->doRequest($request); 

/* 
* If 'dorequest' returns a 'theResponse' in the WSDL, 
* then $result should of the type 'theResponseClass'. 
*/ 
assert(get_class($result) === 'theResponseClass'); 

這是很多工作,所以我建議爲您自己使用子類化SoapClient。另外,爲了使代碼更易於調試,儘可能多地使用PHP類型提示函數和參數參數。它可以防止整個類別的錯誤,值得小小的性能損失。

+1

我喜歡你乾淨的答案,但我不明白typemap數組鍵,似乎wsdl_type_ns不存在,但有一個type_ns。這是一個錯字嗎? – Benoit 2012-12-06 17:58:52

+0

謝謝,趕上!編輯。 – jmkeyes 2012-12-07 03:41:26