繁体   English   中英

PHP调用SOAP Web服务

[英]PHP To Call a SOAP Webservice

我想要一些帮助。

我正在尝试使用PHP调用网络服务,而我在处理不同类型的消息时遇到了问题。

WSDL是“ http://195.144.16.7/ElastrakEDI/ElastrakEDI.asmx?WSDL”,Web服务称为GetPartMaster。 首先,用户名和密码均为TESTUID和TESTPWD(出于测试目的)。

下面的XML是请求

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:edi="http://elastrak.gr/edi/">
   <soap:Header>
      <edi:AuthHeader>
         <!--Optional:-->
         <edi:Username>TESTUID</edi:Username>
         <!--Optional:-->
         <edi:Password>TESTPWD</edi:Password>
      </edi:AuthHeader>
   </soap:Header>
   <soap:Body>
      <edi:GetPartMaster/>
   </soap:Body>
</soap:Envelope>

此XML是响应

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Header>
      <AuthHeader xmlns="http://elastrak.gr/edi/">
         <Username>TESTUID</Username>
         <Password>TESTPWD</Password>
      </AuthHeader>
   </soap:Header>
   <soap:Body>
      <GetPartMasterResponse xmlns="http://elastrak.gr/edi/">
         <GetPartMasterResult>
            <elastrakPartMasterFile xmlns="">
               <PartMasterURL>http://195.144.16.7/elastrakEDI/Temp/Parts/OTWKOJL4.txt</PartMasterURL>
               <ErrorCode/>
               <ErrorDescription/>
            </elastrakPartMasterFile>
         </GetPartMasterResult>
      </GetPartMasterResponse>
   </soap:Body>
</soap:Envelope>

我已经尝试过下面的php代码,但仍然无法正常工作

<?php

$wsdl = 'http://195.144.16.7/ElastrakEDI/ElastrakEDI.asmx?WSDL';

$trace = true;
$exceptions = true;

$xml_array['Username'] = 'TESTUID';
$xml_array['Password'] = 'TESTPWD';

$client = new SoapClient($wsdl, array('trace' => $trace, 'exceptions' => $exceptions));
$client = new SoapClient($wsdl);
$response = $client->GetPartMaster($xml_array);

try
{
   $client = new SoapClient($wsdl, array('trace' => $trace, 'exceptions' => $exceptions));
   $response = $client->GetPartMaster($xml_array);
}

catch (Exception $e)
{
   echo "Error!";
   echo $e -> getMessage ();
   echo 'Last response: '. $client->__getLastResponse();
}

$response = $response->GetPartMaster->PartMasterURL;

var_dump($response);

?>

再次感谢您的时间和帮助。

您的请求当前构造错误,因为您必须考虑到这些实际上是请求中的一部分:

  • SOAP标头,包含用户名和密码
  • SOAP正文,包含GetPartMaster元素

看一下SoapClient类和__setSoapHeaders方法。

这就是为什么我强烈建议您使用WSDL to PHP生成器发送SOAP Request的原因,因为它可以使您轻松构造请求,然后在使用OOP方法的同时处理响应,而不用担心如何发送参数。 使用生成的PHP SDK和良好的IDE(例如PhpStorm或具有自动补全功能的任何基于Eclipse的IDE),将很容易找到自己的方式。 尝试PackageGenerator项目。

暂无
暂无

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

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