SOAP和PHP。如何检索此响应?

时间:2015-02-04 10:15:35

标签: php xml web-services soap

我暂时遇到问题。我试图获得这个在SOAP UI中工作的请求,以便在PHP中工作:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:otp="OTPNA">
<soapenv:Header/>
<soapenv:Body>
  <otp:ProductRequestIn>
     <otp:fromRole>
        <otp:PartnerRoleDescription>
           <otp:ContactInformation>
              <otp:contactName>
                 <otp:FreeFormText lang="NL">?</otp:FreeFormText>
              </otp:contactName>
              <otp:EmailAddress>?</otp:EmailAddress>
              <!--Optional:-->
              <otp:facsimileNumber>
                 <otp:CommunicationsNumber>?</otp:CommunicationsNumber>
              </otp:facsimileNumber>
              <otp:telephoneNumber>
                 <otp:CommunicationsNumber>?</otp:CommunicationsNumber>
              </otp:telephoneNumber>
           </otp:ContactInformation>
           <otp:GlobalPartnerRoleClassificationCode>?  </otp:GlobalPartnerRoleClassificationCode>
           <otp:PartnerDescription>
              <otp:BusinessDescription>
                 <!--Optional:-->
                 <otp:GlobalBusinessIdentifier>-BZNSID-</otp:GlobalBusinessIdentifier>
                 <!--Optional:-->
                 <otp:GlobalSupplyChainCode>?</otp:GlobalSupplyChainCode>
                 <!--Optional:-->
                 <otp:businessName>
                    <otp:FreeFormText lang="?">?</otp:FreeFormText>
                 </otp:businessName>
              </otp:BusinessDescription>
              <otp:GlobalPartnerClassificationCode>?   </otp:GlobalPartnerClassificationCode>
           </otp:PartnerDescription>
        </otp:PartnerRoleDescription>
     </otp:fromRole>
     <otp:Authentication>
        <otp:Username>-USERNAME-</otp:Username>
        <otp:Password>-PASSWORD-</otp:Password>
     </otp:Authentication>
     <otp:productIdentifier type="PDI">---</otp:productIdentifier>
     <otp:lang>NL</otp:lang>
     <otp:ProductInformationType>RIC</otp:ProductInformationType>
     <otp:RequestedPartner>
        <otp:PartnerID>-PARTNERID-</otp:PartnerID>
     </otp:RequestedPartner>
  </otp:ProductRequestIn>

WSDL文件返回1个名为ProductRequest的函数。

我尝试了很多建议,到目前为止还没有任何建议。我不确定我是否应该在函数ProductRequest中使用XML,而XML是参数(没有工作),或者我应该构建一些数组并将其解析为参数(没有工作,或者没有& #39; t做得正确)

可以在此处找到WSDL:http://uddi.onetrail.net/uddidocs/ICT_PROD/Deployment/ProductRequest/ProductRequest.wsdl

1 个答案:

答案 0 :(得分:1)

尝试使用xml中的结构准备php数组,然后:

<?php
$client = new \SoapClient($wsdl, [
        'trace' => true, 
        'style' => SOAP_DOCUMENT, 
        'use' => SOAP_LITERAL, 
        'exceptions' => true
        ]);
 $params = [
     'fromRole' => [
         'PartnerRoleDescription' => [
             'ContactInformation' => [
                 'contactName' => '?', 
                 'EmailAddress' => '?',
                 'telephoneNumber' => [
                     'CommunicationsNumber' => '?',
                 ]
              ],
              'GlobalPartnerRoleClassificationCode' => '?',
              'PartnerDescription' => [
                  'BusinessDescription' => [
                  ]
              ],
              'GlobalPartnerClassificationCode' => '?',
          ] 
      ],
      'Authentication' => [
          'Username' => 'USERNAME',
          'Password' => 'PASSWORD',
      ],
      'productIdentifier' => '---',
      'lang' => 'NL',
      'ProductInformationType' => 'RIC',
      'RequestedPartner' => [
          'PartnerID' => 'PARTNERID',
      ]
 ];

 $result = $client->__soapCall('Your SOAP method', [$params]);

还有另一种方法(但更丑陋)用curl调用SOAP。您必须在$ xmlString变量中创建包含XML的字符串。例如:

<?php
$headers = array(
    "Content-Type: application/soap+xml; charset=utf-8",
    "Content-Length: ".strlen($xmlString)
    ); 


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $soapUrl);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlString);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    try {
        $response = curl_exec($ch);
    } catch (\Exception $e) {

        echo $e->getMessage();
    }

    curl_close($ch);

    $response = str_replace("soap:", "", $response);

    $xml = simplexml_load_string($response, 'SimpleXMLElement');