【问题标题】:How to pass SOAP headers and Body as Parameter in PHP如何在 PHP 中将 SOAP 标头和正文作为参数传递
【发布时间】:2019-03-28 18:37:25
【问题描述】:

我正在尝试将标头和正文传递给 SOAP 请求。由于错误的做法,我收到了连接错误。当我使用 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>

以下是我的代码:

<?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 应用程序给出正确的响应。这里可能是什么问题?我怀疑 Header 分配,是这样,还是在其他地方?

【问题讨论】:

  • @DanielO,嗨,你能看看这个
  • Could not connect to host 听起来像是网络/dns/防火墙问题。
  • 也可能是代理或 ssl 相关问题(例如对等验证问题)。您是否通过proxy 连接到 SSL WebService?它是自签名的 ssl 证书吗?尝试禁用对等验证。 'stream_context'=&gt; stream_context_create(array('ssl'=&gt; array('verify_peer'=&gt;false,'verify_peer_name'=&gt;false)))
  • 请尝试另一个 SoapClient,例如 zend-soap 或尝试通过 Curl 发布 SOAP 数据。
  • 谢谢@DanielO 我已经尝试过卷曲并且它工作得很好。没有连接问题。我已经替换了代码。我将添加代码,可能对其他人有帮助

标签: php soap wamp wampserver soap-client


【解决方案1】:

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

array('trace' =&gt; 1, 'cache_wsdl'=&gt;WSDL_CACHE_NONE, 'exceptions' =&gt; true)

【讨论】:

    猜你喜欢
    • 2016-06-18
    • 2016-05-13
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    相关资源
    最近更新 更多