繁体   English   中英

PHP-为SOAP请求创建XML

[英]PHP - creating XML for SOAP Request

我对使用SOAP的经验为零,并且在任何地方都可以进行搜索以解决此问题。

我正在尝试创建一个XML请求,以从我的PHP代码发送到SOAP服务器。 这是一个现场获取汽车保险报价的网站。

WSDL链接: https : //eins2.zurich.com.my/VIXAPI/VixService.svc?wsdl

我已经使用SOAPUI软件进行了测试,并且为请求提供了如下XML:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:fnGetVehicleDtlsByVIX>
         <!--Optional:-->
         <tem:VehInputInfo>{"AgentCode":"D02940-000","ParticipantCode":"06","RequestDateTime":"2017-Mar-17 11:00:00 PM","ID":"850321-07-5179","VehNo":"WA823H","Signature":"E448A5DE70160A7C541306B38ABAE3C8826ACD262DF217F9AA8B32244374C5E2E66D26D31874BBD832E43A6A569D20F2DFE8F674AECCFD698850BEBFB13767FD"}</tem:VehInputInfo>
      </tem:fnGetVehicleDtlsByVIX>
   </soapenv:Body>
</soapenv:Envelope>

元素VehInputInfo是必需的输入,并且是JSON格式的字符串。 响应是正确的(通过SOAPUI软件,请查看此处的屏幕截图),下一步是我试图在PHP代码中传递上面的XML请求。

以下是我的PHP代码:

<?
// error reporting
error_reporting(E_ALL - E_NOTICE); 
ini_set('display_errors','On');

//header('Content-type: text/xml');

$wsdl = 'https://eins2.zurich.com.my/VIXAPI/VixService.svc?wsdl';

$client = new SoapClient($wsdl, array('trace'=> 1));

$xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:fnGetVehicleDtlsByVIX>
         <!--Optional:-->
         <tem:VehInputInfo>{"AgentCode":"D02940-000","ParticipantCode":"06","RequestDateTime":"2017-Mar-17 11:00:00 PM","ID":"850321-07-5179","VehNo":"WA823H","Signature":"E448A5DE70160A7C541306B38ABAE3C8826ACD262DF217F9AA8B32244374C5E2E66D26D31874BBD832E43A6A569D20F2DFE8F674AECCFD698850BEBFB13767FD"}</tem:VehInputInfo>
      </tem:fnGetVehicleDtlsByVIX>
   </soapenv:Body>
</soapenv:Envelope>';

//echo $xml;

try
{
    $result = $client-> fnGetVehicleDtlsByVIX($xml);
} 
catch (Exception $e)  
{
  var_dump($e->getMessage());
  var_dump($client->__getLastRequest());
  var_dump($client->__getLastResponse());
}

但是我只有错误

不知道这是创建XML的正确方法还是其他方法?

有人可以帮助我吗? 先感谢您。

我解决了问题。 更改为使用cURL,如下所示。 这可能不是最佳答案,但它解决了我的问题。

<?php 
// error reporting
error_reporting(E_ALL - E_NOTICE); 
ini_set('display_errors','On');

$soapUrl = "https://eins2.zurich.com.my/VIXAPI/VixService.svc?wsdl"; //URL of WSDL

// xml post structure

$xml_post_string = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:fnGetVehicleDtlsByVIX>
         <!--Optional:-->
         <tem:VehInputInfo>{"AgentCode":"D02940-000","ParticipantCode":"06","RequestDateTime":"2017-Mar-17 11:00:00 PM","ID":"850321-07-5179","VehNo":"WA823H","Signature":"E448A5DE70160A7C541306B38ABAE3C8826ACD262DF217F9AA8B32244374C5E2E66D26D31874BBD832E43A6A569D20F2DFE8F674AECCFD698850BEBFB13767FD"}</tem:VehInputInfo>
      </tem:fnGetVehicleDtlsByVIX>
   </soapenv:Body>
</soapenv:Envelope>';


$headers = array(
            "POST: https://eins2.zurich.com.my/VIXAPI/VixService.svc HTTP/1.1",
            "Content-type: text/xml;charset=\"UTF-8\"",
            "Accept-Encoding: gzip,deflate",
            "Cache-Control: no-cache",
            "Pragma: no-cache",
            "SOAPAction: \"http://tempuri.org/IVixService/fnGetVehicleDtlsByVIX\"", 
            "Content-Length: ".strlen($xml_post_string),
            "Host: eins2.zurich.com.my",
            "Connection: Keep-Alive"
        ); //SOAPAction: your op URL

$url = $soapUrl;

//print_r($headers);

// PHP cURL  for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// converting
$response = curl_exec($ch); 
curl_close($ch);


print_r($response);

暂无
暂无

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

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