2013-08-22 55 views
3

我已經在PHP中創建了一個soap客戶端,但是對於第二個我希望使用的請求,我似乎無法使PHP正確構建請求。從SoapUI重新創建一個可用的SOAP請求到PHP

這是請求在了SoapUI

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://v1.productapi.gs1ca.org" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <v1:searchProducts> 
     <sessionId>f53c5450-392e-4ca4-b592-adbb436cfe1f</sessionId> 
     <searchCriteria> 
      <v1:AttributeValue> 
       <v1:attribute>dateupdated</v1:attribute> 
       <v1:value i:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">08/01/2013</v1:value> 
      </v1:AttributeValue> 
<v1:AttributeValue> 
       <v1:attribute>dateupdatedcompare</v1:attribute> 
       <v1:value i:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">1</v1:value> 
      </v1:AttributeValue> 
     </searchCriteria> 
     <includeImageAttributes>0</includeImageAttributes> 
     <sortOrder>dateupdated</sortOrder> 
     </v1:searchProducts> 
    </soapenv:Body> 
</soapenv:Envelope> 

我將如何使用PHP來格式化XML以同樣的方式作爲工作要求的作品?

取得了一些進展。

我已經能夠重新創建xml到現在。該請求是這樣的:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://v1.productapi.gs1ca.org"> 
    <SOAP-ENV:Body> 
     <ns1:searchProducts> 
      <sessionId>2a7d0294-8d96-428d-abd8-08add9cfc427</sessionId> 
      <searchCriteria> 
       <ns1:AttributeValue> 
        <ns1:attribute>dateupdated</ns1:attribute> 
        <ns1:value>01/01/2013</ns1:value> 
       </ns1:AttributeValue> 
       <ns1:AttributeValue> 
        <ns1:attribute>dateupdatedcompare</ns1:attribute> 
        <ns1:value>1</ns1:value> 
       </ns1:AttributeValue> 
      </searchCriteria> 
      <includeImageAttributes>false</includeImageAttributes> 
      <sortOrder>dateupdated</sortOrder> 
     </ns1:searchProducts> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

的PHP創建的要求是:

$args0 = array(
     'sessionid'=>$session, 
     'searchcriteria'=> array(array('attribute'=>'dateupdated','value'=>'01/01/2013'),array('attribute'=>'dateupdatedcompare','value'=>'1')), 
     'includeimageattributes'=>0, 
     'sortorder'=>'dateupdated'); 

$result = $client->__soapCall('searchProducts',$args0); 

,這將引發的錯誤是:

Error: SoapFault exception: [a:DeserializationFailed] The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://v1.productapi.gs1ca.org:searchCriteria. The InnerException message was 'Element value from namespace http://v1.productapi.gs1ca.org cannot have child contents to be deserialized as an object. Please use XmlNode[] to deserialize this pattern of XML. 

我仍然缺少包膜的一部分:

xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 

而且我需要看起來像這樣的價值標籤:

<v1:value i:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema"> 

任何想法如何我可以添加這些部分?

回答

3

好吧!我終於明白了這一點。這是醜陋的,但它的工作。

try { 
     $args = array(
      'sessionid'=>$session, 
      'searchcriteria'=> new SoapVar('<searchCriteria><ns1:AttributeValue> 
               <ns1:attribute>dateupdated</ns1:attribute> 
               <ns1:value xsi:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">01/01/2013</ns1:value> 
              </ns1:AttributeValue> 
              <ns1:AttributeValue> 
               <ns1:attribute>dateupdatedcompare</ns1:attribute> 
               <ns1:value xsi:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">1</ns1:value> 
              </ns1:AttributeValue></searchCriteria> 
              ', XSD_ANYXML, "http://www.w3.org/2001/XMLSchema-instance"), 
      'includeimageattributes'=>0, 
      'sortorder'=>'dateupdated'); 
    $result = $client->__soapCall('searchProducts',$args); 
    } catch (SoapFault $e) { 
     echo "Error: {$e}"; 
    } 
+0

沒有更好的方法來實現__soapCall?當結構變得更復雜時,應該更好地使用更多模塊化的東西 – axel