信封中缺少SOAP标头(Zend_Soap_Client,wsdl模式)

时间:2012-11-12 19:55:54

标签: zend-framework soap wsdl zend-soap

我正在努力在Zend Soap Client实例中创建一个合适的SOAP信封。

以下是从WSDL文件生成的预期包络的示例:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                      xmlns:v1="http://coc.gov/xsd/ESB/SupplementalData/V1" 
                      xmlns:ns="http://schemas.calgary.ca/xsd/familycommunitysurvey/2012/08" 
                      xmlns:ns1="http://schemas.calgary.ca/xsd/familycommunitysurveycdm/2012/08">
   <soapenv:Header>
      <v1:SupplementalData>
         <v1:SourceName>CFS</v1:SourceName>
         <v1:ServiceProvider>
            <v1:Name>FamilyCommunitySurvey</v1:Name>
            <v1:OperationName>GetCodeLookupByName</v1:OperationName>
         </v1:ServiceProvider>
         <v1:CorrelationID>23451235634</v1:CorrelationID>
      </v1:SupplementalData>
   </soapenv:Header>

   <soapenv:Body>
      <ns:GetCodeLookupByNameRequest>
         <ns1:Name>Country</ns1:Name>
      </ns:GetCodeLookupByNameRequest>
   </soapenv:Body>
</soapenv:Envelope>

我需要设置的属性是&#39; SourceName&#39;,&#39; CorrelationID&#39;在标题中,&#39;名称&#39;在身体里。 我是这样做的:

    $fsiiConnect = new Zend_Soap_Client($wsdl, array('soap_version' => SOAP_1_1));

    $fsiiConnect->setLocation($endPoint);
    $fsiiConnect->setHttpLogin($userName);
    $fsiiConnect->setHttpPassword($password);

    $data=array(
       'SourceName' => 'CFS',
       'CorrelationID' => '1234251435632',
       'Name' => 'Country'
   );

   try {
     $results = $fsiiConnect->GetCodeLookupsByName($data);
   }
   catch (Exception $e) {
     print $fsiiConnect->getLastRequest();
   }

getLastRequest()的内容是:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
                   xmlns:ns1="http://schemas.calgary.ca/xsd/familycommunitysurveycdm/2012/08" 
                   xmlns:ns2="http://schemas.calgary.ca/xsd/familycommunitysurvey/2012/08">
   <SOAP-ENV:Body>
      <ns2:GetCodeLookupByNameRequest>
         <ns1:Name>Country</ns1:Name>
      </ns2:GetCodeLookupByNameRequest>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

这样它只能为正文中的对象设置属性,而不是标题。任何想法为什么会非常感激!

1 个答案:

答案 0 :(得分:3)

我需要使用addSoapInputHeader来设置标头属性。其他一切都归于身体。这是一个多步骤的过程,所以我想我会在这里记录它以防其他人发现它有用:

<强> 1。使用StdClass创建多级结构

$headerObj = new StdClass();
$headerObj->ServiceProvider = array(
        'Name'          => 'FamilyCommunitySurvey',
        'OperationName' => 'GetCodeLookupByName'
        );
$headerObj->SourceName = 'foo';
$headerObj->CorrelationID = 'bar';

<强> 2。使用SoapHeader创建适当的XML结构

 $header = new SoapHeader('http://coc.gov/xsd/ESB/SupplementalData/V1','SupplementalData',$headerObj);

第一个参数是命名空间,如WSDL文档中所定义。第二个参数是顶级元素。最后一个元素是我们格式化的SOAP标头。

第3。使用addSoapInputHeader将标头添加到Zend_Soap_Client实例

$connect->addSoapInputHeader($header);

这与上面的代码结合产生了一个看起来像预期的请求:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
                   xmlns:ns1="http://schemas.calgary.ca/xsd/familycommunitysurveycdm/2012/08" 
                   xmlns:ns2="http://schemas.calgary.ca/xsd/familycommunitysurvey/2012/08" 
                   xmlns:ns3="http://coc.gov/xsd/ESB/SupplementalData/V1">
   <SOAP-ENV:Header>
      <ns3:SupplementalData>
         <ns3:SourceName>CFS</ns3:SourceName>
         <ns3:ServiceProvider>
            <ns3:Name>FamilyCommunitySurvey</ns3:Name>
            <ns3:OperationName>GetCodeLookupByName</ns3:OperationName>
         </ns3:ServiceProvider>
         <ns3:CorrelationID>1352758058</ns3:CorrelationID>
      </ns3:SupplementalData>
   </SOAP-ENV:Header>
   <SOAP-ENV:Body>
      <ns2:GetCodeLookupByNameRequest>
         <ns1:Name>Country</ns1:Name>
      </ns2:GetCodeLookupByNameRequest>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>