简体   繁体   English

如何在PHP中传递SOAP标头和主体作为参数

[英]How to pass SOAP headers and Body as Parameter in PHP

I'm trying to pass the Header and Body to a SOAP Request. 我正在尝试将标头和正文传递给SOAP请求。 Due to the wrong practice, I'm getting the Connection error. 由于错误的做法,我遇到了连接错误。 When I tried the same using SOAP UI, Im getting the Proper response. 当我使用SOAP UI尝试相同操作时,我得到了正确的响应。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:adp="http://abcddetails.com/">
   <soapenv:Header>
      <adp:UserIdentifierSoapHeaderIn>
         <!--Optional:-->
         <adp:UserName>USER1</adp:UserName>
         <!--Optional:-->
         <adp:Password>PASS</adp:Password>
      </adp:UserIdentifierSoapHeaderIn>
   </soapenv:Header>
   <soapenv:Body>
      <adp:getVehicleDetails>
         <!--Optional:-->
         <adp:request>
            <adp:SystemCode>101</adp:SystemCode>
            <!--Optional:-->
            <adp:UserID>101</adp:UserID>
            <!--Optional:-->
            <adp:PlateInfo>
               <adp:PlateNo>44444</adp:PlateNo>
               <adp:PlateOrgNo>1</adp:PlateOrgNo>
               <adp:PlateColorCode>48</adp:PlateColorCode>
               <adp:PlateKindCode>1</adp:PlateKindCode>
               <adp:PlateTypeCode>1</adp:PlateTypeCode>
               <adp:PlateSourceCode>3</adp:PlateSourceCode>
            </adp:PlateInfo>
            </adp:request>
      </adp:getVehicleDetails>
   </soapenv:Body>
</soapenv:Envelope>

The below is my code: 下面是我的代码:

<?php 

echo "Hello world";
echo "ADDED the below two lines"
ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);


$wsdl   = "https://abcddetails.com/getSoapDetails.asmx?WSDL";
$client = new SoapClient($wsdl, array('trace'=>1));  

 $auth = array(
        'Username'=>'USER1',
        'Password'=>'PASS',
    );
$header = new SOAPHeader($wsdl, 'UserIdentifierSoapHeaderIn', $auth);        
$client->__setSoapHeaders($header);

echo "Header Passed... Body starts";

// web service input params
$request_param = array(
    'getCarDetails' => array(
        'request' => array(
            'SystemCode' => 101,
            'UserID' => 101),
        'PlateInfo' => array(
            'PlateNo' => 44444,
            'PlateOrgNo' => 1,
            'PlateColorCode' => 48,
            'PlateKindCode' => 1,
            'PlateTypeCode' => 1,
            'PlateSourceCode' => 3 )              
        )
    );

$responce_param = null;
try
{
    $responce_param = $client->__soapCall('getCarDetails', ['parameters' => $request_param]);
} 
catch (Exception $e) 
{ 
    echo "<h2>Exception Error!</h2>"; 
    echo $e->getMessage(); 
}

print_r($responce_param);

?>

The error message is 错误消息是

Could not connect to host 无法连接到主机

But as said above, the same xml request is giving proper response through Soap UI application. 但是如上所述,相同的xml请求通过Soap UI应用程序给出了正确的响应。 What could be the issue here? 这里可能是什么问题? I doubt on the Header assignment, Is that so, or somewhere else? 我对标头分配感到怀疑,是这样,还是其他?

Try adding these to your client when you instantiate it. 实例化它们时,尝试将它们添加到您的客户端。 I've had it in the past where it caches from the wsdl and doesn't always connect, but this helps that. 过去我有过它,它从wsdl缓存,并且并不总是连接,但这可以帮助实现这一点。 The trace and exceptions aren't essential but help IMO. 跟踪和异常不是必需的,但可以帮助IMO。

array('trace' => 1, 'cache_wsdl'=>WSDL_CACHE_NONE, 'exceptions' => true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM