如何在 PHP 中将 SOAP 标头和正文作为参数传递 [英] How to pass SOAP headers and Body as Parameter in PHP

查看:23
本文介绍了如何在 PHP 中将 SOAP 标头和正文作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将标头和正文传递给 SOAP 请求.由于错误的做法,我收到了连接错误.当我使用 SOAP UI 尝试同样的操作时,我得到了正确的响应.

I'm trying to pass the Header and Body to a SOAP Request. Due to the wrong practice, I'm getting the Connection error. When I tried the same using SOAP UI, Im getting the Proper response.

<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>

下面是我的代码:

<?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);

?>

错误信息是

无法连接到主机

但如上所述,相同的 xml 请求通过 Soap UI 应用程序给出了正确的响应.这里可能是什么问题?我怀疑标题分配,是这样,还是其他地方?

But as said above, the same xml request is giving proper response through Soap UI application. What could be the issue here? I doubt on the Header assignment, Is that so, or somewhere else?

推荐答案

尝试在实例化客户端时将这些添加到客户端.我过去曾经使用过它从 wsdl 缓存并且并不总是连接,但这有帮助.跟踪和异常不是必需的,但有助于 IMO.

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. The trace and exceptions aren't essential but help IMO.

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

这篇关于如何在 PHP 中将 SOAP 标头和正文作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆